Announcement

Collapse
No announcement yet.

Variables

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

  • Variables

    Please add your comments and suggestions as a Reply to this thread.





    PB/WIN - Variables
    represent numeric or string values. Unlike constants, the value of a variable can change during program execution. Like labels, variable names must begin with a letter and can contain up to 255 letters and digits (although in practical terms you really cannot exceed the length of a line). Be generous in naming important variables. In PowerBASIC, long variable names do not steal run-time memory.

    The Single-precision variables, EndOfMonthTotals and emt, both require exactly four bytes of run-time storage. A good rule of thumb is to preserve a balance, keeping variable names short enough so that statements can fit on one line. Many programmers use single-letter variables for loop counters (i, j, k, l and x, y, z are favorites). However, you can use names like count, total, index, and so on for greater clarity, especially if you have nested loops.

    PowerBASIC has many built-in variable types: Dynamic string; Fixed-length string; nul-terminated string; Field, Integer; Long integer; Quad integer; Byte, Word; Double word; Single; Double; and Extended floating point; Currency and CurrencyX; Variant, Object, Guid, plus Pointer, arrays, and Bit and Sbit bitfield subtypes.

    Declaring a variable as a specific type:
    Use the DIM statement to declare a variable and use the AS type syntax:

    Code:
    DIM iVar AS INTEGER
    Appending a type-specifier to the variable name:
    Code:
    bat# = 1.312 ' bat# is a Double-precision variable
    hat% = 3     ' hat% is an Integer variable
    DEFINT c     '  beginning with c are now Integer
    cats = 16    ' cats is an Integer variable by DEFINT
    Bear in mind that cat?, cat%, cat&, cat&&, cat!, cat#, cat##, [email protected], [email protected]@, and cat$ are ten separate variables. Although using cat over and over again to create different variables like this is legal, good programming practice suggests that you use somewhat different names for different variables. It is also much better to use descriptive and more easily understood names for your variables rather than single letters. It's extremely difficult to debug a program in which [email protected] has been entered instead of x! or x#. Imagine the confusion of trying to distinguish x&& and x&. If you had used variable names like count!, result#, remain##, and company$, you would have had considerably less trouble keeping your variables (and their types) apart.
    See AlsoReferences
    Last edited by Gary Beene; 28 Oct 2014, 10:50 PM.

  • #2
    Suggested change

    Change 'count' to 'number' in the sentence "However, you can use names like count, total, index, and so on for greater clarity, especially if you have nested loops."

    Count is now a keyword, although not reserved, and highlights in the IDE and is a bit disconcerting even though it will compile. COUNT is used in several multi word statements, ie: LISTVIEW GET COUNT, COMBOBOX GET COUNT, HEADER GET COUNT, etc.
    Rod
    I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

    Comment


    • #3
      For completeness "cat$$" should be added.

      Comment


      • #4
        > cat$, cat%, cat& (...)

        Has anybody tried this with the latest compilers? Even with UNIQUE VAR OFF I get a compile-time error: "This name is already in use."
        "Not my circus, not my monkeys."

        Comment


        • #5
          > cat$, cat%, cat& (...)

          Has anybody tried this with the latest compilers? Even with UNIQUE VAR OFF I get a compile-time error: "This name is already in use."
          Compiles here if I remove #DIM ALL (including the suggested cat$$).
          Rod
          I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

          Comment


          • #6
            I think that removing the dim all is not a smart decision!
            "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

            Comment


            • #7
              Ah, right, I forgot that my templates all use #DIM ALL. Still, the docs should note the interaction, and link to #DIM ALL.
              "Not my circus, not my monkeys."

              Comment


              • #8
                Ah, right, I forgot that my templates all use #DIM ALL.
                By working as well as it does, it makes us forget about it!

                Yes, a link to the #DIM metastatement should be included in that paragraph or immediately after. That page provides the needed info.

                If, after a few days there are no more suggestions, I'll post a 'Suggested Variables' page noting these things.
                Rod
                I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                Comment


                • #9
                  Yes, a link to the #DIM metastatement should be included in that paragraph or immediately after.
                  I'd say to note the effect #DIM {ALL|NONE} directive has regarding cat% Cat& Cat$ etc right in that paragraph or in a paragraph PRE-ceding the "cats"

                  **AND ***

                  Make sure to include #DIM on the "See Also" line of links.



                  MCM
                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Yes, good point(s)!
                    Rod
                    I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                    Comment

                    Working...
                    X