当我添加一个新的VB.NET时,ArrayList更改值

时间:2014-11-21 11:02:37

标签: vb.net arraylist

所以这是我的问题。我有一个产品类,我已经创建了该类的ArrayList。 我可以向该阵列添加许多新产品,但问题是该阵列将所有数组的值更改为我添加的最后一个。

产品类别:

Public Class Product
Dim cod_prod As String
Dim state As Boolean

Public Sub New(ByVal cod As String, ByVal est As Boolean)
    cod_prod = cod
    state = est
End Sub

Public Sub New()
    cod_prod = ""
    state = False
End Sub

Public Function get_cod_prod() As String
    Return cod_prod
End Function

Public Function get_state() As Boolean
    Return state
End Function

Public Sub set_cod_prod(ByVal cod As String)
    cod_prod = cod
End Sub

Public Sub set_state(ByVal est As Boolean)
    state = est
End Sub
End Class

这个类是我向ArrayList添加一个新产品。

 Dim array_prod As New ArrayList
 Dim nproducts As Integer = 0

 Public Sub add_prod(ByVal prod As Producto)
    array_prod.Add(prod)
    nproducts += 1
 End Sub

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

好的,在相关问题上再看一点我发现了这一点。 Retrieving data from a VB.NET arraylist of objects

所以我解决它改变这样的方法:

Public Sub add_prod(ByVal prod As Producto)
    Dim nprod As New Producto
    nprod.set_cod_prod(prod.get_cod_prod)
    nprod.set_state(prod.get_state)

    array_prod.Add(nprod)
    nproductos += 1
End Sub