CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2001
    Location
    Accra,Ghana
    Posts
    4

    declearing variables

    I am finding it very difficult to declear variables .And i want to ask how to declear them


  2. #2
    Join Date
    Jun 2001
    Location
    Israel
    Posts
    228

    Re: declearing variables

    Examles:

    Dim n as Integer 'Declaring an integer variable
    Dim s as string 'a string variable
    Dim a(1 to 20) as Boolean 'an array of 20 boolean variables




    ----------
    The @host is everywhere!
    ----------

  3. #3
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: declearing variables

    When declarting variables, important is the scope. The scope is where the variable can be used. Here are some examples

    ' in the general declaration section of a form/module
    ' this is at the top of the module

    private myVariable as string ' myVariable will be accessible from withing the entire module
    Dim myVariable as string ' same as private
    public myVariable as string ' myVariable will be accessible from wothing the entire module, and from other modules

    ' withing a procedure
    private Sub Test()

    Dim myVariable as string ' myVariable will only be accessible withing this procedure, and will lose it's value if the procedure is left
    static myVariable as string ' myVariable will only be accessible withing this procedure, but will keep it's value if the procedure is left

    End Sub



    You cannot declare public variables inside a procedure, this must be done in the general declaration section.
    You cannot use the private statement inside a procedure, you must use Dim

    The second point of attention is the datatype. Each piece of data has a type. We can make three big devisions here. We can have strings, which can contain any alfanumeric value (eg "This is a test"). Values must be enclosed in "s. Second of all, we have numeric values, which we can use to do mathematical functions. Every type has it's limitations. Integer can only hold values from approx -32000 up to approx 32000. Byte can only hold positive numbers up to 256. You should check out MSDN for more exact issues on these, cause they are to big to list all. Finally, we have objects. Objects are thingsd that can contain other things than just a value. They can have properties, methods and a lot of other stuff. This is something where tons of books are written about, and will be written, because the future will go entirely to OO (object oriented) programming.
    Then we have a special case, Variants, this is the default data type, which can contain anything, going from string via number to user defined type to object, in other words, anything.

    Why not make them all variant? Cause variants are slow, they take up a lot of memory, and VB will need to convert them to another datatype aal the time. Same goes for numbers, if you only need like say a number up to 10000, use Integer instead of Long, integer will be faster.

    I hope this cleared some stuff out for you.

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured