Announcement

Collapse
No announcement yet.

making Units work

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • making Units work

    I am trying to take some large (debugged and working) Subroutines and Functions out of my main program and compiling them into a unit (.PBU) file. I have RT(F)M but I think that it assumes greater experience than I have. How is this done in cookbook terms (i.e. step 1, step 2, etc.) ? In one case I have a subroutine that simply draws a box on the screen:
    SUB DrawSingleBox (UpperLeftRow%, UpperLeftColumn%, LowerRightRow%, LowerRightColumn%, Foreground%, Background%)
    'code to draw box
    END SUB

    In another case I am trying to compile the GetCMOS () program out of Quickpack Professional. I have DECLARED both in the Main program and unit file but still get error messages (462: Undefined SUB/FUNCTION reference). Do I need an EXTERN statement?
    Help!

    ------------------

  • #2
    You need to link the unit to your program, e.g.

    $LINK "MyPbu.PBU"


    ------------------

    Comment


    • #3
      Without knowing exactly how you are going about making the
      segments units, I can give you one example of what was giving
      me problems when I started.

      You have to make the SUB's-n-FUNCTION's public. i.e.,

      $compile unit
      function DoSomething(a,b,c) PUBLIC
      ...
      ...
      end function

      If the function is being compiled in the main body of the
      program, you don't need the PUBLIC statement but in UNITS
      you do.

      Hope this helps.

      ------------------
      And also what Jose said.


      [This message has been edited by Mel Bishop (edited March 08, 2003).]
      There are no atheists in a fox hole or the morning of a math test.
      If my flag offends you, I'll help you pack.

      Comment


      • #4
        In regard to Jose's post I have linked the unit

        with regard to Mel's post, the data presented seems to be exactly opposite to what is stated in the language reference; never put a PUBLIC declaration in a unit. Which is correct. Also, when I add the PUBLIC statement as presented in Mel's example I get more error messages. It doesn't seem as though this should be all that difficult. Anyone have any suggestions?

        ------------------

        Comment


        • #5
          You should never use PUBLIC for a VARIABLE in a unit; however, Mel
          is correct - procedures included in units must have the PUBLIC
          verb in order for your main program to use them:

          Code:
              $COMPILE UNIT
              
              FUNCTION DoThisOrThat$ (BYVAL A AS INTEGER) [b]PUBLIC[/b]

          ------------------




          [This message has been edited by Clay Clear (edited March 12, 2003).]

          Comment


          • #6
            Which error messages are you having?

            Example of little unit:
            Code:
            ' ==========================================================================
            ' DLDate.BAS - Rutinas ptratamiento fechas
            ' (C) 1999 Jos‚ Roca - PowerBASIC 3.5
            ' ==========================================================================
            
            $COMPILE UNIT "DLDate"
            $CODE SEG "DLPBU"
            
            DEFINT A-Z
            
            DECLARE FUNCTION InvierteFecha$(BYVAL Fecha$)
            DECLARE FUNCTION FechaValida%(BYVAL Fecha$)
            
            ' ==========================================================================
            ' Invierte la posici¢n del a¤o y el d¡a
            ' ==========================================================================
            FUNCTION InvierteFecha$(BYVAL Fecha AS STRING) PUBLIC
            
               InvierteFecha$ = MID$(Fecha$,5)+MID$(Fecha$,3,2)+MID$(Fecha$,1,2)
            
            END FUNCTION
            
            ' ==========================================================================
            ' Comprueba la validez de una fecha
            ' ==========================================================================
            FUNCTION FechaValida%(BYVAL Fecha AS STRING) PUBLIC
            
               DIM DiasMes(12) AS LOCAL INTEGER
               DIM i AS LOCAL INTEGER
               DIM Di AS LOCAL STRING
               DIM Dia AS LOCAL INTEGER
               DIM Me AS LOCAL STRING
               DIM Mes AS LOCAL INTEGER
               DIM An AS LOCAL STRING
               DIM Anyo AS LOCAL INTEGER
               DIM Sigl AS LOCAL STRING
               DIM Siglo AS LOCAL INTEGER
            
               REDIM DiasMes%(12)
               DiasMes%(1) = 31: DiasMes%(2) = 28: DiasMes%(3) = 31
               DiasMes%(4) = 30: DiasMes%(5) = 31: DiasMes%(6) = 30
               DiasMes%(7) = 31: DiasMes%(8) = 31: DiasMes%(9) = 30
               DiasMes%(10) = 31: DiasMes%(11) = 30: DiasMes%(12) = 31
            
               FechaValida% = 0
               Di$ = MID$(Fecha,1,2)
               Dia% = VAL(Di$)
               Me$  = MID$(Fecha$,3,2)
               Mes% = VAL(Me$)
               IF LEN(Fecha$) = 4 THEN
                  An$ = MID$(DATE$, 9)
               ELSE
                  An$ = MID$(Fecha$, 5)
               END IF
               Anyo% = VAL(An$)
               IF LEN(An$) = 4 THEN
                  Siglo% = 0
               ELSE
                  Sigl$ = MID$(DATE$,7,2)
                  Siglo% = VAL(Sigl$) * 100
               END IF
               IF ((ABS(Siglo% + Anyo%)) MOD 4) = 0 THEN
                  DiasMes%(2) = 29                          ' { A¤o bisiesto }
               ELSE
                  DiasMes%(2) = 28
               END IF
               IF Mes% < 1 OR Mes% > 12 THEN
                  FechaValida% = 0
               ELSE
                  IF (Mes% >= 1) AND (Mes% <= 12) _
                     AND (Dia% >= 1) AND (Dia% <= DiasMes%(Mes%)) THEN _
                     FechaValida% = -1
               END IF
            
            END FUNCTION
            
            ' ==========================================================================
            To use it in your program, you have to declare the functions
            Code:
            DECLARE FUNCTION InvierteFecha$(BYVAL Fecha$)
            DECLARE FUNCTION FechaValida%(BYVAL Fecha$)
            and link the PBU
            Code:
            $LINK "DLDate.PBU"
            That's all. You can also made a .PBL library, include your units in it and link the library, e.g. $LINK "MyLibrary.PBL", instead of link each .PBU.

            Also is not the same to declare PUBLIC variables than declare a SUB or a FUNCTION as PUBLIC. If you don't declare the SUBs or FUNCTIONs as PUBLIC, they can't be accessed by the main program or other PBUs.


            ------------------

            Comment


            • #7
              Originally posted by Steve Andrues:
              ...never put a PUBLIC declaration in a unit....
              Well Steve, it seems you are right about the documentation.
              When I converted my PB2.1f programs over to 3.5 however, I did
              have to make some minor modifications but the PUBLIC statements
              in the UNITS was not one of them. I never gave it a thought
              until your latest post.

              Did some checking on this subject and everything seems to be
              working okay on this end.
              ------------------
              Okay, I think Clay made that fairly clear. Seems I was typing
              this as he posted his reply.

              To expand a little on Clays reply, I think you may be having
              a problem with SHARE(ing) variables.




              [This message has been edited by Mel Bishop (edited March 12, 2003).]
              There are no atheists in a fox hole or the morning of a math test.
              If my flag offends you, I'll help you pack.

              Comment


              • #8
                During the time I posted my last whine and these last responses, I was able to get my SUB's to compile as a .PBU (Brain pointer to infinity I think was the problem). Now my problem is that the SUBS/FUNCTIONS provided in Quickpack Professional are not recognized (i.e. Error 462: Undefined SUB/FUNCTION). Any Ideas? (BTW Jose, I enjoyed reading code in Spanish!)

                ------------------

                Comment


                • #9
                  To use QuickPack, use $INCLUDE "QPakPro.inc" and make sure that the libraries PRO.PBL, PRO1.PBL and PRO2.PBL are accessible to the compiler, i.e. include the path where they reside in the Link Directorioes option of the compiler.

                  Sorry if the code of the PBU I posted is in Spanish, but it was intended only to show you how to declare the functions. The code of the functions themselves is irrelevant.


                  ------------------

                  Comment

                  Working...
                  X