Click here to Skip to main content
15,939,243 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am very new to, I used ArrayList in a project and I observed that arraylist overwrites the previous items in the arraylist.
<serializable()> _
Public Class benchPortalPara

    Public name As String = ""
    Public attribute As String = ""
    Public time As Double = 0.0
    Public CmdCnt As Integer = 0 'ToDo: to remove

    <(XmlArrayItem(Type:=GetType(benchPortalPara)), _
    'XmlArrayItem(Type:=GetType(CLS_BENCH_CMD_ALARM))> _
    'Public benchList As ArrayList = New ArrayList

    <XmlArrayItem(Type:=GetType(benchPortalPara))> _
    Public benchList As ArrayList

    Public Shared Instance As benchPortalPara = Nothing

    Public Shared Sub passCmdData(ByVal Cls_Cmd As CLS_BENCH_CMD_TIMING)
        If (Instance Is Nothing) Then
            Instance = New benchPortalPara
        End If = Cls_Cmd.cmdName
        Instance.attribute = Cls_Cmd.cmdDescription
        Instance.time = Cls_Cmd.duration
        Instance.CmdCnt += 1

        If (Instance.benchList Is Nothing) Then Instance.benchList = New ArrayList()
    End Sub
End Class

Everytime when I call passCmdData() routine, my previous arraylist data is overwritten.

Please suggest that what can go wrong?

Thanks in advance,

Updated 21-Feb-12 1:19am
Varun Sareen 21-Feb-12 7:19am    
edit for: naveed khowaja
naveed khowaja 22-Feb-12 4:40am    
Want to add something for your info, I am calling passCmdData() for 100 times. In this class I have declared my arraylist as "Public benchList As ArrayList".

do I need multi-dimentional arraylist for that? If so, how to implement it?

1 solution

Why does your class appear to hold an instance of itself, called Instance?
Why not use Me instead?
If (Me.benchList Is Nothing) Then Me.benchList = New ArrayList()
Share this answer
naveed khowaja 21-Feb-12 20:42pm    

Thanks for the reply. But when I use Me instead of "instance" then it gives me following error.

"Me is only valid within an instance method"
OriginalGriff 22-Feb-12 4:00am    
Then take off the "Shared" attribute.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900