VB.NET Cheat Sheet By: Andrew Kiceniuk Unit 1: Computer Memory Computer Memory: Computers have two types of memory:

1. Permanent  ex: your hard drive  info stay on your computer even after the power is turned off.

2. Temporary  RAM (Random Access Memory)  this is the computers working memory, it allows programs to run, but all info saved in the RAM is lost when the computer’s power is turned off.

You can think of the computer’s RAM as being a collection of storage boxes. Sometimes your programs will need to save information to the computer’s RAM. That would be like putting the information into the storage box. Ex: 5

Sometimes your programs will need to use information from the computer’s RAM. That would be like taking information out of the storage box. Ex: 5

These storage boxes are called memory locations. When we give a memory location a unique name we call it a variable.

Working With Variables: Working with a memory location involves three steps: 1) Declaring a variable (i.e. naming the memory location) 2) Initializing the variable (i.e. putting a value in the memory location) 3) Using the variable a. To complete of a calculation or b. To output an answer to a label

Let us look at each step: 1) Declaring a variable – requires a statement that has three parts: intNumberOfFeet

Statement Format: Dim preVariableName As DataType Declaration Example: Dim intNumberOfFeet As Integer

Dim - is a keyword to indicate that you want to name a memory location. VariableName – is the name you are giving the memory location. Is should be descriptive of the value to be saved in the memory location. - Notice that the variable name starts with a 3 letter, lowercase, prefix that describes the data type that the variable will hold. Data Type – This tells the computer what type of values will be saved into the memory location. The basic data types we will work with are: VB.NET Cheat Sheet By: Andrew Kiceniuk Unit 1: Computer Memory Data Type Value Held 3 Letter Prefix String “Text” str Integer numbers without decimals int Double numbers with decimals dbl Boolean True, 1, False, 0 bln Char A single keyboard chr character VB.NET Cheat Sheet By: Andrew Kiceniuk Unit 1: Computer Memory

2) Initializing a variable You can save a value to a memory location by using an assignment statement (as in you are assigning a value to the variable.) intNumberOfFeet

Statement Format: preVariableName = value Assignment Example: intNumberOfFeet = 5

You specify the variable name followed by the assignment operator (i.e. =), then the value. The value must match the data type specified in the variable declaration.

3) Using the variable You use variables either to:

1) complete a calculation Example: dblAnswer = intNumberOfFeet * 2.50 Computer reads it as: dblAnswer = 5* 2.50

2) display in a label Example: Me.lblAnswer.Text = dblAnswer Label displays: 12.50

When Do You Use Variables? There are two situations: 1.) To complete calculations – the result of a math calculation should be saved in a variable. Example: dblAnswer = intNumberOfFeet * 2.50

2.) To accept user input – whenever you have a TextBox on your form you will need a variable to save the value the user enters into it.

How to handle text input from a TextBox:

Statement Format: preVariableName = Me.TextBoxName.Text Example: strHomeTown = Me.txtHomeTown.Text

How to handle numeric input from a TextBox:

Statement Format: preVariableName = Val(Me.TextBoxName.Text) Example: intAge = Val(Me.txtPetsAge.Text)

Note: Everything entered into a TextBox is actually text. For example: The user input in the Kraken Kafe program is not recognized as a number, but rather as text (i.e. “25”). VB.NET Cheat Sheet By: Andrew Kiceniuk Unit 1: Computer Memory

We need to use the Val( ) method to convert the text value (“25”) into a numeric value (25).