VB.NET Tutorials 4 – Variables

In tutorial 3, we saw how to work with Text Property of Labels and forms. If you find font or size of labels is too small to read, you can alter it from the property box of label. Find Font, Size options there and change their values accordingly. It is very easy task to do and can be easily practiced. Thus, we will move towards very important part in any programming language; Variables.

But what are variables? Variable is empty storage area where you can store numbers, letters or strings for further use. To perform different evaluations on numbers or strings, we store each number or string in a variable. A variable can store a single number or string. For multiple numbers or strings, multiple variables can be used. After storing numbers in variables, we can add or subtract or multiply variables to perform respective action on those numbers. Let us first learn how to declare variables in VB and how to assign value to them. Consider number of lines written below:

Dim number1 As Integer

Din number2 As Integer

number1= 3

number2= 4

What we did? Well, you have just explored through your first VB.net program. Now let us break down the code to understand things better.

Dim stands for Dimension, a variable type. By writing Dim, we indicate VB that a variable is getting created. Thus whenever you start with declaration of variable, do not forget to write Dim. But remember, this word will change with change in type of variables. We will soon cover different types of variables.

number1/number2 are nothing but names of variables. We have allotted name to the empty storage space so that we can refer them while storing data into them. If you can see last two line of coding, we have allotted value “3” to number1 by using statement “number1=3”. Thus, we have allocated value 3 to the empty space marked with name “number1”. Similar explanation goes with number2.

Integer is type of variable. As we know both numbers and strings can be stored into variables. We are telling VB that our variables will hold value in the form of number by writing Integer data type at the end. More on different data types will be covered in next session.

But where to put this code? We need to attach this code to the button in order to perform any action. Let us now see how this can be done. Create new project containing Form and add Textbox and button to it as we have seen in Tutorial 2. Button must indicate what function it performs. Currently, out button is displaying “button” as text upon it. Change it to “Add Two Numbers” using text property. It can be done in similar way as that of labels, which we have learn in Tutorial 3.

Your form must look like this:

Form

Now double click on button while it is selected and a new windows will appear on screen. This window is know as Coding window. You must be seeing something like this:

Coding window

Don’t get too confused with the coding. Click on open space between “Private Sub” and “End Sub” so that cursor falls in there. But what is all this? Who wrote this part of code? We will learn about it in next tutorial. So stay tuned!

For any queries, feel free to write us. To get notified when next tutorial gets published, sign up for our news letter or follow us on Facebook.

About the author

Guest@TGC

This post has been submitted by a Guest Author. If you would like to submit a guest post, you may contact us on the mail ID mentioned in the About page.

1 Comment

Click here to post a comment