[VBS.NET] Tutorial Part 2 - Variables and Labels

Discussion in 'Mixed Languages' started by jlgager, Nov 30, 2010.

  1. jlgager

    jlgager MDL Developer

    Oct 30, 2009
    365
    3,230
    10
    #1 jlgager, Nov 30, 2010
    Last edited by a moderator: Apr 20, 2017
    [VBS.NET] Tutorial Part 2 - Variables and Labels ​


    In this series I will show you everything from the basics to the most advanced. If you would like me to cover a specific topic, just PM me or post below. If it is a topic I wasn’t planning on covering I will put a star next to its name so check back a lot for updates.

    This Tutorial is in a series. Here is the link for part 1.

    [VBS.NET] Tutorial Part 1 - Your First Program

    Variables – What are they?​


    With Visual Basic, and most programming languages, what you are doing is storing things in the computer's memory, and manipulating this store. If you want to add two numbers together, you put the numbers into storage areas and "tell" Visual Basic to add them up. But you can't do this without variables.
    So a variable is a storage area of the computer's memory. Think of it like this: a variable is an empty cardboard box. Now, imagine you have a very large room, and in this room you have a whole lot of empty cardboard boxes. Each empty cardboard box is a single variable. To add two numbers together, write the first number on a piece of paper and put the piece of paper into an empty box. Write the second number on a piece of paper and put this second piece of paper in a different cardboard box.

    Now, out of all your thousands of empty cardboard boxes two of them contain pieces of paper with numbers on them. To help you remember which of the thousands of boxes hold your numbers, put a sticky label on each of the two boxes. Write "number1" on the first sticky label, and "number2" on the second label.

    What have we just done? Well, we've created a large memory area (the room and the cardboard boxes), and we've set up two of the boxes to hold our numbers (two variables). We've also given each of these variables a name (the sticky labels) so that we can remember where they are.

    Now examine this:

    Code:
    Dim number1 As Integer
    Dim number2 As Integer
    number1 = 3
    number2 = 5
    That's code from Visual Basic Net. It's VB's way of setting up (or declaring) variables.

    Here's a breakdown of the variable Declaration:

    Dim
    Short for Dimension. It's a type of variable. You declare (or "tell" Visual Basic) that you are setting up a variable with this word. We'll meet other types of variables later, but for now just remember to start your variable declarations with Dim.

    number1
    This is the cardboard box and the sticky label all in one. This is a variable. In other words, our storage area. After the Dim word, Visual Basic is looking for the name of your variable. You can call your variable almost anything you like, but there are a few reserved words that VB won't allow. It's good practice to give your variables a name appropriate to what is going in the variable.

    As Integer
    We're telling Visual Basic that the variable is going to be a number (integer). Well meet alternatives to Integer later.

    Number1 = 3
    The equals sign is not actually an equals sign. The = sign means assign a value of. In other words, here is where you put something in your variable. We're telling Visual Basic to assign a value of 3 to the variable called number1. Think back to the piece of paper going into the cardboard box. Well, this is the programming equivalent of writing a value on a piece of paper

    Now that the explanation is over let’s start a new demo program.

    On the new window, you'll normally want the option selected: "Windows Forms Application", in the "Installed Templates/Visual Basic" folder. This option lets u create a program with a form window. If you choose the “Console Application” you will be designing a CMD program. We will only be covering the “Windows Forms Application” version right now so select that one if not already selected.
    Next you will need to name your program. You can name it whatever you want but for consistency we will be naming it: “Demo2”. Replace whatever name you choose with the “WindowsApplication1” text listed in the “Name:” textbox and click ok.

    Now click and drag the button from the menu and this will give you a box under the cursor. Drag this to the bottom left corner like in the picture below.
    The lines that show up are telling you that you have it lined up perfectly with the corner. Unclick and the button is formed.

    [​IMG]

    Now do the same with a label but in the top left. Do you notice that there is a new line. That one is showing that the label is lined up perfectly with the left side of the button. See picture below.

    [​IMG]

    Now you need to rename the button and label.

    Give the button the name: btnDemo and Give the label the name: lblDemo
    Change the button text to “Demo!” and Change the label text to “Number will appear here.”

    Now that we have the design done, double click on the button.

    Now copy this text:

    Code:
    Public Class Form1
        Private Sub btnDemo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo.Click
            Dim var1 As Integer
            Dim var2 As Integer
            var1 = 1
            var2 = 5
            lblDemo.Text = var1 + var2
        End Sub
    End Class
    Basically what this code does is add 1 and 5 together and displays the result as the text of the label.

    Here it is explained:

    We are declaring var1 and var2 as empty Integer values. Then we are assigning the number 1 to var1 and 5 to var2.

    Next we are calling up the label’s (lblDemo) text property and assigning the value of 1+5 which is 6.

    This is the very basics right now but I will soon be adding more to this Variables and Labels tutorial.

    In the next lesson, I will introduce you to text boxes.

    [VBS.NET] Tutorial Part 3 - Text Boxes

    [​IMG]
    Want to thank me?
    Then just hit the thanks button on this post!
    Also post any ideas for future topics and any typos please.
     
  2. jlgager

    jlgager MDL Developer

    Oct 30, 2009
    365
    3,230
    10
  3. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270