Visual Basic data types
In this part of the Visual Basic tutorial, we will talk about data types.Computer programs work with data. Spreadsheets, text editors, calculators or chat clients. Tools to work with various data types are essential part of a modern computer language. According to the wikipedia definition, a
data type is a set of values,
and the allowable operations on those values.
Boolean values
There is a duality built in our world. There is a Heaven and Earth, water and fire, jing and jang, man and woman, love and hatred. In Visual Basic theBoolean data type is a primitive data type having one 
of two values: True or False. This is a fundamental data type. Very common 
in computer programs.
Happy parents are waiting a child to be born. They have chosen a name for both possibilities. If it is going to be a boy, they have chosen John. If it is going to be a girl, they have chosen Jessica.
Option Strict On
Module Example
    Dim num As Byte
    Dim male As Boolean
    Sub Main()
        Randomize()
        num = CType(Math.Round(Rnd()), Byte)
        
        If num = 0 Then
            male = True
        Else
            male = False
        End If
        If male = True Then
            Console.WriteLine("We will use name John")
        Else
            Console.WriteLine("We will use name Jessica")
        End If
    End Sub
End Module
The program uses a random number generator to simulate our case. 
Dim num As Byte
The num variable has a Byte data type. This is important. Byte data
types may have integer values 0..255. 
Randomize()
num = CType(Math.Round(Rnd()), Byte)
These two lines pick up randomly 0 or 1. 
The Rnd() function returns a random value 
less than 1, but greater than or equal to zero.  We use the Round() 
method to round the number. 
Random values greater than 0.5 are rounded to 1. All
other random values are rounded to 0. The Double data type is converted into Byte using the
CType() function. So the num variable is assigned 0 or 1.
The Randomize() function initializes a random-number 
generator with a seed based on the system timer.
If num = 0 Then
    male = True
Else
    male = False
End If
Depending on the num variable, the Boolean male variable is
set to True or False. 
If male = True Then
    Console.WriteLine("We will use name John")
Else
    Console.WriteLine("We will use name Jessica")
End If
If the male variable is set to True, we choose name John.
Otherwise, we choose name Jessica. Control structures like
If/Else statements work with Boolean values. 
Integers
Integers are a subset of the real numbers. They are written without a fraction or a decimal component. Integers fall within a set Z = {..., -2, -1, 0, 1, 2, ...} Integers are infinite.In computer languages, integers are primitive data types. Computers can practically work only with a subset of integer values, because computers have finite capacity. Integers are used to count discrete entities. We can have 3, 4, 6 humans, but we cannot have 3.33 humans. We can have 3.33 kilograms.
| VB Alias | .NET Type | Size | Range | 
|---|---|---|---|
| SByte | System.SByte | 1 byte | -128 to 127 | 
| Byte | System.Byte | 1 byte | 0 to 255 | 
| Short | System.Int16 | 2 bytes | -32,768 to 32,767 | 
| UShort | System.UInt16 | 2 bytes | 0 to 65,535 | 
| Integer | System.Int32 | 4 bytes | -2,147,483,648 to 2,147,483,647 | 
| UInteger | System.UInt32 | 4 bytes | 0 to 4,294,967,295 | 
| Long | System.Int64 | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 
| ULong | System.UInt64 | 8 bytes | 0 to 18,446,744,073,709,551,615 | 
Option Strict On
Module Example
    Sub Main()
        
        Dim a As Byte = 254
        
        Console.WriteLine(a)
        a += 1
        Console.WriteLine(a)
        a += 1
        Console.WriteLine(a)
    End Sub
End Module
In this example, we try to assign a value beyond the range
of a data type. Mono compiler gives a System.OverflowException.
The Visual Basic 2008 Express will not compile this example. 
Integers can be specified in three different notations in Visual Basic. Decimal, hexadecimal and octal. Octal values are preceded by &o, hexadecimal by &h.
Option Strict On
Module Example
    Sub Main()
        
        Dim num1 As Byte = 31
        Dim num2 As Byte = &o31
        Dim num3 As Byte = &h31
        
        Console.WriteLine(num1)
        Console.WriteLine(num2)
        Console.WriteLine(num3)
    End Sub
End Module
We assign 31 to three variables using three notations. And we print them to the console.
$ mono intnotations.exe 
31
25
49
The default notation is the decimal. The program shows these three numbers in decimal. 
If we work with integers, we deal with discrete entities. We would use integers to count apples.
Option Strict On
Module Example
    Sub Main()
        Rem number of baskets
        Dim baskets As Byte = 16
        Rem number of apples in each basket 
        Dim apples_in_basket As Byte = 24
        Rem total number of apples
        Dim total As Short = baskets * apples_in_basket
        Console.WriteLine("There are total of {0} apples", total)
        
    End Sub
    
