Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to configure an array with a couple of levels in it so I can select "House" "Room" and "Items" where there is a house with 2 rooms and 3 items in each room, so I want to be able to use "House.Room(1).Item(2).Name".
I can setup the array as - House.Room(1).Item(2).Name = "Bedroom"
but then I get the error - 'Object reference not set to an instance of an object.'
If I setup each variable separately it is fine but when I add the array for the rooms and items I cannot access it and I get the above error.
I have tried several options and looked up multiple samples that do not seam to work, I am hoping someone can suggest some changes.

What I have tried:

Structure Pieces
    Public Name1 As String
    Public Name2 As String
    Public Name3 As String
End Structure

Structure EachRoom
    Public Name As String        'holds the room name
    Public Items() As Pieces
    Private Sub Initialize()
        ReDim Items(0 To 3)
    End Sub
End Structure

Structure MyHouse
    Public Name As String
    Public Room() As EachRoom
    Private Sub Initialize()
        ReDim Room(0 To 2)
    End Sub
End Structure
Public House As MyHouse
Posted
Updated 11-May-17 20:07pm
Comments
[no name] 11-May-17 20:11pm    
Learn how to use the debugger.
Richard Deeming 12-May-17 9:33am    
None of the code you've posted ever calls the private Initialize methods, so your arrays are never initialized, and will always be Nothing.

1 solution

use classes instead of structures ... but notice : classes want to be instantiated ...

VB
Class Pieces
      Public Name1 As String
      Public Name2 As String
      Public Name3 As String

      Public Sub New()
      End Sub
  End Class

  Class EachRoom
      Public Name As String        'holds the room name
      Public Items As New List(Of Pieces)

      Public Sub New()
          For i As Integer = 0 To 3
              Items.Add(New Pieces)
          Next
      End Sub
  End Class

  Class MyHouse
      Public Name As String
      Public Room As New List(Of EachRoom)

      Public Sub New()
          For i As Integer = 0 To 2
              Room.Add(New EachRoom)
          Next
      End Sub
  End Class

  Public House As New MyHouse


Additional like written in the Reply :

VB
Class MyHouse
    Public Name As String
    Public Room As New List(Of EachRoom)

    Public Sub New()
        For i As Integer = 0 To 2
            Room.Add(New EachRoom)
        Next
    End Sub

    Public Sub New(RoomCount as integer) ' with this constructor you can tell the class how much EachRoom you want to have inside it. You are now flexible and not more fixed to 3 EachRoom ...
        For i As Integer = 1 To RoomCount
            Room.Add(New EachRoom)
        Next
    End Sub
End Class
 
Share this answer
 
v2
Comments
Gyzmo 13-May-17 0:21am    
Yes I realised after shutting down my PC that I forgot to add the initialize commands thanks for that and actually went to add it in when I saw your reply.
I have been looking and working on your code but did not understand the "classes want to be instantiated" as I have not had to do this before(it seams).
Hope you can offer a suggestion thanks.
Ralf Meier 13-May-17 6:23am    
OK ... have you tested my changes to your code ?
If Yes you have seen, that this code automaticly does (with the constructor : New) what you want.
Different to a structure is :
- if you write : 'Public House as MyHouse' you haven't created the Object itself - you only defined the Type
- if you write : 'Public House as NEW MyHouse' you create a NEW Instance of the Object with all it's functionality. Also the Constructor-Method 'New' inside the class is called. If you follow this way you will see that you can also create different constructors to a class. To see what I mean you should test yourself a little bit the code and what it does (Debugger !!! and Breakpoints inside the code !!!)
To give you an idea I improve my Answer with a second constructor ...

(If you find my answer useful it would be nice if you accept 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