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

代わりに、このコードを別のクラス/フォームに入れたいと思います。このクラスは、ユーザーが入力した情報を保管するために使用されます。リスト ボックスが 2 つ、ボタンが 1 つ、テキスト ボックスが 1 つあります。ユーザーは各リスト ボックスで項目を選択し、テキスト ボックスに番号を入力してから、ボタンをクリックして情報をストックします。

別のクラスを使ってコードを実装しようとしましたが、動作しませんでしたが、上記のコードのように配置すると動作します。

編集:どうもありがとう!私はこれを少し試してみます。

4

2 に答える 2

1

さまざまな可能性があります。製品と販売者用に 2 つのクラスを作成することをお勧めします

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 に答える