End Module
In our program, we count the total amount of apples. We use the multiplication operation. 
$ mono apples.exe 
There are total of 384 apples
The output of the program. 
Floating point numbers
Floating point numbers represent real numbers in computing. Real numbers measure continuous quantities. Like weight, height or speed. In Visual Basic we have three important floating point types. Single, Double and Decimal.| VB Alias | .NET Type | Size | Precision | Range | 
|---|---|---|---|---|
| Single | System.Single | 4 bytes | 7 digits | 1.5 x 10-45 to 3.4 x 1038 | 
| Double | System.Double | 8 bytes | 15-16 digits | 5.0 x 10-324 to 1.7 x 10308 | 
| Decimal | System.Decimal | 16 bytes | 28-29 decimal places | 1.0 x 10-28 to 7.9 x 1028 | 
We can use various syntax to create floating point values.
Option Strict On
Module Example
    Sub Main()
        Dim n1 As Single = 1.234
        Dim n2 As Single = 1.2e-3
        Dim n3 As Single = 1 / 3
        Console.WriteLine(n1)
        Console.WriteLine(n2)
        Console.WriteLine(n3)      
      
    End Sub
    
End Module
We have three ways to create floating point values. The first is the 'normal' way using
a decimal point. The second uses scientific notation. And the last one as a result
of a numerical operation. 
$ mono fnotations.exe 
1.234
0.0012
0.3333333
This is the output of the above program. 
Option Strict On
Module Example
    Sub Main()
        Dim n1 As Single = 1 / 3
        Dim n2 As Double = 1 / 3
        If n1 = n2 Then
            Console.WriteLine("Numbers are equal")
        Else 
            Console.WriteLine("Numbers are not equal")
        End If
      
    End Sub
    
End Module
Single and Double values are stored with different precision. 
Caution should be exercised when comparing floating point values. 
$ mono fequal.exe 
Numbers are not equal
And the numbers are not equal. 
Let's say a sprinter for 100m ran 9.87s. What is his speed in km/h?
Option Strict On
Module Example
    Sub Main()
        Dim distance As Single
        Dim time As Single
        Dim speed As Single
        
        Rem 100m is 0.1 km
        distance = 0.1
        Rem 9.87s is 9.87/60*60 h
        time = 9.87 / 3600
        speed = distance / time
        Console.WriteLine("The average speed of a sprinter is {0} km/h", speed)
       
    End Sub
    
End Module
In this example, it is necessary to use floating point values. 
speed = distance / time
To get the speed, we divide the distance by the time. 
$ mono sprinter.exe 
The average speed of a sprinter is 36.47416 km/h
This is the output of the sprinter program. 
Enumerations
Enumerated type (also called enumeration or enum) is a data type consisting of a set of named values. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. Enumerations make the code more readable.Option Strict On
Module Example
    Enum Days
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        Sunday
    End Enum
    Sub Main()
        Dim day As Days = Days.Monday
        
        If day = Days.Monday
            Console.WriteLine("It is Monday")
        End If
        Console.WriteLine(day)
    End Sub
End Module
In our code example, we create an enumeration for week days. 
Enum Days
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
End Enum
The enumeration is created with a Enum keyword.
The Monday, Tuesday ... barewords store in fact numbers 0..6. 
Dim day As Days = Days.Monday
We have a variable called day, which is of enumerated type Days.
It is initialized to Monday.
If day = Days.Monday
    Console.WriteLine("It is Monday")
End If
This code is more readable than if comparing
a day variable to some number. 
Console.WriteLine(day)
This line prints 0 to the console. For a computer,
an enum is a number.
We further work with enumerations.
Option Strict On
Module Example
    Enum Seasons As Byte
        Spring = 1
        Summer = 2
        Autumn = 3
        Winter = 4
    End Enum
    Sub Main()
        Dim s1 As Seasons = Seasons.Spring
        Dim s2 As Seasons = Seasons.Autumn
        
        Console.WriteLine(s1)
        Console.WriteLine(s2)
    End Sub
End Module
Seasons can be easily used as enums. We can specify
the underlying type for the enum plus we can give exact
values for them. 
Enum Seasons As Byte
    Spring = 1
    Summer = 2
    Autumn = 3
    Winter = 4
End Enum
With the As keyword we specify the
underlying type for the enum. We also give each member a
specific number. 
Console.WriteLine(s1)
Console.WriteLine(s2)
These two lines print 1, 3 to the console. 
Strings and chars
String is a data type representing textual data in computer programs.
A string in Visual Basic is a sequence of Unicode characters. A Char
is a single unicode character. Strings are enclosed by single or double quotes.
Since strings are very important in every programming language, we will dedicate a whole chapter to them. Here we only drop a small example.
Option Strict On
Module Example
    Sub Main()
        Dim word As String = "ZetCode"
        Dim c As Char = CType(word.SubString(0, 1), Char)
        Console.WriteLine(c)
    End Sub
