Module Module1 '**************************************************** 'this module is meant to show several programming 'structures as they appear in VB.NET 'These include, Sub routines, Functions, 'Declaring Variables, While loops, for loops, 'IF then else structures, Select case and 'a simple array. It also includes calling methods 'and functions and passing parameters. It also 'contains a thrown error and a try catch structure 'Steve Conger December 2007 '***************************************************** 'comments begin with an apostrophe. A module 'is a left over from VB6--it is almost but 'not quite a class 'sub Main is where the program starts. 'A sub is a routine that doesn't return 'a value--notice no braces-- a sub ends 'with End Sub Sub Main() Display() End Sub Sub Display() 'dim--declare a variable and type and assign a value Dim choice As Integer = 1 'put the menu in a while loop While choice <> 4 Console.Clear() Console.WriteLine("**************************************") Console.WriteLine("*1 Get Modulus of two numbers *") Console.WriteLine("*2 Create and display an array *") Console.WriteLine("*3 Get the cube of a number *") Console.WriteLine("*4 Exit *") Console.WriteLine("**************************************") Console.WriteLine() Console.WriteLine("Enter your choice") choice = Integer.Parse(Console.ReadLine()) 'use a select case to evaluate the variable choice 'same as a switch structure in C languages Select Case choice Case 1 Console.WriteLine("Enter the firstNumber") Dim num1 As Integer = Integer.Parse(Console.ReadLine()) Console.WriteLine("Enter the SecondNumber") Dim num2 As Integer = Integer.Parse(Console.ReadLine()) 'create a try to test the code, if there is an error 'it will be caught by the catch and in this case 'display the error message Try Console.WriteLine("The modulus is {0}", Remainder(num1, num2)) Catch ex As Exception Console.WriteLine(ex.Message) End Try Pauseit() Case 2 displayArray() Pauseit() Case 3 Console.WriteLine("Enter a Number to Cube") Dim num As Double = Double.Parse(Console.ReadLine()) Console.WriteLine("The cube is {0}", Cube(num)) Pauseit() Case 4 Exit Sub Case Else 'catch any number not a valid case Console.WriteLine("Invalid entry please choose again") Pauseit() End Select End While End Sub 'a routine that returns a value is a function Function Remainder(ByVal number1 As Integer, ByVal number2 As Integer) As Integer Dim rmdr As Integer 'simple if else statement If number2 <> 0 Then 'the modulus operator is mod not % rmdr = number1 Mod number2 Else 'throw an exception back to the calling function Throw New DivideByZeroException End If Return rmdr End Function Function Cube(ByVal number As Double) As Double Return number * number * number End Function Function createArray() As Double() 'Dim an array Dim myArray() As Double = New Double(5) {} Randomize() 'Dim a random object Dim rand As New Random() 'Dim a variable Dim x As Integer 'example of a for loop For x = 0 To 5 'create a random number between 1 and 1000 'and assign it to the array myArray(x) = rand.Next(1, 1000) Next Return myArray 'return an array End Function Sub displayArray() 'assign the array that is returned from 'create array to a new array Dim myArray2() As Double = createArray() Dim i As Integer 'loop through the array to show 'its values For i = 0 To myArray2.Length - 1 Console.WriteLine(myArray2(i)) Next End Sub Sub Pauseit() Console.WriteLine("Press any key to continue") Console.ReadKey() End Sub End Module