Generic Example

Generic Example

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Namespace GenericsExample1
    Public Class MyGenericArray(Of T)
        Private array As T()

        Public Sub New(ByVal size As Integer)
            array = New T(size + 1 - 1) {}
        End Sub

        Public Function getItem(ByVal index As Integer) As T
            Return array(index)
        End Function

        Public Sub setItem(ByVal index As Integer, ByVal value As T)
            array(index) = value
        End Sub
    End Class

    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim intArray As MyGenericArray(Of Integer) = New MyGenericArray(Of Integer)(5)

            For c As Integer = 0 To 5 - 1
                intArray.setItem(c, c * 5)
            Next

            For c As Integer = 0 To 5 - 1
                Console.Write(intArray.getItem(c) & " ")
            Next

            Console.WriteLine()
            Dim charArray As MyGenericArray(Of Char) = New MyGenericArray(Of Char)(5)

            For c As Integer = 0 To 5 - 1
                charArray.setItem(c, ChrW((c + 97)))
            Next

            For c As Integer = 0 To 5 - 1
                Console.Write(charArray.getItem(c) & " ")
            Next

            Console.WriteLine()
            Dim floatArray As MyGenericArray(Of Single) = New MyGenericArray(Of Single)(5)

            For c As Integer = 0 To 5 - 1
                floatArray.setItem(c, CSng((c + 3.1416)))
            Next

            For c As Integer = 0 To 5 - 1
                Console.Write(floatArray.getItem(c) & " ")
            Next

            Console.WriteLine()
            Console.ReadKey()
        End Sub
    End Class
End Namespace


┬ęCopyright 2016 All Rights Reserved LearnDotNetHome.com