Popular Posts

Showing posts with label Visual Basic lesson2. Show all posts
Showing posts with label Visual Basic lesson2. Show all posts

Thursday, March 5, 2009

Working With Integers (Continue)

Working With Integers (Continue)
Answer:

0
20
Blah
30

Why is that?
Lets pass over the code line after line:

Dim Blah As Integer
A new Integer with the name Blah has been declared

Print Blah
Will print the Value that found in the Blah variable.
But there is nothing in the Blah variable!
The Blah variable has just been declared, and we
didn't put inside it any value yet.
The default value of any integer variable is 0.
When you write :
Dim Blah As Integer
It's like you've written:
Dim Blah As Integer
Blah = 0

So it will print the value that found right now
in the Blah variable - 0

Blah = 10
Now the Blah variable holds the number 10

Blah = 20
Now the Blah variable holds the number 20
What's happened to the 10 that was inside it?
It has been deleted!
A variable can holds only one value, and when
you put in it a value, the old value is being immediately deleted.

So what is the differents between

Blah = 20

and

Blah = 10
Blah = 20

?

There is no differents!
In both cases the Blah variable will hold the number 20

Print Blah
Will print the value that found right now
in the Blah variable - 20


Blah = 30
Now the Blah variable holds the number 30


Print "Blah"
Will print the Text that found between the quotes - Blah


Print Blah
Will print the value that found right now
in the Blah variable - 30

Working With Integers (Continue)

Working With Integers (Continue)
Question: What will be printed on the form
after executing the following code:

Dim Blah As Integer
Print Blah
Blah = 10
Blah = 20
Print Blah
Blah = 30
Print "Blah"
Print Blah


After you've thinking about the answer, you can
check it by inserting the code above into a Command Button's
Click event, and press the button at run-time.

Anyway, the Answer can be found on the next page...

Working With Integers

Working With Integers
The process of creating variable called "Declaring"
To Declare (=create) Integer variable simply write:

Dim MyTest As Integer


What does the line above do?
It creates an Integer variable with the name MyTest.

Dim = Declare
MyTest = the name of the new variable
As Integer = The new variable type will be Integer.

Now you can put a number inside this variable.
You can do that by simple write:

MyTest = 10

The line above will insert the number 10 into the MyTest variable.
Now the MyTest variable stores the Number 10, but how
can you access this value from your program?
You can do that using the variable name. Example:

Print MyTest

The line above will write 10 on the form.
Pay attention to the differents between

Print MyTest

and

Print "MyTest"

Where you putting a text inside quotes (Like in the bottom line),
Visual Basic treat it as a Text, and will print it As-Is.
The Print "MyTest" Line will print MyTest on the form.

Where you putting a text Without quotes (Like in the upper line),
Visual Basic treat it as a variable name, and will print the value that found
in the variable.
The Print MyTest Line will print the value that found in
the MyTest variable, therefore it will print 10 on the form.

Learning about Variables

Learning about Variables
Using Variables is one of the most basic and important subjects
in programming, so this lesson is very important.

Variables are destined to save data.
You can save in variable Text or number.
For example, you can have one variable that holds the Text "Hello and Goodbye",
and have another variable that holds the number 623882

You can think about variables as a cabinet's drawers.
Every drawer has a sticker on it with its unique name.
You can put in each one of the drawers one number or one text String.
During your program, you can open the drawer with the sticker "TheUserName"
and get the text string that found inside the drawer.
You can also delete the text string that found inside the drawer
and put instead of it other text string.


Right now, we are going learn about 2 variable types.
The first type called Integer.
Integer variable can store an Integer number (round number without any fraction)
between -32,768 to 32,767.

You can store in Integer variable the number 0 or the number 375 or
the number -46, but you can't store the number 4.5 (Because of the .5)
or the number 100,000 (Because It's bigger than 32767) or the
number -50,042 (Because it's smaller than -32,768)


The second type called String.
You can store in String variable any text that you want.
For example "Hello" or "abcDDefGhIjk123456 blah blah %$#@!??? Blah!"