In this article, am going to discuss with you visual basic data types as well as variables declaration.
Computer is not a natural human being. It was created by humans and as such only understands what you asked it to do. If you asked wrongly, it will give you wrongly vise versa.
In other words, computer can only understand your inputs if only you specify the kind of data they are and what you are expecting (data types). So as a computer programmer, you should know what data types is all about and when to use them. Simply put, data types determine the kind of values to be hold by a Variable.
Data types in programming refers to type that specifies the type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error. For instance, integer is a data type used to specify whole numbers.
There are various data types in Visual Basic and they are;
The storage space allocated depends on the platform of implementation. Values range from 0 to about 2 billion Unicode characters.
It is mostly used for expression that whose value would either be true or false.
The allocated storage depends on the platform of implementation. Its value can be either True or False.
Allocated storage space of 1 byte. Values range from 0 to 255 (unsigned).
Allocated a space of 2 bytes. Values range from 0 to 65535 (unsigned).
It is used for holding Date and Time values.
Allocated storage space of 8 bytes. Values range from 0:00:00 (midnight) January 1, 0001 to 11:59:59 PM of December 31, 9999.
It has a storage space of 4 bytes. Values range between -2,147,483,648 to 2,147,483,647 (signed).
It has a storage space of 8 bytes. Numbers range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807(signed).
There are functions that we can use to convert from one data type to another. They include:
-
CBool (expression): converts the expression to a Boolean data type.
- CDate(expression): converts the expression to a Date data type.
- CDbl(expression): converts the expression to a Double data type.
- CByte (expression): converts the expression to a byte data type.
- CChar(expression): converts the expression to a Char data type.
- CLng(expression): converts the expression to a Long data type.
- CDec(expression): converts the expression to a Decimal data type.
- CInt(expression): converts the expression to an Integer data type.
- CObj(expression): converts the expression to an Object data type.
- CStr(expression): converts the expression to a String data type.
- CSByte(expression): converts the expression to a Byte data type.
- CShort(expression): converts the expression to a Short data type.
Variable declaration simply means giving a name to the variable and defining the data type to which it belongs. It has the following syntax:
Dim VariableName As Data Type
In the above syntax, VariableName is the variable name while Data Type is the type of data that the variable will hold.
For example,
Dim pName As String
The above declared variable will be used to hold personal name. In the example given, pName is the Variable Name while String is the Data Type.
However, when a Variable is being declared does not mean that it has been initialized and this take us to Variable Initialization
Variable Initialization simply means assigning a value to a declared variable. Variable initialization can also be done while declaring the variable. Below I have demonstrated both cases.
In the first example, I created a subroutine to calculate the addition of two numbers
Sub AddNum()
' Variable declaration
Dim num1 As Integer
Dim num2 As Integer
Dim result As Double
'Variable initialization
num1 = 3
num2 = 5
'Data processing
result = num1 + num2
MsgBox (result)
End Sub
However, the above codes can be rewritten in such a way that the variables will be declared and initialized at the same time as given below
Sub AddNum()
' Variable declaration and I cnitialization
Dim num1 As Integer = 3
Dim num2 As Integer =5
Dim result As Double
'Data processing
result = num1 + num2
MsgBox (result)
End Sub
In Visual Basic.Net, expressions are of two types;
Lvalue: Lvalue expression may appear on the left-hand or on the right-hand side of the assignment operator.
Rvalue: Rvalue expression can only appear on the right-hand of the assignment operator but not on the left-hand side.
Variables are Lvalues, meaning that we can put them on the left side of the assignment operator. For example:
Dim num As Integer = 2
For numeric literals, they can neither be assigned nor can they appear on the left-hand side of the assignment operators since they are Rvalues. For example:
2 = num
The above expression is wrong and will generate a compile-time error.