0

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

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

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

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

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

4

2 回答 2

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 的建议使用绑定。列表框可以直接绑定到卖家和产品列表。


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

于 2013-02-22T17:32:40.390 回答
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

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

于 2013-02-22T17:37:05.853 回答