What are variables and how are they used in vb code.
Printable View
What are variables and how are they used in vb code.
A variable is a named bit of space put aside for holding a value.
Each variable has a name, a scope and a type.
Name - This is a unique name that the variable is known as in the program.
Type - This indicates what kind of data the space put aside can hold. For example, if you want to hold a number with no decimal portion you would probably put aside space for an Integer. If you wanted to store text you put aside space for a string.
Scope - This indicates where the space can be seen from. For example, if you have a module and you want to be able to access a variable in some code outside that module then you should scope it as public...if not you should scope it as private. Variables declared inside a function or subroutine are not visible outside of that routine.
HTH,
Duncan
-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
'a small example
'Variables: think at them as cups.
'You can put a thing in the cup to carry it.
'When you need, you can check what is in a cup,
'and you can add the content of a cup to the
'content of another cup...
'How to use them in Vb? First, start Visual Basic.
'You should have a form in front of you.
'Double click (with left mouse button) on the form.
'You should see a code section like the following:
private Sub Form_Load()
End Sub
'add this code between the "private Sub Form_Load()"
'and the "End Sub" statements:
Dim myFirstVar as Variant
Dim mySecondVar as string
Dim myThirdVar as Integer
'The above lines of code declares three variables
'the first is a variable which can contain any
'kind of data, the second can contain text data
'(which means: alphanumeric data)
'the third can contain numeric data only
' ranging between about -32000 to about +32000
'
'let us add this code to fill variables with values:
mySecondVar = " This string follows the numeric value"
myThirdVar = 10
myFirstVar = "here i could put what I want. This time a string! "
'call a standard function that pops up a window
'to show what is happened
MsgBox myFirstVar & myThirdVar & mySecondVar
'here is how code should look:
private Sub Form_Load()
'declare 3 variables
Dim myFirstVar as Variant
Dim mySecondVar as string
Dim myThirdVar as Integer
'assign values to variables
mySecondVar = " This string follows the numeric value"
myThirdVar = 10
myFirstVar = "here i could put what I want. This time a string! "
'show result in a window
MsgBox myFirstVar & myThirdVar & mySecondVar
End Sub
'run the code (select menu run->Start)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater