V-5 Using Variables and Arrays

Rectangular Callout: In my opinion, this is one of the most important subjects on VBN [variables and arrays]  Dave Bohm

 

Rounded Rectangular Callout: Specifying a Data TYPE is a very good idea, but VBN will work without thisRectangular Callout: You Declare a variables name

 

Rectangular Callout: String types of variables use alphabet characters, letters and words, not numbers

 

 

Rectangular Callout: Opening Visual Studio\VB Net

Starting Visual Studio.NET

Rectangular Callout: Name and Age LABELSRounded Rectangular Callout: Garth built this UI User Interface

Instructor Garth Schulte pre-set this interface up, but didn’t put any code behind it – yet.

Rounded Rectangular Callout: He opened the Properties window by ViewŕProperties Window [F4] key

 

Rounded Rectangular Callout: …and that the Properties window shows the properties for that field on the rightRounded Rectangular Callout: He clicked on The First Name field on the left

 

Rectangular Callout: …and that the Properties window shows the properties for that field on the rightRectangular Callout: He clicked on The Last Name field on the left

 

Rounded Rectangular Callout: …and that the Properties window shows the properties for that field on the right

Rounded Rectangular Callout: He clicked on The Age field on the left

 

Rounded Rectangular Callout: Same as above for the Show Name button. 

It will do all the work in this program as you’ll see. 

Also notice the shaded border around this button. This mean that it is the “accept” button [some people call it the “focus” button.

This just means that if you hit the Enter button when this program is run, it is the same as if you clicked on this button.

He dbl clicked on the Show Name button to get to the spot in the code window below:

 

 

Rectangular Callout: You declare variables by using the dim statement.

 

Rounded Rectangular Callout: He gives the variable name he is about to type in the prefix “str” since it will be a string variable. 

This is one that uses letters and words, not numbers.

 

Rounded Rectangular Callout: He names the first variable “FirstName”. Technically this is all that is required to name a variable, but if he stops here, this variable will be viewed as an “object” type variable by VBN. This is not the best choice as it uses too many resources and will slow down the program when it runs. 
So…

 

Rounded Rectangular Callout: …to prevent this, he will give the variable a “type” by typing in the keyword “as”.
Then the Intellisense feature of VBN will open the drop down box you see here.

 

Rounded Rectangular Callout: Since he wants this to be a string variable, he starts typing in the word string and Intellisense goes to the keyword string.

 

Rectangular Callout: Now the variable FirstName is both declared and typed. I like to use the word typecast to avoid confusion since that’s really what is meant here. [We don’t mean typed as in typewritten.]

 

Rectangular Callout: After he did the LastName as another string variable, he made a numerical variable called an integer variable and gave it the prefix int.

 

Rectangular Callout: Lastly he made a Name variable which as we will see will be a combination of the FirstName and the LastName. 

I think he should’ve called it strFullName for clarity.

The way he will combine these later is via a combination method called concatenation.

 

Rectangular Callout: Before we get to explaining concatenation we need to explain this. This statement will make the program take whatever the user types in the First Name text box, and store that information into the FirstName variable

 

Rounded Rectangular Callout: Same thing is true for the strLastName variable and the intAge variable. In effect we are allowing the user to place data into these variables.

 

Rectangular Callout: The way he does concatenation, is by using 2 Ampersands which is the name for the “&” symbol. Sometimes this is called the AND symbol.Rounded Rectangular Callout: This last statement is setting the full Name variable to be a combination of the First and Last names. This is concatenation.

 

Rounded Rectangular Callout: He’s also doing a dbl quote symbol “ followed by a space and another “. This will put a space between the First and Last named when it is later displayed.

 

Rectangular Callout: This statement will take the data that the user enters into the age text box and will add 5 years to whatever age they put in.

 

Rounded Rectangular Callout: So, in the above slide, the code line that reads:
                              intAge = txtAge.Text
will take the data that the user enters into the Age Text Box and make that the value stored in the Age Variable.
Rounded Rectangular Callout: In the above slide, the Age variable is called intAge in code.Rounded Rectangular Callout: In the above slide, it is called
intAge.Text in code.
Rounded Rectangular Callout: Its name in Properties is txtAgeRounded Rectangular Callout: To make things clearer:Rounded Rectangular Callout: This is the Age Text Box.

 

Rounded Rectangular Callout: Remember that the statement:

Dim strFirstName As String 

COULD HAVE been written as merely:

Dim FirstName

And it still would’ve correctly declared the FirstName variable. [Same for the other variables.]

However Dim strFirstName As String 
is the far better way to do it. Who can tell me the 2 reasons why? [Test Question]

 

Dim strFirstName As String

 

COULD HAVE been written as merely:

 

Dim FirstName

 

And it still would’ve correctly declared the FirstName variable. [Same for the other variables.]

 

However Dim strFirstName As String

 

is the far better way to do it.

 

Answer 1 in Dim strFirstName As String

The “str” prefix makes it easier for people reading the code to see that it’s a string variable.

 

Answer 2 in Dim strFirstName As String

 

The As String correctly type-casts [or types] the variable as a string variable. This makes it easier for VB.NET reading the code to see that it’s a string variable.

 

If the variable is not typed [type-cast] what will VBN see this variable as?

 

What type of variable? [Test Question]

 

Why is this not good? [Test Question]

 

 

 

 

 

 

Answers:

 

 

 

If the variable is not typed [type-cast] what will VBN see this variable as?

 

What type of variable? [Test Question]  

 

An Object type variable.

 

Why is this not good? [Test Question]

 

An Object type variable uses a lot more memory space than a string type variable. The program will run slower.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Rectangular Callout: These lines of code manipulate the data by in this case combining some and adding 5 to another.Rectangular Callout: These lines of code allow the variables to get specific values; the values that the user types into the appropriate Text Box for each of these variables.Rectangular Callout: These lines of code declare the variables and “type” them

 

Rectangular Callout: These lines of code display the values in the Labels that the user typed into the Text Box 

[note replay video section 23:23 to 24:17.]

 

Rectangular Callout: Intro to Arrays, which are simply Variables with more than one element which are the same data type.
E.G. all string or all integer, can’t mix and match data types. [Test Question]
Rounded Rectangular Callout: The reason he used (7) when he needed 8 elements is because arrays are _____” index based”, [Test Question]Rectangular Callout: Here Garth needed 8 elements so he declared the variable with a (7)

 

Rectangular Callout: Before we get into Arrays, right click on Array

 

Rectangular Callout: If you don’t, then what will happen when you later hit the run button?  Rectangular Callout: Then select “Set as StartUp Project”.

 

Rounded Rectangular Callout: Now Arrays will run when we are ready.

 

Rounded Rectangular Callout: Garth made is box with Radio Buttons that will demo an array.

 

Rounded Rectangular Callout: The label that will display the results is called lblElementNameRounded Rectangular Callout: In Properties it is called radbtn1Rounded Rectangular Callout: The 1st radio button, Element1

 

Rounded Rectangular Callout: He dbl clkd the Show Element Button which goes to this code window. Then he started to enter the 1st line of array code. Right now it looks just the same as an ordinary variable line of code.

 

Rounded Rectangular Callout: The only differences are, one he put an “a” in front of the str prefix so it is easy to see that its an array string. Next he put a 2 in parentheses (2) because he wants 3 elements in this array.

 

Rectangular Callout: Here Garth placed values [the names of Trainers] into the elements of the array.

 

Rounded Rectangular Callout: Back to the form. If the user checks radio button Element 1 then the <Name> that will be displayed is the name associated with the first element, which is element 0 Dan Charbonneau.

 

Rectangular Callout: … then the Label…

Rounded Rectangular Callout: It basically says that if Radio Button 1 [radbtn1] is checked…Rectangular Callout: Garth made an error, so the IntelliSense feature of VBN let him know by putting a squiggly blue underline where the error occurred.Oval Callout: …that is associated with Trainer ( 0 ) will be displayed.Rounded Rectangular Callout: Here’s our 1st example of an If…Then statement.

 

Rounded Rectangular Callout: The ElseIf…Then code just says that if instead of the first button the 2nd, or 3rd, or 4th button is selected Then display the label associated with that element of the variable array.

 

Rounded Rectangular Callout: He ran the program and selected radio button 2…Oval Callout: …then his name displayed in the labelRectangular Callout: ..then hit the Show Element Value” button…

 

 

Rounded Rectangular Callout: The only problem is that the ReDim keyword will wipe out the values that he already entered for the first 3 trainers, unless…Rectangular Callout: He also changed to ( 3 ) to handle the 4th one.Rectangular Callout: Garth decided he wanted to add a 4th trainer, but the program is already written. So he used a ReDim keyword that resizes, or re-dimension the array to handle that.

Rectangular Callout: …unless he uses the Preserve keyword. That way he won’t loose the first 3 name values.

 

Rectangular Callout: Here he added Troy’s name, next…

 

Rectangular Callout: Gave it the Text name Element 4, Rectangular Callout: Next he copy and pasted the Element 3 button here.

 

Rectangular Callout: Then he scrolled up to the (Name) window and named it radbtn4, then back to the code window…

 

Rectangular Callout: then back to the code window he did the ElseIf for radbtn4 to display element trainer number ( 3 )

 

Rectangular Callout: A Multi-dimensional array adds another one or more dimensions to the array. In this case the 2nd dimension he added is another <Field>

 

Rectangular Callout: 38:58Rectangular Callout: The 2nd dimension 1) is the Trainer’s field, in this case “MCSE Trainer”Rectangular Callout: The 1st element (0, and its dimension 0) is still the Name field for DanRectangular Callout: The (4,  1) just means that there are still only 4 name fields, but a 2nd dimension [the 1) ].

 

Rectangular Callout: How it looks in the planning board

 

Rectangular Callout: Right click on the Project “Array” then select “Properties” to get the window below:

 

Rounded Rectangular Callout: Change the “Startup object:” from frmArrays to frmMultiArrays

 

Rectangular Callout: …and it showed the 2nd dimension (1) is field of training, MCSD TrainerRectangular Callout: When he ran it, it showed the first dimension (0) his name

 

Rectangular Callout: Option Explicit On is on by default, but just to prove a point, he typed it in at the beginning of the code. It will help catch code errors right away as we’ll see below…

 

Rounded Rectangular Callout: Option Explicit On will underline in blue if you try to set up a variable that you never declared

 

Rectangular Callout: The program wouldn’t run, and it would give you a Build Error notice, and if you double clicked on the error….

 

Rectangular Callout: …it would take you to the line of code that is causing the problem.