What is a variable?
A variable is something that is used in a program to store data in memory. Variables are used for storing the data that a program is busy working with. Variables can store all sorts of different types of data including things like numbers and text. Variables are important because it is almost impossible to write a useful program that doesn't use them.Declaring a variable
Here is an example of how to declare a variable followed by an explanation.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyInt As Integer
End Sub
End Class
The variable is declared using the Dim keyword followed by the name of the variable which in this case is MyInt. After that is the As keyword followed by the data type of the variable which is an Integer in this example. An Integer is a variable that stores numbers. There are different types of variables for storing different types of data which you will learn about later.
Setting the value of a variable
You set the value of a variable using an equals sign (=). Here is an example of how to set the value of a variable.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyInt As Integer
MyInt = 5
End Sub
End Class
In the above example the variable called MyInt has been set to the value of 5.
The Console.WriteLine command can be used to display the value of a variable. You must not use double quotes with Console.WriteLine when using a variable like you had to when printing words on the screen because then the compiler thinks you want to print the name of the variable instead of the value it holds. Here is how you print the value of a variable.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyInt As Integer
MyInt = 5
Console.WriteLine(MyInt)
End Sub
End Class
You can set the value of a variable at the same time as you declare it which is called initializing a variable. Here is an example of how to initialize a variable.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyInt As Integer = 7
End Sub
End Class
You can declare more than one variable by adding another variable name to the same declaration in which case you must separate the variable names using a comma. Another way of doing it is to just declare another variable on a new line which a lot of programmers prefer doing because then the variables can be initialized easily.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyInt1, MyInt2 As Integer
Dim MyInt3 As Integer
End Sub
End Class
Calculations using variables
You can perform calculations on variables using one of the 4 basic operations which are addition, subtraction, multiplication and division. The result of an operation must be stored in a variable otherwise it is lost. Here is an example in which the numbers 2 and 3 are added together and the result is stored in a variable called MyInt which is printed on the screen.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyInt As Integer
MyInt = 2 + 3
Console.WriteLine(MyInt)
End Sub
End Class
You can see from the example above that the plus sign (+) is used for adding numbers. Here are the signs for the 4 operations.
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide |
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyInt1 As Integer = 10
Dim MyInt2 As Integer = 8
Dim Result As Integer
Result = MyInt1 - MyInt2
Console.WriteLine(Result)
End Sub
End Class
Data types
So far we have only been using the Integer data type which can store only numbers. If you want to store other types of data then you need to use a different data type.The String data type is used for storing text. When you set the value of a string to some text you must surround it with double quotes because this tells the compiler that the text between the quotes is some text and not part of the code that the program must run. Here is an example of how to declare a string called MyString and set its value.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyString As String
MyString = "Some text"
Console.WriteLine(MyString)
End Sub
End Class
The Char data type is similar to a String except that you can only store 1 character in it. Most of the time you will use Strings instead of Chars but it is still important to understand how Chars work. Here is an example of how to declare a Char variable called MyChar and set its value to the letter V.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyChar As Char
MyChar = "V"
Console.WriteLine(MyChar)
End Sub
End Class
Integers can only store whole numbers and can't store numbers with a decimal point. To be able to work with numbers with a decimal point you must use the Decimal data type. Here is an example in which the number 10 is divided by 4 which gives a result that has a decimal point and therefore needs to be stored in the Decimal data type.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyDec As Decimal
MyDec = 10 / 4
Console.WriteLine(MyDec)
End Sub
End Class
The Boolean data type is used for storing either the value True or the value False and nothing else. It doesn't seem very useful but you will find later that it actually is. Understanding how Booleans work is very important for helping you understand how certain other things in programming work which you will come across later. Here is an example of how to declare a Boolean variable called MyBool and set its value to True and to False.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Dim MyBool As Boolean
MyBool = True
Console.WriteLine(MyBool)
MyBool = False
Console.WriteLine(MyBool)
End Sub
End Class
There are many more data types but the ones you have learnt so far cover the most common things you will come across in VB.NET programming. Your knowledge of the data types you have learnt will be enough to help you figure out how to use any of the other data types.
Storing user input
If you want to get the user to type something in and use it in your program then you can do that by using the Console.ReadLine command which waits for the user to type some text and press Enter. When the user has pressed Enter the text the user entered is returned by Console.ReadLine so you have to store the value returned by the command in a variable. Here is an example in which the user is asked to type in some text and then the text the user entered is printed out again.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Console.WriteLine("Please enter something:")
Dim EnteredText As String
EnteredText = Console.ReadLine()
Console.WriteLine("You entered:")
Console.WriteLine(EnteredText)
End Sub
End Class
Type conversions
You will often have the need to convert variables to other data types. For example the Console.ReadLine command only reads in Strings so if you want the user to enter a number you have to convert the String that was read in to an Integer. There are quite a few different ways of converting between data types but you will only learn a few of them right now.The first way of converting between data types is using a group of commands that start with the letter C. There is a command called CInt for example which is used to convert other data types such as Strings to Integers. Here is an example of how to use it in which the user is asked to enter a value which is stored in a String and then converted to an Integer using CInt and then shown again to the user.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Console.WriteLine("Please enter a number:")
Dim EnteredText As String
EnteredText = Console.ReadLine()
Dim MyInt As Integer
MyInt = CInt(EnteredText)
Console.WriteLine("The converted value is:")
Console.WriteLine(MyInt)
End Sub
End Class
Here are the most useful C conversion commands:
CInt (Convert to Integer)
CStr (Convert to String)
CChar (Convert to Char)
CDec (Convert to Decimal)
CBool (Convert to Boolean)
Another way to convert between types is to use the commands that begin with Convert.To . The .To is followed by the data type such as Convert.ToString. The command for converting to an Integer in this way is Convert.ToInt32. The advantage of the Convert commands over the C commands is that they are also used in programming languages such as C# which you might want to use one day. Here is an example that is the same as the previous one but using Convert.ToInt32 instead of CInt.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Console.WriteLine("Please enter a number:")
Dim EnteredText As String
EnteredText = Console.ReadLine()
Dim MyInt As Integer
MyInt = Convert.ToInt32(EnteredText)
Console.WriteLine("The converted value is:")
Console.WriteLine(MyInt)
End Sub
End Class
Here are the most useful Convert commands:
Convert.ToInt32 (Convert to Integer)
Convert.ToString (Convert to String)
Convert.ToChar (Convert to Char)
Convert.ToDecimal (Convert to Decimal)
Convert.ToBoolean (Convert to Boolean)
The CType command can be used for when the data type you want to convert to doesn't have either one of the above conversion methods because it can convert to any type. CType takes 2 parameters which are the value to be converted and then the name of the data type to convert to. Here is the same example from above again but this time it uses CType.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Console.WriteLine("Please enter a number:")
Dim EnteredText As String
EnteredText = Console.ReadLine()
Dim MyInt As Integer
MyInt = CType(EnteredText, Integer)
Console.WriteLine("The converted value is:")
Console.WriteLine(MyInt)
End Sub
End Class
Constants
A constant is a variable whose value can't be changed. You use a constant when you specifically don't want the value of a variable to change during the execution of your program. Constants can be used for things like the company name of the company which wrote the program because the company name must not change anywhere. All you have to do to declare a constant is change the Dim keyword in the normal variable declaration to Const. The value of the constant must be initialized on the same line as it is declared otherwise you will get a compiler error. Here is an example of how to declare a constant for a company name.
Imports Microsoft.VisualBasic
Imports System
Class Variables
Shared Sub Main()
Const CompanyName As String = "ABC Company"
Console.WriteLine(CompanyName)
End Sub
End Class
0 comments:
Post a Comment