Array of struct Learn programming Visual Basic (VB.net)



Lesson:

Arrays, Structures and Strings


Exercise:

Array of struct


Objetive:

Expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the user for data for the first two points and then display them.


Code:

Imports System
Public Class exercise80
    Structure point
        Public x As Short
        Public y As Short
        Public r As Byte
        Public g As Byte
        Public b As Byte
    End Structure

    Public Shared Sub Main()
        Dim p As point() = New point(999) {}
        Console.Write("Enter X for first point: ")
        p(0).x = Convert.ToInt16(Console.ReadLine())
        Console.Write("Enter Y for first point: ")
        p(0).y = Convert.ToInt16(Console.ReadLine())
        Console.Write("Enter Red for first point: ")
        p(0).r = Convert.ToByte(Console.ReadLine())
        Console.Write("Enter Green for first point: ")
        p(0).g = Convert.ToByte(Console.ReadLine())
        Console.Write("Enter Blue for first point: ")
        p(0).b = Convert.ToByte(Console.ReadLine())
        Console.Write("Enter X for second point: ")
        p(1).x = Convert.ToInt16(Console.ReadLine())
        Console.Write("Enter Y for second point: ")
        p(1).y = Convert.ToInt16(Console.ReadLine())
        Console.Write("Enter Red for second point: ")
        p(1).r = Convert.ToByte(Console.ReadLine())
        Console.Write("Enter Green for second point: ")
        p(1).g = Convert.ToByte(Console.ReadLine())
        Console.Write("Enter Blue for second point: ")
        p(1).b = Convert.ToByte(Console.ReadLine())
        Console.WriteLine("P1 is located in ({0},{1}), colour ({2},{3},{4})", p(0).x, p(0).y, p(0).r, p(0).g, p(0).b)
        Console.WriteLine("P2 is located in ({0},{1}), colour ({2},{3},{4})", p(1).x, p(1).y, p(1).r, p(1).g, p(1).b)
    End Sub
End Class



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.