如何将此代码或类似代码放在不同的类中? (VB.NET)

时间:2013-02-22 17:17:11

标签: arrays vb.net class

这是我到目前为止的代码:

Public Class firstForm
    Dim sale(3, 4) As Integer
    Dim numberSellers(3) As Integer
    Dim numberProducts(4) As Integer

Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click


    Dim sellerLineInteger As Integer
    Dim productColumnInteger As Integer

    sellerLineInteger = sellerListBox.SelectedIndex
    productColumnInteger = productListBox.SelectedIndex

    ' add in two dimensional array 
    If sellerLineInteger >= 0 And productColumnInteger >= 0 Then
        sale(sellerLineInteger, productColumnInteger) = Decimal.Parse(saleTextBox.Text)
    End If

    saleTextBox.Clear()
    saleTextBox.Focus()

End Sub

我想将此代码放在不同的类/表单中。该类将用于存储用户输入的信息。 我有两个列表框,一个按钮和一个文本框。用户在每个列表框中选择一个项目,在文本框中输入一个数字,然后单击按钮以存储信息。

我试图通过使用另一个类来实现代码,但是我无法使它工作,但是当我把它放在我上面显示的代码中时它会起作用。

编辑:非常感谢!我会稍微尝试一下。

2 个答案:

答案 0 :(得分:1)

有不同的可能性。我建议为产品和卖家创建两个类

Public Class Product
    Public Property Name As String
    Public Property Sale As Decimal
End Class

Public Class Seller
    Public Property Name As String

    Private _products As New Dictionary(Of String, Product)()
    Public ReadOnly Property Products() As Dictionary(Of String, Product)
        Get
            Return _products
        End Get
    End Property

    Public Sub SetProductSale(productName As String, sale As Decimal)
        Dim product As Product
        If _products.TryGetValue(productName, product) Then
            product.Sale = sale
        Else
            product = New Product() With { _
                .Name = productName, _
                .Sale = sale _
            }
            _products.Add(productName, product)
        End If
    End Sub

    Public Function GetProductSale(productName As String) As Decimal
        Dim product As Product
        If _products.TryGetValue(productName, product) Then
            Return product.Sale
        End If
        Return 0D
    End Function
End Class

在您的表单中,您可以执行以下操作(我假设您的列表框将卖家和产品的名称存储为字符串):

Public Class FirstForm
    Private _sellers As New Dictionary(Of String, Seller)()

    Public Sub addButtonClick(sender As Object, e As EventArgs)
        If sellerListBox.SelectedIndex >= 0 AndAlso _
           productListBox.SelectedIndex >= 0 Then

            Dim sellerName As String = sellerListBox.SelectedItem.ToString()
            Dim productName As String = productListBox.SelectedItem.ToString()

            Dim sale As Decimal
            If [Decimal].TryParse(saleTextBox.Text, sale) Then
                Dim seller As Seller
                If Not _sellers.TryGetValue(sellerName, seller) Then
                    seller = New Seller() With { _
                        .Name = sellerName _
                    }
                    _sellers.Add(sellerName, seller)
                End If

                seller.SetProductSale(productName, sale)
            End If
        End If
    End Sub

End Class

但你可以更进一步,使用绑定,如DJ Burb建议的那样。列表框可以直接绑定到卖家和产品列表。


正如我所说,有不同的方法。在此示例中,我将销售直接存储在产品中,并且每个销售商都有每个产品的副本。您还可以考虑将产品和销售额结合起来的单独销售类别。然后,卖方将拥有销售词典而不是产品词典。然后,所有产品将存储在单独的产品词典中。这将允许您保留产品的唯一实例。

答案 1 :(得分:1)

我假设你想尽可能多地移动到另一个班级...... 如果是这样,那就是你如何做到这一点的一个例子。

您的修改后的表单代码如下:

Public Class firstForm

    Dim MyOtherClass As New Class1

    Private Sub addButton_Click(sender As System.Object, e As System.EventArgs) Handles addButton.Click
        MyOtherClass.addItem(sellerListBox, productListBox, saleTextBox)
    End Sub
End Class

并且新类看起来像这样:

Public Class Class1
    Private sale(3, 4) As Integer
    Private numberSellers(3) As Integer
    Private numberProducts(4) As Integer




    Public Sub addItem(ByRef my_sellerListBox As ListBox, ByRef my_productListBox As ListBox, ByRef my_saleTextBox As TextBox)
        Dim sellerLineInteger As Integer
        Dim productColumnInteger As Integer

        sellerLineInteger = my_sellerListBox.SelectedIndex
        productColumnInteger = my_productListBox.SelectedIndex

        ' add in two dimensional array 
        If sellerLineInteger >= 0 And productColumnInteger >= 0 Then
            sale(sellerLineInteger, productColumnInteger) = Decimal.Parse(my_saleTextBox.Text)
        End If

        my_saleTextBox.Clear()
        my_saleTextBox.Focus()


    End Sub

End Class

请注意,您必须创建要使用的类的实例,因为它包含数据(它不能是共享子)