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

Didier Tibule 21 Reputation points
2021-05-04T05:55:22.17+00:00

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?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,578 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2021-05-04T07:47:04.87+00:00

    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
       . . .
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful