变量不是类的成员

时间:2018-08-25 09:38:04

标签: asp.net .net

我正在运行一个循环,以将填充有产品的数组列表中的行添加到数据表中。我可以得到第一个对象项,但是以下项带来了以下问题:

  

错误BC30456'itemName'不是'Product'的成员

逐步解决该问题,我可以看到该数组确实包含我要显示的对象项。

Product class
public class Product
{
private int itemQuantity { get; set; }
public string itemID { get; set; }
private string itemName { get; set; }
private string itemCategory { get; set; }
private double itemPrice { get; set; }
private string itemDescription { get; set; }


public Product()
{
}

public Product(string ID, string name, string category, double price, string description)
{
    itemID = ID;
    itemName = name;
    itemCategory = category;
    itemPrice = price;
    itemDescription = description;
    itemQuantity = 1;
}
}

和我向数据表添加行的方法

Public Sub createShoppingCartTable()

    Dim tableColumnCode As System.Data.DataColumn = New System.Data.DataColumn
    With tableColumnCode
        .DataType = System.Type.GetType("System.String")
        .ColumnName = "ProductCode"
        .DefaultValue = "0"
    End With
    ShoppingCartTable.Columns.Add(tableColumnCode)

    Dim tableColumnName As System.Data.DataColumn = New System.Data.DataColumn
    With tableColumnName
        .DataType = System.Type.GetType("System.String")
        .ColumnName = "Name"
        .DefaultValue = "unknown"
    End With
    ShoppingCartTable.Columns.Add(tableColumnName)


    Dim tableColumnQuantity As System.Data.DataColumn = New System.Data.DataColumn
    With tableColumnQuantity
        .DataType = System.Type.GetType("System.String")
        .ColumnName = "Quantity"
        .DefaultValue = "1"
    End With
    ShoppingCartTable.Columns.Add(tableColumnQuantity)

    Dim tableColumnPrice As System.Data.DataColumn = New System.Data.DataColumn
    With tableColumnPrice
        .DataType = System.Type.GetType("System.Int32")
        .ColumnName = "Price"
        .DefaultValue = "0"
    End With
    ShoppingCartTable.Columns.Add(tableColumnPrice)

    Dim tableColumnTotalPrice As System.Data.DataColumn = New System.Data.DataColumn
    With tableColumnTotalPrice
        .DataType = System.Type.GetType("System.Int32")
        .ColumnName = "TotalPrice"
        .DefaultValue = "0"
    End With
    ShoppingCartTable.Columns.Add(tableColumnTotalPrice)


    'create rows with products added to cart
    Dim tableRow As System.Data.DataRow = ShoppingCartTable.NewRow()
    Dim intMaxRows As Integer = shoppingCart.Count - 1
    Dim i As Integer
    Dim localObj As New Product()

    For i = 0 To intMaxRows
        localObj = shoppingCart(i)
        tableRow = ShoppingCartTable.NewRow()
        tableRow("ProductCode") = localObj.itemID
        tableRow("Name") = localObj.itemName
        tableRow("Quantity") = localObj.itemQuantity
        tableRow("Price") = localObj.itemPrice
        tableRow("TotalPrice") = localObj.itemQuantity * localObj.itemPrice
        ShoppingCartTable.Rows.Add(tableRow)
    Next

    Me.dgDatagrid.DataSource = ShoppingCartTable
    Me.dgDatagrid.DataBind()
End Sub
End Class

我的解决方案正确显示了itemID,但是此后的所有操作均因上述错误而失败。

1 个答案:

答案 0 :(得分:0)

也许是访问修饰符(public / private)的问题。尝试将private类中的Product关键字更改为public