|
learn
visual basic -- 4.1, variables and operators
here,
we are going into a fundamental step in programming -- variables.
variables determine how much memory, or RAM (random access
memory) your program needs from the computer. they are also
"containers" which hold information for the program,
be it input or output.
if
you followed my articles closely, try to remember this:
greeting
= txt1.txt
the
word greeting is a variable. it is not enclosed by quots.
it is simply a word which could, preferably, be identified
with what value it holds. for example, the varable greeting
here, will contain a string value (a sentence enclosed by
quots, see previous chapter), which is actually a greeting
for the Hello World program when the user clicks on the greet
button.
the
operator, =, assigns the value of txt1.text, which is the
value of the property text of the textbox, to the variable
greeting. so far so good. then, consider another situation:
answer
= 1 + 1
arithmathically,
answer equals to 2. and correct! answer is really 2. the variable,
answer, is assigned the value of 1 + 1, which is 2. the operator,
+, adds the values together. you can guess that:
-
for subtract
/ for divide
* for multiply
and
it is true for all integer values (numbers and decimals, both
negative and positive). note that integer values are not between
quots. if not, check this:
var1
= "1" + "1"
var2 = "1 + 1"
the
value of var1, a variable, is "11". this is a string.
the quots forced the program to read the numbers "1"
as a string and the operator + joins the two together forming
the string value "11".
for
var2, the value it is assigned is "1 + 1" as it
is. because the whole expression is in quots which means it
is a string. a string always appears as it is and is not affected
by operators.
however
consider these:
var3
= "1" - "1"
var4 = "1" / "1"
var3
is definitely not "0" and var4 is ridiculous. you
cannot take a sentence and divide it by another sentence.
your maths teacher didn't teach you this.
for
the plus sign (+) in var1, it acts as a concatenation operator,
joining two strings or more together. however a better or
rather professional way is to use the ampersand (&). you
can try this: below:
create
a new project (see preivous chapters if you don' know how).
then create a command button on the form. change the properties
to:
commandbutton1
name -- cmd1
caption -- click me
form1
name -- frm1
caption -- My VB program
double
click on the command button and type in the following code:
Private
Sub cmd1_Click()
var1 = "I am a number "
var2 = 1
var3 = "Visual Basic "
var4 = "programmer!"
message = var1 & var2 & var3 & var4
MsgBox message, , var2
End Sub
run
the program by clicking on the run sub/userform button on
the tool bar. click on "click me" and a message
box comes up shows the message "I am a Visual Basic programmer!"
and the title is "Visual Basic". notice how we used
variables in the MsgBox funtion. yes, a function. i'll talk
about functions in intermediate stages.
all
the variables we just discussed are not declared explicitly.
they are used, because you happen to need one only here in
the program, you probably did not plan to have it. but what
if you planned to have a variable that can only take some
values? you declare it by using the dim key word like this:
dim
variable
what
if you want it to take only a particular value?
dim
variable as type
the
as type keyword or phrase is used to define and restrict
the value of the variable. this would ensure that the value
type will not be changed by other procedures accidentally
and cause an error in the program. try this:
Private
Sub cmd1_Click()
Dim var1 As String
Dim var2 As Integer
Dim var3 As String, var4 As String
var1 = "I am a number "
var2 = 1
var3 = " Visual Basic "
var4 = "programmer!"
message = var1 & var2 & var3 & var4
MsgBox message, , var3
End Sub
notice
that you can declare, with the dim keyword, one variable on
each line or, a series of variables on one line separated
by commas. also not that even when the variables on a single
lines carries the same value type, you still have to enter
the as type keyword for every variable on that line
and not:
Dim
var3, var4 As String
in
this case, only var4 is of String type and var3 can contain
any type of information. using the Dim statement is advantages
as it allocates memory for you program to prevent memory shortages.
other types include:
As
Boolean
true or false. any value not 0 is true (except negative numbers)
and 0 is false.
As
Double
a decimal number.
As
Float
a decimal number also, but smaller than Double and requires
less memory than Double.
continue
next chapter
|