End Module
The program prints Z character to the terminal. 
Dim word As String = "ZetCode"
Here we create a string variable and assign it "ZetCode" value.
Dim c As Char = CType(word.SubString(0, 1), Char)
The SubString() method is used to return a
substring from a string. The method return type is a String. We want
to retrieve and assign a single character. This is the reason why we
need to convert a String to a Char with the CType 
function. The character is later printed to the terminal. 
Arrays
Array is a complex data type which handles a collection of elements. Each of the elements can be accessed by an index. All the elements of an array must be of the same data type.We dedicate a whole chapter to arrays, here we show only a small example.
Option Strict On
Module Example
    Sub Main()
        
        Dim array(5) As Integer
        array(0) = 3
        array(1) = 2
        array(2) = 1
        array(3) = 5
        array(4) = 6
        For i As Integer = 0 To array.Length-1
            Console.WriteLine(i)
        Next
    End Sub
End Module
In this example, we declare an array, fill it with data
and then print the contents of the array to the console. 
Dim array(5) As Integer
We declare an integer array, which can store up to 5
integers.
array(0) = 3
array(1) = 2
array(2) = 1
array(3) = 5
array(4) = 6
Here we assign values to the created array.
For i As Integer = 0 To array.Length-1
    Console.WriteLine(i)
Next
We traverse the array and print the data to the
console. The Length property
of the array gives us the length of the array in question.
Date
ADate is value type, which contain date values, 
time values, or date and time values.
Option Strict On
Module Example
    Sub Main()
        Dim today As Date
        today = Now()
        System.Console.WriteLine(today)
        System.Console.WriteLine(today.ToShortDateString())
        System.Console.WriteLine(today.ToShortTimeString())
    End Sub
End Module
We show today's date in three different formats. Date & time, 
date and time. 
Dim today As Date
We declare a variable of Date datatype. 
today = Now()
Returns the current date and time, uses the computer's system date and time.
System.Console.WriteLine(today)
This line prints the date in full format. 
System.Console.WriteLine(today.ToShortDateString())
System.Console.WriteLine(today.ToShortTimeString())
The ToShortDateString()
returns a short date string format, the ToShortTimeString()
returns a short time string format. 
$ mono date.exe 
8/17/2010 1:19:05 PM
8/17/2010
1:19 PM
The output of the example.
Type casting
We often work with multiple data types at once. Converting one data type to another one is a common job in programming. Type conversion or typecasting refers to changing an entity of one data type into another. There are two types of conversion. Implicit and explicit. Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler.Rem Option Strict On
Module Example
    Sub Main()
        Dim val As Byte
        val = 0.54
        Console.WriteLine(val)
        Console.WriteLine(12 + 12.5)
        Console.WriteLine("12" + 12) 
        Console.WriteLine("12" & 12)
    End Sub
    
End Module
In this example, we have an bunch of implicit conversions. 
Rem Option Strict On
Some of the implicit conversions are not possible when the
Option Strict statement is On. This is why we comment this line.
val = 0.54
A floating point value is assigned to the variable of a
Byte data type. It is an integer
data type. The value is rounded to 1; some precision is lost.
This is a narrowing implicit conversion.
Console.WriteLine(12 + 12.5)
We add two values. One integer and one floating point value.
The result is a floating point value. It is a widening implicit conversion.
Console.WriteLine("12" + 12) 
The result is 24. The string is implicitly converted to a number.
Console.WriteLine("12" & 12)
The result is 1212. An integer is converted to a string and
the two strings are added. 
Next we will show some explicit conversions in Visual Basic.
| Function | Converts to | 
|---|---|
| CBool | Boolean | 
| CByte | Byte | 
| CChar | Unicode character | 
| CDate | Date | 
| CDb1 | Double | 
| CDec | Decimal | 
| Clnt | Integer | 
| CLng | Long | 
| CObj | Object | 
| CShort | Short | 
| CSng | Single | 
| CStr | String | 
Option Strict On
Module Example
    Sub Main()
        Console.WriteLine(CByte(0.4))
        Console.WriteLine(CByte(0.9))
        Console.WriteLine(CShort(12 + 12.5))
        Console.WriteLine(Val("1071 Fifth Avenue"))
    End Sub
    
End Module
Console.WriteLine(CByte(0.4))
Console.WriteLine(CByte(0.9))
These two lines perform a narrowing explicit conversion.
The first number is rounded to 0, the second to 1. 
Console.WriteLine(Val("1071 Fifth Avenue"))
The Val() function returns the numbers 
contained in a string. In our case, 1071.
$ mono explicit.exe 
0
1
24
1071
Output of the example. 
In this part of the Visual Basic tutorial, we covered data types and their conversions.
source : http://zetcode.com/lang/visualbasic/
0 comments:
Post a Comment