![]() |
VB.Net Tutorial #5
Welcome to the fifth VisualBasic.Net tutorial (I really must find a better way to start these tutorials). I want to start with something that is very useful particularly if you want to create console based menus (I know that Windows GUI programming does all that but it is a useful construct all the same) which is called case...select (if you are familiar with C then you will know it as case...switch). It allows a decision to be made without using a whole lot of if...else statements as follows:-
Imports System
Class hello
Public Shared Sub Main()
Dim i As Integer = 4
Select i
Case 1
Console.WriteLine("The value is 1")
Case 2
Console.WriteLine("The value is 2")
Case 3
Console.WriteLine("The value is 3")
Case Else
Console.WriteLine("The value isn't 1, 2 or 3")
End Select
End Sub
End Class
C# differs from earlier languages in that it insists on the use of the break keyword in each case statement but VB is quite happy without it. So how does it work? An integer value is set up which is coded to conatain the number 4. The case statements will therefore branch at the default statement and writeline the relevent value - if it had been set to 1, 2 or 3 then the corresponding writeline will be used and the program will end. Try changing the value of i and recompile and see what happens. This can also be used with strings or characters as follows:-
Imports System
Class hello
Public Shared Sub Main()
Dim c As Char = "H"
Select c
Case "h"
Console.WriteLine("The value is h")
Case "H"
Console.WriteLine("The value is H")
Case "e"
Console.WriteLine("The value is e")
Case Else
Console.WriteLine("The value isn't h, H or e")
End Select
End Sub
End Class
Note that the test is case sensitive (although using toUpper or toLower would cure the problem). As I said, a very useful construct. Now on to two different concepts that may at first seem unrelated but they do lead on into the next tutorial. The first is a structure as folows:-
Imports System Class hello Public Structure MyPerson Public first As String Public last As String End Structure Public Shared Sub Main() Dim personName As MyPerson personName.first = "Nick" personName.last = "James" Console.WriteLine(personName.first) Console.WriteLine(personName.last) End Sub End Class
A structure (known as a struct in C) is not unlike an array in that it can contain data which can then be added to or accessed. This first example creates a structure containing 2 variables then can then be accessed from the main program. The name of the structure is used to create a reference to it which is called personName which creates an instance of the structure in memory. The individual elements of the structure are accessed using a dot followed by the name of the element. So to update the first name we use personName.first followed by the string to enter into it.
Now it gets interesting. I wanted to create a structure like I have in the past using standard C as follows:-
struct MyPerson{
char first[25];
char last[25];
} personName[50];
This creates an array of fifty structures with each element fixed to 25 characters each which can be accessed using:-
personName[i].first="Nick"
so we can have a database of 50 people to call on. Seems quite reasonable that VB would be the same. It isn't. Take a look at this:-
Imports System Imports System.Runtime.InteropServices Class hello <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _ Public Structure MyPerson <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=25)> _ Public first As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=25)> _ Public last As String End Structure Public Shared Sub Main() Dim personName(50) As MyPerson personName.Initialize personName(1).first = "Nick" personName(1).last = "James" Console.WriteLine(personName(1).first) Console.WriteLine(personName(1).last) End Sub End Class
This VB program does the same as the C program above it. What is all this stuff? We need the interop service so we can use the MarshalAs command further down. The layout of the structure is then defined although in some case it may be alright to remove it all. Now we want to fix the size of each array element to 25 characters. This is achieved using the MarshalAs code which sets the type of string and the size which strikes me as being a bit ungainly. On now to functions.
Imports System Public Class FunctionSample Public Shared Function AddItems(ByVal Value1 As Integer, ByVal Value2 As Integer) As Integer Dim ReturnValue As Integer ReturnValue = Value1 + Value2 Return ReturnValue End Function Public Shared Sub Main() Dim k As Integer k = AddItems(1, 2) Console.WriteLine(k) End Sub End Class
Functions were what made the C programming language one of the most useful languages ever and it is included in VB. A function is a little like a subroutine except that information can be passed to it and information can be passed out of it again back to calling program. So here we have a function that will add 2 numbers and return the result of the calculation. Note that while Value1 and Value2 are part of the function decalration, it isn't necessary to declare them inside the function. However, within the function is the ReturnValue which we need to pass back to Main() so it is declared.
So thats it with functions and structures. But it is possible to combine them both into...Classes...see you next time...