Click to See Complete Forum and Search --> : declearing variables


fofrancis
October 4th, 2001, 03:21 PM
I am finding it very difficult to declear variables .And i want to ask how to declear them

deghost
October 4th, 2001, 04:38 PM
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!
----------

Cakkie
October 5th, 2001, 01:13 AM
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
slisse@planetinternet.be

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