无法弄清楚基于 Class 制作数组
Can't figure out making an Array based off of a Class
我想做的是从 Class 创建一个包含信息列表的数组。
到目前为止我尝试过的是 - 首先,创建 Class:
Public Class PowerArray
Public Name As String
Public Action As String
Public Cost As Integer
Public Pool1 As String
Public Pool2 As String
Public Range As String
Public Duration As String
Public Tags As ArrayList = New ArrayList
Public Desc As String
End Class
然后,定义一个使用 PowerArray 作为类型的新对象,包含在 Public Class 中,以便可以在任何子对象中调用它:
Public Class GVar
Public Shared CharPowers(100) As PowerArray
Public Shared PCount As Integer = 0
End Class
但是当我尝试写入该对象时,它一直抛出空引用异常。
示例:
正在尝试向 CharPowers(GVar.Pcount) 写入条目(其中 PCount 当前为 0,“txt.PowerName.Text”为任意字符串):
GVar.CharPowers(GVar.PCount).Name = txtPowerName.Text
...抛出异常。无论我尝试写入哪个值,都会发生这种情况。
在使用该类型(不是数组,而只是一个简单的对象)的子内部定义新对象时,我在这条路线上没有遇到任何重大问题,所以我猜它是一个对象不合适的数组。我只是不知道如何或为什么。它显然存在,它是在启动时定义的。
我似乎被一个答案绊倒了。特别是:
Public Class GVar
Public Shared CharPowers(100) As PowerArray
Public Shared PCount As Integer = 0
Shared Sub New()
For i = 0 To CharPowers.GetUpperBound(0)
CharPowers(i) = New PowerArray
Next
End Sub
End Class
您在 GVar
中的数组是类型为 PowerArray
且值为 Nothing
的元素数组。您必须创建 PowerArray
的实例并将其分配给数组中的元素,然后才能设置 PowerArray
对象的字段。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim PA As New PowerArray
GVar.CharPowers(GVar.PCount) = PA
GVar.CharPowers(GVar.PCount).Name = TextBox1.Text
MessageBox.Show(GVar.CharPowers(GVar.PCount).Name)
End Sub
您打算增加 PCount
吗?如果您使用 List(Of PowerArray)
.
,则无需跟踪您在数组中的位置
编辑
Public Class PowerArray
Public Property Name As String
Public Property Action As String
Public Property Cost As Integer
Public Property Pool1 As String
Public Property Pool2 As String
Public Property Range As String
Public Property Duration As String
Public Property Tags As New List(Of Object)
Public Property Desc As String
End Class
Public Class GVar
Public Shared Property CharPowers As New List(Of PowerArray)
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim PA As New PowerArray
PA.Name = TextBox1.Text
GVar.CharPowers.Add(PA)
For Each item As PowerArray In GVar.CharPowers
MessageBox.Show(item.Name)
Next
End Sub
我想做的是从 Class 创建一个包含信息列表的数组。
到目前为止我尝试过的是 - 首先,创建 Class:
Public Class PowerArray
Public Name As String
Public Action As String
Public Cost As Integer
Public Pool1 As String
Public Pool2 As String
Public Range As String
Public Duration As String
Public Tags As ArrayList = New ArrayList
Public Desc As String
End Class
然后,定义一个使用 PowerArray 作为类型的新对象,包含在 Public Class 中,以便可以在任何子对象中调用它:
Public Class GVar
Public Shared CharPowers(100) As PowerArray
Public Shared PCount As Integer = 0
End Class
但是当我尝试写入该对象时,它一直抛出空引用异常。
示例:
正在尝试向 CharPowers(GVar.Pcount) 写入条目(其中 PCount 当前为 0,“txt.PowerName.Text”为任意字符串):
GVar.CharPowers(GVar.PCount).Name = txtPowerName.Text
...抛出异常。无论我尝试写入哪个值,都会发生这种情况。
在使用该类型(不是数组,而只是一个简单的对象)的子内部定义新对象时,我在这条路线上没有遇到任何重大问题,所以我猜它是一个对象不合适的数组。我只是不知道如何或为什么。它显然存在,它是在启动时定义的。
我似乎被一个答案绊倒了。特别是:
Public Class GVar
Public Shared CharPowers(100) As PowerArray
Public Shared PCount As Integer = 0
Shared Sub New()
For i = 0 To CharPowers.GetUpperBound(0)
CharPowers(i) = New PowerArray
Next
End Sub
End Class
您在 GVar
中的数组是类型为 PowerArray
且值为 Nothing
的元素数组。您必须创建 PowerArray
的实例并将其分配给数组中的元素,然后才能设置 PowerArray
对象的字段。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim PA As New PowerArray
GVar.CharPowers(GVar.PCount) = PA
GVar.CharPowers(GVar.PCount).Name = TextBox1.Text
MessageBox.Show(GVar.CharPowers(GVar.PCount).Name)
End Sub
您打算增加 PCount
吗?如果您使用 List(Of PowerArray)
.
编辑
Public Class PowerArray
Public Property Name As String
Public Property Action As String
Public Property Cost As Integer
Public Property Pool1 As String
Public Property Pool2 As String
Public Property Range As String
Public Property Duration As String
Public Property Tags As New List(Of Object)
Public Property Desc As String
End Class
Public Class GVar
Public Shared Property CharPowers As New List(Of PowerArray)
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim PA As New PowerArray
PA.Name = TextBox1.Text
GVar.CharPowers.Add(PA)
For Each item As PowerArray In GVar.CharPowers
MessageBox.Show(item.Name)
Next
End Sub