15
Visual Programming Visual Programming Variables in Variables in Visual Basic Visual Basic

Visual Programming 2

  • Upload
    kannan

  • View
    108

  • Download
    0

Embed Size (px)

DESCRIPTION

Learning about VariablesWorking With IntegersComplicated ExpressionsMore Variable TypesWorking With StringsScope of Variables

Citation preview

Page 1: Visual Programming 2

Visual ProgrammingVisual Programming

Variables in Visual BasicVariables in Visual Basic

Page 2: Visual Programming 2

Learning about VariablesWorking With IntegersComplicated ExpressionsMore Variable TypesWorking With StringsScope of Variables

ObjectivesObjectives

Page 3: Visual Programming 2

Learning about VariablesLearning about Variables

Using Variables is one of the most basic and important subjects in programming.

Variables are destined to save data. For example, you can have one variable that holds the Text

"Hello and Goodbye", and have another variable that holds the number 623882

• Right now, we are going learn about 2 variable types. The first type called Integer and second type called string.

Page 4: Visual Programming 2

Learning about Variables- Learning about Variables- IntegersIntegers• 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).

• Examples

X = 0X = - 46X = 375× X = 4.5 (Because of .5)× X = 100000 (Because It's bigger than 32767)

Page 5: Visual Programming 2

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

Dim MyTest As Integer

• 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

? How can you access this value from your program?

Page 6: Visual Programming 2

How can you access the variable value from your program?

• You can do that using the variable name. Example:Print MyTestThe 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 Print "MyTest"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 Print MyTest Visual Basic treat it as a variable name, and will print the value that foundin the variable.The Print MyTest Line will print the value that found inthe MyTest variable, therefore it will print 10 on the form

Working With Integers

Page 7: Visual Programming 2

Working With Integers - Continued

• 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

A new Integer with the name Blah has been declared

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.

Blah = 10 Now the Blah variable holds the number 10Blah = 20 Now the Blah variable holds the number 20What's happened to the 10 that was inside it? It has been deleted!A variable can holds only one value, and when

print the value that found right nowin the Blah variable 20

Assign the value 30 to Blah variablePrint the text “Blah” since inside the quotesPrint the value of 30

Page 8: Visual Programming 2

Working With Integers - ContinuedQuestion: What will be printed on the form

after executing the following code:• Dim Blah As Integer• Blah = 2• Print 2 + 3• Print "2 + 3"• Print Blah + 3• Print Blah• Blah = Blah + 1• Print Blah• Blah = Blah + Blah• Print Blah

Answers5

2 + 35236

Page 9: Visual Programming 2

Complicated expressionsYou can use the following operators in expressions:• + Plus• - Minus• * Multiply• / Division• ( Opening parenthesis• ) Closing parenthesis• ^ Power

• The order of the operators are like in math:

First "(" and ")" , then "^", then "*" and "/", and at last "+" and "-"• For example:

2 + 3 * 4 is equal to 14(2 + 3) * 4 is equal to 20

Page 10: Visual Programming 2

More Variable TypesThe Variable types list:

• Byte - Can store only Integer numbers between 0 to 255

• Integer - Can store only Integer numbers between -32,768 to 32,767• Long - Can store only Integer numbers between -2,147,483,648 to

2,147,483,647• Single - Can store Non-Integer (numbers with fractions) Numbers

between -3.402823E38 to -1.401298E-45 for negative values, and 1.401298E-45 to 3.402823E38 for positive values.

• Double - Can store Non-Integer Numbers between -1.79769313486231E308 to -4.94065645841247E-324 for negative values, and 4.94065645841247E-324 to 1.79769313486232E308 for positive values

The Declaring command is very simple:Dim Abc As DoubleDim Blah As SingleDim Popeye As ByteAnd so on.

Page 11: Visual Programming 2

Working With Strings• String variables are meant to store Text.• When you assign a text to a String

variable, you must put the text inside quotes.

• For example:

Dim Abc As String

Abc = "Good Morning“

Question: What will be printed on the form after executing the following code?

Dim res As Stringres = "Hello!!!"Print "res"Print res

Answer:res

Hello!!!

Page 12: Visual Programming 2

Learning about Variables- Learning about Variables- StringsStrings

• You can store in String variable any text that you want.

• For example "Hello" or "abcDDefGhIjk123456 blah blah %$#@!??? Blah!“

• ExamplesX = “Hello”X = “abcDDefGhIjk123456 blah blah %$#@!??? Blah! ”

Page 13: Visual Programming 2

Working With Strings (Continue)

• You can join two Strings together using the "+" operator.• Example:

Dim gogo As StringDim result As Stringgogo = "Hello"result = "world"gogo = gogo + " !!! " + resultPrint gogo

The code below will print Hello !!! world on the form. The expression gogo + " !!! " + result is equal to Hello + " !!! " + world and that's equal to "Hello !!! world“ So eventually, the command that will be executed is gogo = "Hello !!! world“

Page 14: Visual Programming 2

Scope of the variablesScope of the variablesInsert two Command Buttons to yourForm (with the Names Command1and Command2),and Add thefollowing code to your program:

Private Sub Command1_Click()Dim gogo As Integergogo = 100End Sub

Private Sub Command2_Click()MsgBox gogoEnd Sub

Insert two Command Buttons to yourForm (with the Names Command1and Command2),and Add thefollowing code to your program:

Dim gogo As Integer

Private Sub Command1_Click()gogo = 100End Sub

Private Sub Command2_Click()MsgBox gogoEnd Sub

Page 15: Visual Programming 2

Choosing A Valid Name For Variable

The Rules1) The only characters that can appear in the name are letters,

numbers and the character _ (the underscore character) You can't use other characters like: ` ! @ # % ^ & * ( ) Therefore, the following names are NOT valid:

AB!, BB$, tot#o2) The Name Must begin with a letter: You can't use the names:

2abc, 3, 2345, _aba

3) You can't use space in the variable's name. Therefore, the following names are NOT valid: A B, hello world