• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

VB.NET help with classes

Joined
Jan 7, 2003
Messages
39
i am working on this transcript program for students.
i have a class called cSemester
on my form I need a bunch of semesters so i want to declare an array. The problem I am running into is that I can't delcare it properly.

dim sem(8) as cSemester

that will let me declare it. however, whenever i try to use any of the class's methods i get this error "Object reference not set to an instance of an object". So i then assumed its because i didn't create a new instance of the class by using the new keyword so i tried this

dim sem(8) as new cSemester

when i try to compile with that line it says "Arrays can not be declared with 'New'."

so my qestion is how do i declare an array of type cSemester?
 
Try/look into ReDim

You might be able to just ReDim or you might have to dim it with no array size and then ReDim it with the proper size.
 
if i declare it without an inital size

dim sem() as new cSemester

it still doesn't work because it is still an array. it just doesn't have an intial size. If i declare it as not an array i can't redim it because it isn't initially an array

CURSES to VB !!!!!
 
A couple of examples of instantiating an array of classes:
Code:
Module Module1

    Sub Main()
        Dim i As Integer

        Dim myArray1(5) As Foo
        For i = 0 To myArray1.GetUpperBound(0)
            myArray1(i) = New Foo(i)
        Next

        Dim myArray2() As Foo = New Foo() {New Foo(2), New Foo(4), New Foo(6)}

        show(myArray1)
        show(myArray2)

        Console.Read()
    End Sub

    Sub show(ByVal inArray As Foo())
        Dim i As Integer
        Console.WriteLine("Elements: {0}", inArray.GetUpperBound(0))
        For i = 0 To inArray.GetUpperBound(0)
            Console.Write("{0}  ", inArray(i).val)
        Next
        Console.WriteLine()
    End Sub

End Module

Class Foo
    Public val As Integer = 5
    Sub New(ByVal inVal As Integer)
        val = inVal
    End Sub
End Class
 
Back
Top