Visual Basic 2008 (Console) Guide
Arrays Of Records

You may have noticed that, in multi-dimensional arrays, the elements of each dimension must be the same data type. Bear in mind that we have already come across a way of storing more than one type of information with a single identifier. Reread the section on Structures to remind yourself of the basics.

The following program creates an array of structures to store the details of products for sale.

Dim products(4) As TProduct

Structure TProduct
   Dim description As String
   Dim price As Double
End Structure

Sub Main()
   For count = 0 To products.Length - 1
      Console.Write("Enter the description for product {0}: ", count)
      products(count).description = Console.ReadLine()
      Console.Write("Enter the price for product {0}: ", count)
      products(count).price = Console.ReadLine()
   Console.Write("Enter the number of the product: ")
   Dim intProd As Integer = Console.ReadLine()
   Console.WriteLine("Product: {0}, Price: £{1}", products(intProd).description, products(intProd).price)
End Sub

Having A Go

  1. Design a record structure to store multiple choice questions for a small quiz program. Each record must include the question text, the text for 3 possible answers and the letter that corresponds to the correct answer.

    Use an array to store 3 questions using the record structure. Use a loop to output the questions to the screen and accept the user's answers in the form a, b, or c. Store these answers in an array.

    Use a loop to check whether each answer is correct and store the running total of marks in an integer variable.

    End the program by telling the user how many questions they got right.