创建一个类对象框的新实例()

时间:2011-09-29 03:40:26

标签: .net vb.net

我在那里, 我的目标是在按下按钮时创建一个新的实例框,该框具有字符串名称和要填充的Id,用于连接自动生成的ID,即0到210加上用户添加的字符串“Box”。 我之前在类框中创建了一个结构时遇到了问题,坦率地说我不确定下面的代码是否可行,因为我尝试在VB中运行它我得到一个错误,表明我使用的是变量strBoxPositions()和strBoxNumbers()没有赋值给它。 问题是strBoxPositions()应该由arrPosition()自动填充,它将在创建新对象框的同时创建,并且strNumbers()将使用空字符串创建,以便在后一阶段被填充并在必要时。

有人可以告诉我如何初始化这些数组以使程序运行并且可能无法查看代码是否有效,或者只是使用更好的代码和注释更正此代码,如果可能的话关于如何在文本框字段上更新一次输入字符串的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

2 个答案:

答案 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属性并将其分配给文本框文本,然后在每次单击按钮时创建并保存它。