重载解析失败vb.net

时间:2014-02-15 22:54:26

标签: vb.net overloading

我正在使用Visual Basic 2010,当我尝试声明重载解析失败,因为没有可访问的'New'接受此数量的参数。) >我的产品。我不知道我做错了什么。请帮忙。

Dim Counter As Integer = 0

Private arrayProducts(9) As Products.Products.Products

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    If Counter < 10 Then
        Dim myProduct As New Products.Products.Products(arrayProducts(Counter + 1),
                                                        CDec(txtItemNumber.Text), txtName.Text,
                                                        txtManufacturer.Text, CDbl(txtWholeSalePrice.Text), CInt(txtQuantity.Text))
        arrayProducts(Counter) = myProduct
        MessageBox.Show("Product is added to inventory.")
        Counter += 1
    Else
        MessageBox.Show("Sorry, inventory is full.")
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

这段代码:

   Dim myProduct As New Products.Products.Products(arrayProducts(Counter + 1),
                                                    CDec(txtItemNumber.Text),
                                                     txtName.Text,
                                                    txtManufacturer.Text,
                                                    CDbl(txtWholeSalePrice.Text),
                                                    CInt(txtQuantity.Text))

意味着你必须拥有一个需要6个args的ctor(Sub New),可能是这样的:

 Sub New(Index As Integer, ProdCode As String, ProdName As String,
         ManufacturerName As String, Price as Double, Quantity As Integer)

错误是说它找不到那个构造函数(即许多参数但类型也必须按顺序{Int32, String, String, String, Double, Integer})。有了这么多args,你可能有太多 - 或者很难想象 - 太少了。 IntelliSence应该弹出实际的;你输入的arg列表。

您可能会发现List比旧学校阵列更容易使用:

Friend ProdList As New List(Of Products.Products.Products)
....
Dim p As New Products.Products.Products(...<long arg list> )
ProdList.Add(p)
...
TextBox.Text = ProdList(DesiredProd).ProductName