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

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

问题描述:

这是我到目前为止的代码:如何将此代码或类似的代码放在不同的类中? (VB.NET)

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 

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

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

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

+1

你想分开addButton_Click里面的代码吗?因为如果你是,你可能需要做某种绑定,而不是使用控件来提供值 – 2013-02-22 17:23:21

+0

是的,我不确定你是什么意思。 – Exn 2013-02-22 17:26:19

+1

通过绑定,您可以使用类和属性执行所有操作,并将其绑定到控件。用你上面写的方式,你将永远无法将它分成不同的类,因为当前类是唯一具有sellerListBox,productListBox等的类。 – 2013-02-22 17:28:45

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

您体改形式的代码如下所示:

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 

注意,必须创建类使用的实例,因为它含有数据(它不能是一个共享的子)

+1

请注意,尽管其他类不能直接访问表单对象(列表框,测试框,等等)在我的例子中,我传递了对列表框和文本框的引用......你可以做解析/等。在按钮偶处理程序中,然后传递整数。 (这就是我会这样做的 - 它使得这个类更加可重用,并且帮助将前端与后端分开) – Allen 2013-02-22 17:43:25

+1

也不需要像变量名称那样将“my_”前缀添加到变量名称 - 他们没有重复名称,因为它们不在同一个范围内。我只是为了clariry而做的,表明你正在访问传递的变量,而不是直接访问表单变量。 – Allen 2013-02-22 17:52:16

+0

工程很棒。谢谢。 – Exn 2013-02-22 19:21:19

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

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

对。然后OP将需要在控件上使用数据绑定。对于OP,这里是关于数据绑定http://www.codeproject的文章。com/Articles/3665/Data-binding-concepts-in-NET-windows-forms – 2013-02-22 17:34:03