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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
Luis PérezSoftware Architect in .NetCommented:
I'm glad to see that it works now.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.