|
In
programming, one of the most important concepts you must know about is called
variables. Don't let that term spook you out. Just because you don't know
Algebra doesn't mean you aren't going to know variables. In fact, programming
with variables is totally different than Algebra!
Types
of Variables
There
are various types of variables they include:
Integers-Real
numbers, negative or positive. Does not include decimals.
Single- A single is an number that includes decimals.
Double - A double is a number that includes decimals that have more digits
(to be more precise)
String - A block of text.
How
to declare (create) variables
and
use them properly
In
order to create variables, you must use a Dim statement, such as Dim
X As Integer
. This
statement must follow the
Inherits
System.Windows.Forms.Form statement.
The declarations statement must also precede any code, if you want it to be
accessible anywhere in your program. You
are creating a variable called X that can store Integers. Later on in your
code, you can say, X = 3, which would store the value of 3 into X.
You can
also say X = X + 1. In Algebra, this statement wouldn't make sense at all. A number called X cant
be the same itself, plus 1. In programming, however the value on
the right hand side is stored into the variable
on the left hand side. Let's consider these 2 statements.
Dim
X
As Integer
'You are creating an
Integer called X
X
= 3 'The
variable X has the value of 3 now
X = X + 1 'X
is now 4
Remember,
the value on the right is stored into the value on the left. So if X = 3, then
if you say
X = X + 1 ,then
X would be 4.
This concept needs to sink in your mind before we can move any further.
Our Program
Create
a new project called Variables (if you don't know how, refer to the Hello World
tutorial). Add a button named MyButton and set the text property to,
"Click Me." Go to your code. After Inherits
System.Windows.Forms.Form
type in:
Dim
MyVariable
As Integer
'You
are creating 3 variables
Dim
MyVariable2 As Integer
Dim
MyVariable3 As Integer
In
MyButton.Click type in:
MyVariable
= InputBox("Type in your 1st Number")
'An InputBox is a MessageBox that
has a text area to enter info.
MyVariable2
= InputBox("Type in your 2nd Number")
'In these cases, you are storing
your "info" into MyVariable[2]
MyVariable3
= MyVariable + MyVariable2
'You are storing the sum of 2
variables into MyVariable3.
MessageBox.Show(MyVariable3)
Run
your program. You will type in 2 numbers and a MessageBox appears showing the
sum of both numbers. The
first 3 lines of code create MyVariable, MyVariable2, and MyVariable3 and
makes them Integers. An InputBox is a MessageBox with a place to type in info to get some
results back. When you say MyVariable = InputBox("Type in your 1st
Number"), you are saying, MyVariable equals what the user types in for
their first number. Two lines later, the compiler
adds MyVariable and MyVariable2 and stores that number into MyVariable3. The
last line may seem different to you. Look again:
MessageBox.Show(MyVariable3)
This
line of code shows the value of MyVariable3. But, looking back at our Hello
World program, we saw,
MessageBox.Show("Hello
World!") 'From our Hello World program
Notice
the Quotation Marks in this example. Let's Change our code from,
MessageBox.Show(MyVariable3)
'Notice there are no quotes
to
MessageBox.Show("MyVariable3")
'Notice the quotation marks
When
you run your program, you will get a MessageBox showing,
"MyVariable3", you wont see the sum of both numbers.
This
is because when the MessageBox.Show statements has quotation marks, it shows
the text that is in between the quotes. But, when it doesn't have quotes, it
shows the value of the variable. If you didn't declare a variable, and
displayed it in a MessageBox, it would display an error.
Remember to declare variables before you use them. Nevertheless, change the
code back to how it originally was (without the quotes) You have just
learned how to use variables!
The Source Code
for this tutorial is located here:
You can also
locate this by logging in to vbProgramming Forums and going to:
Tutorials > Tutorial Source Code > Source Code
|