Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys
I am trying to make an array as a class. But no luck.
Anybody have idea why this code returns exception and doesn't run?

What I have tried:

VB.NET
Class MainWindow
    Sub New()
        InitializeComponent()
        Dim MyTest(1) As Employee
        MyTest(1).ID = 1
        MyTest(1).Name = "John"
        MsgBox(MyTest(1).ID)
    End Sub
    Public Class Employee
        Public Property ID As Long
        Public Property Name As String
    End Class
End Class
Posted
Updated 2-Jul-23 14:19pm
v6
Comments
Dave Kreskowiak 2-Jul-23 18:26pm    
What do you mean by "array as a class"?

Arrays in both VB & C# are zero-based, not one-based with .Net Framework and .Net (Core). ref: Arrays - Visual Basic | Microsoft Learn[^].

Also, as pointed out by Ralf, if the class is not static, a Module in VB.Net, then you need to initialise it before using it.

So you need to change to:
VB
Dim MyTest(1) As Employee
MyTest(0) = New Employee() 'initialise before use
MyTest(0).ID = 1
MyTest(0).Name = "John"
MsgBox(MyTest(0).ID)

VB.Net also has some nice syntactical sugar, for readability, when working with setting properties or calling methods (subroutines & functions) on a class using the With[^] syntax:
VB
Dim MyTest(1) As Employee

MyTest(0) = New Employee()
With MyTest(0)
    .ID = 1
    .Name = "John"
End With

MsgBox(MyTest(0).ID)

NOTE: This is only in VB.Net and not supported in other languages.
 
Share this answer
 
v3
Comments
Ralf Meier 3-Jul-23 2:35am    
where is the difference between your suggestion_1 and my suggestion_2 ?
after dimensioning the Array to 1 it consists of 2 elements (Index 0 and Index 1) - I don't know if that is equal to C# ... but at VB it happens like described ...
Graeme_Grant 3-Jul-23 3:37am    
You change from Array to List. Still have an out of bound issue for your second example with array size of 1. Yes, I too tested. ;)
Ralf Meier 3-Jul-23 3:40am    
No ... of course List of is also an suggestion from me ... but please read my solution and the added parts carefully ...
There is a difference in dimensioning Arrays ...
Ralf Meier 3-Jul-23 3:42am    
my question now : why did my code sample works by me and not by you ? Where is the difference ?
Graeme_Grant 3-Jul-23 3:45am    
You did not answer the question but gave a different solution. You then gavea second solution with an error. Then the last solution was a correction after I called out your error. My solution was correct from the first posting. I added a bonus method of working with class properties and methods, as I too was once a VB programmer. I still offer VB code with articles that I write. ;)

Love the down vote Ralf ... very nice of you ;)
A Class is an Object which must be first instanced before using it.
In your code you don`t have such an Instance from Employee - only an Object-Declaration.

Instead of that you could use the following code :
VB.NET
Sub New()
    InitializeComponent()
    Dim MyTest As New List(Of Employee)
    Dim x As New Employee
    x.ID = 1
    x.Name = "John"
    MyTest.Add(x)
    MsgBox(MyTest(0).ID)
End Sub

Public Class Employee
    Public Property ID As Long
    Public Property Name As String
End Class


or like that :
VB
Sub New()
    InitializeComponent()
    Dim MyTest(1) As Employee
    MyTest(1) = New Employee
    MyTest(1).ID = 1
    MyTest(1).Name = "John"
    MsgBox(MyTest(1).ID)
End Sub


completed after discussion with Grame_Grant :
VB
Sub New()
    InitializeComponent()
    Dim MyTest(1) As Employee
    MyTest(0) = New Employee
    MyTest(0).ID = 1
    MyTest(0).Name = "John"

    MyTest(1) = New Employee
    MyTest(1).ID = 2
    MyTest(1).Name = "Jack"

    MsgBox(MyTest(0).ID.ToString + " " + MyTest(0).Name + vbCrLf + MyTest(1).ID.ToString + " " + MyTest(1).Name)
End Sub


and if I add the following I will get the decribed error / exception :
VB
MyTest(2) = New Employee
MyTest(2).ID = 3
MyTest(2).Name = "Jim"
 
Share this answer
 
v6
Comments
Graeme_Grant 2-Jul-23 20:14pm    
"A Class is an Object which must be first instanced before using it." only true if the class is not static. If a method of a class is public and static, then the class also does not need to have an instance generated to call that method.

Also, your code example will fail. Read my first sentence in my solution, I think that some missed that detail.
Ralf Meier 3-Jul-23 2:29am    
OK ... you are right ... but if you make a Class Static you create an Instance of it ...
Where do you think thta my code-sample will fail ? I have tested both versions ...
Graeme_Grant 3-Jul-23 3:08am    
A collection size of (1) = 1 element. Array/collection is base 0. So accessing element(1) = 2nd element, not the first, which is element(0), is out of bounds. So if you run your code you will see the error message:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Ralf Meier 3-Jul-23 3:25am    
No ... as I allready wrote - I tested it and it works ...
See also my comment to your Solution ...
Ralf Meier 3-Jul-23 3:34am    
I made also an addition to my Solution as an add to our discussion ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900