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

Didier Tibule 21 Reputation points


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  
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  


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

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

Accepted answer
  1. Viorel 113K Reputation points

    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