简体   繁体   English

如何从另一个类 VB.NET 向数组添加项

[英]How To Add Item To An Array From Another Class VB.NET

Okay, so I have a class element, and a form.好的,所以我有一个类元素和一个表单。

I have an Array inside my class element, and I need it to set it from a form...我的类元素中有一个数组,我需要它从表单中设置它...

basically what I'm doing:基本上我在做什么:

----Form1---- ----Form1----

*DataClass.VarArray()*

----DataClass---- ----数据类----

Public VarArray() As String

And I need to know, how do I add an item to an array, and also how to get it from another class... Please我需要知道,如何将一个项目添加到一个数组中,以及如何从另一个类中获取它...请

I Already Tried Different Methods, Like Making A List But that Did not work我已经尝试过不同的方法,比如列一个清单,但没有奏效

NEW: 新:

So, I added a TextBox and a RichTextBox to the form... 因此,我在表单中添加了一个TextBox和RichTextBox。

TextBoxUserName(TextBox): contains the username that we are going to enter to the list TextBoxUserName(TextBox):包含我们要输入到列表中的用户名

TextBoxListOfUsers(RichTextBox): contains the usernames addedd throught the button... is only to get a view of what we are doing. TextBoxListOfUsers(RichTextBox):包含通过按钮添加的用户名...仅用于查看我们在做什么。

Now when you click the NewUserBT button you will add a string (the user name) to the list if is not blank, white spaces, is not yet contained ecc... 现在,当您单击NewUserBT按钮时,如果不是空白,空格,尚未包含ecc,则会将字符串(用户名)添加到列表中。

Check out and tell me... 退房并告诉我...

Public Class Form1
    ' Declare "usersClass" and its type
    Dim usersClass As DataClass

    Public Sub New()
        InitializeComponent()
        ' now give to "usersClass" a value, an instance, so...
        usersClass = New DataClass()
    End Sub

    Public Sub NewUserBT_Click() Handles NewUserBT.Click
        Dim new_user As String = Me.TextBoxUserName.Text

        ' If is not blank, empty or white spaces, and is not contained in the list...
        If Not String.IsNullOrEmpty(new_user) AndAlso Not String.IsNullOrEmpty(new_user) AndAlso Not Me.usersClass.usersNameList.Contains(new_user) Then
            usersClass.usersNameList.Add(new_user)
            Me.TextBoxListOfUsers.Text += new_user + vbCrLf ' name + linefeed
        End If
    End Sub
End Class

Public Class DataClass
    ' Declare "usersNameList" var and its type
    Public usersNameList As List(Of String)

    Public Sub New()
        ' When we "usersClass = New DataClass()" we will go here and set the instance of userNameList
        usersNameList = New List(Of String)
    End Sub
End Class

OLD: 旧:

Public Class Form1
    Dim foo As ArrayClass

    Public Sub New()
        InitializeComponent()
        foo = New ArrayClass()
    End Sub

    Public Sub pippo() Handles Button1.Click
        foo.array(1) = "something"
        Button1.Text = foo.array(1)
    End Sub
End Class

Public Class ArrayClass
    Public array(10) As String

    Public Sub New()

    End Sub
End Class

This might be what you are looking for... I think. 我想这可能是您要寻找的东西。 You can use the array in the form class, give it a try. 您可以在表单类中使用该数组,尝试一下。

Tip: 小费:

Dim strings_list As List(Of String) ' this is a list of strings
Dim strings_array(10) As String ' this is an array to hold 10 strings

I would recommend using a List(Of T) instead of an array if you don't know up front what the size will be. 如果您不知道大小会如何,我建议您使用List(Of T)而不是数组。

I changed your field to a property. 我将您的字段更改为财产。 Fields are usually private. 字段通常是私有的。

In the button click event, I first had to declare an instance of the class. 在按钮单击事件中,我首先必须声明该类的实例。 Then use the instance variable to assign an item to the list. 然后使用实例变量将项目分配给列表。

Public Class Data
    Public ReadOnly Property lst As New List(Of String)

End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dat As New Data
    dat.lst.Add("Hello")
End Sub

Using a field and an array 使用字段和数组

Public Class Data
    Public VarArray(10) As String 'Array needs a size to be created
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dat As New Data
    dat.VarArray(0) = "Hello"
    Debug.Print(dat.VarArray(0))
End Sub

This particular VarArray will only be a part of a particular instance of the Data class. 这个特定的VarArray只会是Data类的特定实例的一部分。 Same is true of the list. 列表也是如此。 To share the data you would make the property/field Shared. 要共享数据,您将使属性/字段共享。 You can then reference as Data.VarArray or Data.lst in the Form. 然后,您可以在窗体中将其引用为Data.VarArray或Data.lst。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM