Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.89/5 (3 votes)
See more:
Hi guys.
When I define an array into an array into a class, I give exception.
Any kindly help please?

What I have tried:

The code:


VB.NET
Class MainWindow
    Sub New()
        InitializeComponent()
        Dim MyTest(1) As Employe = New Employee()
        MyTest(1).ID = 1
        MyTest(1).Name = "John"
        MyTest(1).BankNames(1)="ISQE Bank"   '<--- The exception is in this line 
    End Sub
    Public Class Employee
        Public Property ID As Long
        Public Property Name As String
        Public Property BankNames() as String
    End Class
End Class
Posted
Updated 3-Jul-23 18:13pm
Comments
Ralf Meier 3-Jul-23 15:40pm    
and what size has this Array "BankNames" inside your Class ?
What are you trying to achieve ?
Sh.H. 3-Jul-23 16:03pm    
Guess it is 10. I forgot to assign it in the main program.

Well this is the sma question you already posted at Why I can't make array as class? Vb.net[^], and you were already given the answer.

However, it probably bears repeating. First issue is that you cannot set a complete array to reference a single item. When you create the array you must initialise every element within it. Also remember, as @Ralf_Meier pointed out in the previous question, that in VB.NET when declaring an array, the number in parentheses is the upper limit of the array, not the number of elements.
So ...
VB
    Sub New()
        InitializeComponent()
'        Dim MyTest(1) As Employe = New Employee() <-- this is incorrect
        Dim MtTest(1) As Employee ' that is, create an array of TWO elements
        MyTest(0) = New Employee() ' initialise the first element
        MyTest(1) = New Employee() ' initialise the second element
' only now can you access the elements MyTest(0) and MyTest(1)
        MyTest(1).ID = 1
        MyTest(1).Name = "John"
        MyTest(1).BankNames(1)="ISQE Bank"   '<--- The exception is in this line 
    End Sub
 
Share this answer
 
Comments
Ralf Meier 3-Jul-23 15:50pm    
... so we are all back again ... ;-)
Sh.H. 3-Jul-23 15:52pm    
The Exception is on defining array of BankNames. Could you please gimme a example of how can I do that in code? Guess it is 10 times for example.
Ralf Meier 3-Jul-23 15:54pm    
See my Solution - it is exactly like that ...
perhaps you try it like this :

VB.NET
Public Class Employee
    Public Property ID As Long
    Public Property Name As String
    Property BankNames As String()
        Get
            Return myBankNames
        End Get
        Set(value As String())
            myBankNames = value
        End Set
    End Property
    Private myBankNames(10) As String
End Class


in this case your Sub-Array has a Size of 11 Elements (0 .. 10)


After seeing the other Answers: I would prefer the Suggestion from Dave - in my opinion that would be the best approach ...
 
Share this answer
 
v2
Comments
Sh.H. 3-Jul-23 15:57pm    
Thanks. But what is the different? You wrote the old version of Property in class!
Ralf Meier 3-Jul-23 16:00pm    
the difference is that the class now has a defined array in it which will be delivered with the Property. Using a Porperty like a public Variable isn't a good style at any time. It's allways better to use the Getter and the Setter Methodes of it ...
Richard MacCutchan 3-Jul-23 15:57pm    
I agree, but your answer is still better than mine.
Sh.H. 3-Jul-23 15:58pm    
You set it to 10! I do not need to set it in class. I need to set it in main program.
Ralf Meier 3-Jul-23 16:03pm    
that isn't possible - sorry.
In this case you should think about the suggestion of Dave - but that isn't an Array - that is an empty suitcase which could be filled with your need. Perhaps you engage yourself a little with that ...
You never said what the exception was, but I can take a guess.

In your Employee class, you declared a property called BankNames that CAN hold an array of strings. You did not actually DEFINE the array of strings. BankNames doesn't point to an array yet.

A better way to do it would be to use a List(Of String) instead of an array.
VB.NET
Class MainWindow
    Sub New()
        InitializeComponent()

        Dim MyTest(1) As Employee = New Employee()
        MyTest(0).ID = 1
        MyTest(0).Name = "John"
        MyTest(0).BankNames.Add("ISQE Bank")
    End Sub
End Class

Public Class Employee
    Public Property ID As Long
    Public Property Name As String
    Public Property BankNames As New List(Of String)
End Class
 
Share this answer
 
Comments
Sh.H. 3-Jul-23 15:56pm    
You are right. Thanks for correct guess. But List(Of String) is not different with Array, is it?
Ralf Meier 3-Jul-23 16:04pm    
As I wrote at my suggestion - it is complete different --- but very much better ...
Dave Kreskowiak 3-Jul-23 18:03pm    
The List classes are wrappers around a managed array. If the array needs to be resized, the List class handles that for you.
Thanks everybody. As Ralf Meier says, I write a clean code here as final solution.
the only thing should be change is the place of parenthesis in the class. It should be after String, not after BankNames. :-)

VB.NET
Class MainWindow
    Sub New()
        InitializeComponent()
        Dim MyTest(1) As Employe = New Employee()
        MyTest(1).ID = 1
        MyTest(1).Name = "John"
        Dim MyTest(1).BankNames(1) As string  '<--- Here makes an array of BankNames with size of 2.
        MyTest(1).BankNames(1)="ISQE Bank"   
    End Sub
    Public Class Employee
        Public Property ID As Long
        Public Property Name As String
        Public Property BankNames as String()
    End Class
End Class
 
Share this answer
 
v4
Comments
Ralf Meier 4-Jul-23 3:00am    
I#M sorry - you version may work ... but it isn't a good way to realize that. I don't agree with it ...
Sh.H. 4-Jul-23 5:33am    
@Ralf Meier
Why? Please explain your reason.
Ralf Meier 4-Jul-23 5:43am    
In my opinion the memory-usage comes from the underlying class. In your Solution your dimension of the BankNames-Array is done outside it.
What happens if you later (in same months or more) go back to your application ? Or someone complete different ? I suppose that is difficult to realize the/your layout. Also in my opinion it is complete against OOP - but that are my 2 cents ...
Sh.H. 4-Jul-23 7:40am    
Thanks. Hope someone find better solution.
Ralf Meier 4-Jul-23 12:18pm    
I don't believe ... For your issue the best approach would be, as allready mentioned by me, the Solution with the List (of String).
Perhaps you take a look on in and engage yourself with it ...

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