如何在VB.NET中配置数组 [英] How to configure an array in VB.NET

查看:73
本文介绍了如何在VB.NET中配置数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试配置一个包含几个级别的数组,所以我可以选择HouseRoom和Items,其中每个房间有2个房间和3个项目的房子,所以我想要能够使用House.Room(1).Item(2).Name。

我可以将数组设置为 - House.Room(1).Item(2).Name = 卧室

然后我得到错误 - '对象引用没有设置为对象的实例。'

如果我单独设置每个变量,它很好但是当我添加了房间和我无法访问的项目的数组,我得到了上述错误。

我已经尝试了几个选项并查找了多个不接缝工作的样本,我希望有人可以建议一些变化。



我尝试过:



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

推荐答案

使用类而不是结构......但请注意:类想要实例化...



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

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 :

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


这篇关于如何在VB.NET中配置数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