ArrayList Being overwritten

I have this piece of code here

        'This validation is a custom validation to check for identical times
        Dim ser As JavaScriptSerializer = New JavaScriptSerializer()
        Dim dict As New Dictionary(Of String, Object)
        dict = ser.Deserialize(Of Dictionary(Of String, Object))(json)
        Dim StartTimeValueArray As New ArrayList
        Dim EndTimeValueArray As New ArrayList
        Util.JsonHelper._keyToSearch = "StartTime"
        Util.JsonHelper._strValue.Clear()
        Util.JsonHelper.DisplayDictionary(dict)
        'start time array
        If (Util.JsonHelper._strValue.Count > 0) Then
            'loop through the array and display
            StartTimeValueArray = Util.JsonHelper._strValue
        End If
        Util.JsonHelper._keyToSearch = "EndTime"
        Util.JsonHelper._strValue.Clear()
        Util.JsonHelper.DisplayDictionary(dict)
        'end time array
        If (Util.JsonHelper._strValue.Count > 0) Then
            EndTimeValueArray = Util.JsonHelper._strValue
        End If
        If StartTimeValueArray.Count <> EndTimeValueArray.Count Then
            'Start Time and End Time do not have equal array list values
            Return False
        Else
            For inx As Integer = 0 To StartTimeValueArray.Count - 1
                If Not (StartTimeValueArray(inx) Is EndTimeValueArray(inx)) Then
                    Return False
                End If
            Next
        End If
        Return True

Open in new window


after the second call to the  Util.JsonHelper.DisplayDictionary(dict) my first Array list  StartTimeValueArray is being overwritten by the values from the seocnd call.  I can't figure it out. Why is it doing that?
jonjojrAsked:
Who is Participating?
 
Luis PérezConnect With a Mentor Software Architect in .NetCommented:
I'm glad to see that it works now.
0
 
Luis PérezSoftware Architect in .NetCommented:
Obviously, becauso the Util.JsonHelper._strValue is a shared property that changes after your second call to Util.JsonHelper.DisplayDictionary(dict), so in fact your StartTimeValueArray and EndPointValueArray are really pointing to the same memory values.

Try to adding values in your StartTimeValueArray instead of directly value assigment. I mean:
Instead of:
StartTimeValueArray = Util.JsonHelper._strValue
Try this:
StartTimeValueArray.AddRange(Util.JsonHelper._strValue)

And, if it still doesn't work:
Instead of:
StartTimeValueArray = Util.JsonHelper._strValue
Try this:
For each value As Object In Util.JsonHelper._strValue
    StartTimeValueArray.Add(value)
Next

Hope that helps.
0
 
jonjojrAuthor Commented:
so a direct assignment is not going to work. what I did was removed the shared part of it and made it an object let me know if this code below makes sense and if that is the best way to do it.

       'This validation is a custom validation to check for identical times
        Dim ser As JavaScriptSerializer = New JavaScriptSerializer()
        Dim dict As New Dictionary(Of String, Object)
        dict = ser.Deserialize(Of Dictionary(Of String, Object))(json)
        Dim StartTimeValueArray As New ArrayList
        Dim EndTimeValueArray As New ArrayList
        Dim jsonUtilsStartTime As New Util.JsonHelper("StartTime")
        Dim jsonUtilsEndtime As New Util.JsonHelper("EndTime")
        StartTimeValueArray = jsonUtilsStartTime.DisplayDictionary(dict)
        EndTimeValueArray = jsonUtilsEndtime.DisplayDictionary(dict)
        If StartTimeValueArray.Count <> EndTimeValueArray.Count Then
            'Start Time and End Time do not have equal array list values
            Return False
        Else
            For inx As Integer = 0 To StartTimeValueArray.Count - 1
                If (StartTimeValueArray(inx) Is EndTimeValueArray(inx)) Then
                    Return False
                End If
            Next
        End If
        Return True

Open in new window


Thanks
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Luis PérezSoftware Architect in .NetCommented:
It seems right, but without seeing the inside of the DisplayDictionary method I cannot be sure. Try it and let's see if it works.
0
 
jonjojrAuthor Commented:
Here is the inside of DisplayDictionary. its no big secret:

        Public _strValue As New ArrayList()
        Private _strOutput As String
        Public _keyToSearch As String


        Public Function DisplayDictionary(ByVal dict As Dictionary(Of String, Object)) As ArrayList
            Dim keyFound As Boolean = False
            For Each strKey As String In dict.Keys
                If _keyToSearch = strKey Then
                    keyFound = True
                End If
                Dim o As Object = dict(strKey)
                If TypeOf o Is Dictionary(Of String, Object) Then
                    DisplayDictionary(DirectCast(o, Dictionary(Of String, Object)))
                ElseIf TypeOf o Is ArrayList Then
                    Dim inx As Integer = 0
                    For Each oChild As Object In DirectCast(o, ArrayList)
                        If TypeOf oChild Is String Then
                            _strOutput = DirectCast(oChild, String)
                            If keyFound And (strKey = _keyToSearch) Then
                                _strValue.Insert(_strValue.Count, _strOutput)
                                'Exit Sub
                            End If
                        ElseIf TypeOf oChild Is Dictionary(Of String, Object) Then
                            DisplayDictionary(DirectCast(oChild, Dictionary(Of String, Object)))
                        End If
                    Next
                Else
                    _strOutput = o.ToString()
                    If keyFound And (strKey = _keyToSearch) Then
                        _strValue.Insert(_strValue.Count, _strOutput)
                    End If
                End If
            Next

            Return _strValue
        End Function

Open in new window


What I wrote was how to parse the json string and get the value from the key that I pass in and return an array if there is more than one.

0
 
jonjojrAuthor Commented:
It works the way that I set it, but I also wrote another way using your method. I'll try that in another location that I need the same type of array.
0
 
jonjojrAuthor Commented:
I was doing something wrong, but thanks for all your help. I'm glad I got this work. Hopefully i'll have something more challenging in the future. LOL Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.