有人可以告诉我如何初始化这些数组以使程序运行并且可能无法查看代码是否有效,或者只是使用更好的代码和注释更正此代码,如果可能的话关于如何在文本框字段上更新一次输入字符串的strNumbers数组的一些指示。
感谢您的帮助
Public class form1
Public Class Box
Public intBoxID As Integer
Public strBoxName As String
Structure positions
Public strBoxPositions() As String
Public strBoxNumbers() As String
End Structure
Public Sub New()
End Sub
Public Sub New(ByVal BoxID As Integer, ByVal BoxName As String)
intBoxID = BoxID
strBoxName = BoxName
End Sub
End Class
Private Sub FormLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ArchiveBox() As Box = {}
Dim arrLetters() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I"}
Dim arrNumbers() As String = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
Dim arrPositions() As String '= New String() {}
'GABE gave me this code earlier which works fine, thanks.
arrPositions = (From letter In arrLetters
From number In arrNumbers
Select letter + number).ToArray()
Dim i As Integer
Dim BoxPositions As Box.positions
Dim strSampleNr() As String = New String() {""}
For i = 0 To 80
ListBox1.Items.Add(arrPositions(i))
BoxPositions.strBoxPositions(i) = arrPositions(i)
BoxPositions.strBoxNumbers(i) = strSampleNr(i)
Next
End Sub
End class
答案 0 :(得分:0)
作为结构位置一部分的数组永远不会被初始化。您应该在Dim BoxPositions
之后立即初始化它们如果您的arrPositions是您实际想要放入单独数组的正确数量的项目,那么我会选择以下内容:
'this is to create new instances of the arrays used in the position structure
'Which is what was missed in your initial code. It initializes the arrays as the same size as
'your arrPositions
BoxPositions.strBoxNumbers = New String(arrPositions.Length - 1) {}
BoxPositions.strBoxPositions = New String(arrPositions.Length - 1) {}
这样你就可以在启动循环过程时准备一个正确大小的数组。
但是,该循环的初始代码仍然无效,因为初始化时strSampleNr数组的大小不正确。
Dim strSampleNr() As String = New String() {""}
创建一个长度为1的字符串数组,因为您没有指定。
在任何情况下,您需要在开始尝试在循环内访问它们之前使用关键字New来初始化数组。尝试访问未实例化的数组将导致您收到的错误。
答案 1 :(得分:0)
Dim ArchiveBox(1) As Box
Dim BoxPositions(81) As Box.positions
Dim arrLetters() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I"}
Dim arrNumbers() As String = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
Dim arrPositions() As String
Dim i As Integer
BoxPositions(i) = New Box.positions
arrPositions = (From letter In arrLetters
From number In arrNumbers
Select letter + number).ToArray()
For i = 0 To 80
BoxPositions(i).strBoxPositions = arrPositions(i)
BoxPositions(i).strBoxNumbers = ""
ListBox1.Items.Add(BoxPositions(i).strBoxPositions)
ListBox1.Items.Add(BoxPositions(i).strBoxNumbers)
Next
我现在尝试了解如何访问BoxID和BoxName属性并将其分配给文本框文本,然后在每次单击按钮时创建并保存它。