question

DidierTibule-3312 avatar image
0 Votes"
DidierTibule-3312 asked Viorel-1 commented

Create a table in a class that inherits from another class. vb.net

Hi;

I've been battling for some times now with a little problem which I think has a very easy solution; but can't find it. Maybe I couldn't get the right way to formulate my question on Google... Anyways, I went around it but the way I've done it is very inelegant and cumbersome. What I really want to do would be something like this:

 Public Class Form1
 Public Class my_classOne
     Public the_name As String = ""
     Public the_surname As String = ""
 End Class

 Public Class my_classTwo
     Inherits my_classOne
     Public a_table(10) As my_classOne
     Public a_thing As New my_classOne
 End Class


 Public the_record As New my_classTwo

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     Dim i As Integer
     For i = 1 To 10
         the_record.a_table(i).the_name = "Name is:" + i.ToString
         Label1.Text = the_record.a_table(i).the_name
         the_record.a_thing.the_name = "Name: " + i.ToString
         Label1.Text = the_record.a_thing.the_name
     Next
 End Sub
 End Class

I get, when debugging, the error shown in the picture on that line of code:

 the_record.a_table(i).the_name = "Name is:" + i.ToString

93439-error.jpg

What do I do wrong? How can I get a_table() in my_classTwo to inherit from my_classOne?


dotnet-visual-basic
error.jpg (107.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered Viorel-1 commented

It seems that you did not create the objects. Try this:

 For i = 1 To 10
     the_record.a_table(i) = New my_classOne
     the_record.a_table(i).the_name = "Name is:" + i.ToString
    . . .
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks!!! It works good.

I had although to give a dimension to the array for it to work. Which is fine; but do i HAVE to give a dimension to the array or there is another way. Just curious :)

0 Votes 0 ·

If you the size is not fixed, you can use a list instead of array: ‘Public a_list As List(Of my_classOne) = New List(Of my_classOne)’. Then you can add and remove objects, access elements by ‘a_list(i)’, etc.

1 Vote 1 ·