Results 1 to 14 of 14

Thread: [RESOLVED] Question about ArrayList

  1. #1

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Resolved [RESOLVED] Question about ArrayList

    I have an arrayList and I use a function to find some object in that list.
    VB Code:
    1. Private Function findObject(ByVal someValue As String) As myClass
    2.         For Each i As myClass In Me.myList
    3.             If aCondition Then
    4.                 Return i
    5.             End If
    6.         Next
    7.         Return Nothing
    8.     End Function
    I use this function as follows:
    VB Code:
    1. Dim myObject As myClass
    2.         myObject = Me.findObject(someValue)
    3.         ... (here I change myObject)
    The problem is I expected the code above to change the object in the list (because I didn't create another myObject using new), but it doesn't. I know I could delete the object in the list and then insert it again with the new value, but I was wondering if there is a way to access directly the object in the list.
    Thank you for any advice.
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Question about ArrayList

    Something like this should work for you:

    return DirectCast(i, MyClass)

    Make sure you have option strict on, that will help you avoid these complications in the future.

    Also, you could use the indexof method instead of that function. You might have a reason for using it, but I thoguht I would throw it out there.

  3. #3

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Re: Question about ArrayList

    Quote Originally Posted by sevenhalo
    Something like this should work for you:

    return DirectCast(i, MyClass)

    Make sure you have option strict on, that will help you avoid these complications in the future.

    Also, you could use the indexof method instead of that function. You might have a reason for using it, but I thoguht I would throw it out there.
    That still doesn't work... I have read this about DirectCast, but I don't really understand what's the difference between this and CType. Thank you for the option strict advice, I didn't even knew about it...
    As for indexOf method, I have some reasons and I need to use that function... Thank you.
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

  4. #4
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Question about ArrayList

    can you unabstract the code a little? Like, what is aCondition?

  5. #5

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Re: Question about ArrayList

    I compare two Strings: the one given as a parameter with a string in the Class...
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

  6. #6
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Question about ArrayList

    and MyList only contains objects of type MyClass?

  7. #7

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Re: Question about ArrayList

    Yes, I forgot to mention that, but I see you got that...
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

  8. #8
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Question about ArrayList

    Ok, I tossed this together for you. It's in 1.1, but it should work almost tick for tick:
    VB Code:
    1. Private MyList As New ArrayList
    2.  
    3.     Private Class MyClassObj
    4.         Private _str As String
    5.         Private _int As Int32
    6.  
    7.         Public Sub New(ByVal str As String, ByVal int As Int32)
    8.             _str = str
    9.             _int = int
    10.         End Sub
    11.  
    12.         Friend Property MyStr() As String
    13.             Get
    14.                 Return _str
    15.             End Get
    16.             Set(ByVal Value As String)
    17.                 _str = Value
    18.             End Set
    19.         End Property
    20.  
    21.         Friend Property MyInt() As Int32
    22.             Get
    23.                 Return _int
    24.             End Get
    25.             Set(ByVal Value As Int32)
    26.                 _int = Value
    27.             End Set
    28.         End Property
    29.     End Class
    30.  
    31.     Private Function findObject(ByVal someValue As String) As MyClassObj
    32.         For Each i As MyClassObj In Me.MyList
    33.             If i.MyStr = someValue Then
    34.                 Return i
    35.             End If
    36.         Next
    37.         Return Nothing
    38.     End Function
    39.  
    40.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    41.         Dim NewObj As MyClassObj
    42.  
    43.         MyList.Add(New MyClassObj("Banana", 1))
    44.         MyList.Add(New MyClassObj("Terecotta", 2))
    45.         MyList.Add(New MyClassObj("Pie", 3))
    46.  
    47.         NewObj = findObject("Banana")
    48.         NewObj.MyStr = "Cheese Whiz"
    49.  
    50.         MessageBox.Show(DirectCast(MyList.Item(0), MyClassObj).MyStr)
    51.    end sub

  9. #9

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Re: Question about ArrayList

    Nice... Thanks.
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

  10. #10

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Re: [RESOLVED] Question about ArrayList

    Last question: if you do it like this:
    VB Code:
    1. NewObj = findObject("Banana")
    and after that you change something to the NewObject, the changes will reflect to the ArrayList (which is just what I want to do). But just for improving my programming skills, how would you do this if you just want to find the object in the list, make some changes to it, but not to the list? Thank you.
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

  11. #11
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] Question about ArrayList

    There's probably a nicer way, but...
    VB Code:
    1. Private MyList As New ArrayList
    2.  
    3.     Private Class MyClassObj
    4.         Implements ICloneable
    5.  
    6.         Private _str As String
    7.         Private _int As Int32
    8.  
    9.         Public Sub New(ByVal str As String, ByVal int As Int32)
    10.             _str = str
    11.             _int = int
    12.         End Sub
    13.  
    14.         Friend Property MyStr() As String
    15.             Get
    16.                 Return _str
    17.             End Get
    18.             Set(ByVal Value As String)
    19.                 _str = Value
    20.             End Set
    21.         End Property
    22.  
    23.         Friend Property MyInt() As Int32
    24.             Get
    25.                 Return _int
    26.             End Get
    27.             Set(ByVal Value As Int32)
    28.                 _int = Value
    29.             End Set
    30.         End Property
    31.  
    32.         Public Function Clone() As Object Implements System.ICloneable.Clone
    33.             Return Me.MemberwiseClone
    34.         End Function
    35.     End Class
    36.  
    37.     Private Function findObject(ByVal someValue As String) As MyClassObj
    38.         For Each i As MyClassObj In Me.MyList
    39.             If i.MyStr = someValue Then
    40.                 Return i
    41.             End If
    42.         Next
    43.         Return Nothing
    44.     End Function
    45.  
    46.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    47.         Dim NewObj As MyClassObj
    48.  
    49.         MyList.Add(New MyClassObj("Banana", 1))
    50.         MyList.Add(New MyClassObj("Terecotta", 2))
    51.         MyList.Add(New MyClassObj("Pie", 3))
    52.  
    53.         NewObj = DirectCast(findObject("Banana").Clone, MyClassObj)
    54.         NewObj.MyStr = "Cheese Whiz"
    55.  
    56.         MessageBox.Show(DirectCast(MyList.Item(0), MyClassObj).MyStr)

  12. #12

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Re: [RESOLVED] Question about ArrayList

    Oka, maybe I shouldn't have started another thread for this, but I thought this thread is already marked as resolved, so...
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim NewObj As MyClassObj
    3.  
    4.         MyList.Add(New MyClassObj("Banana", 1))
    5.         MyList.Add(New MyClassObj("Terecotta", 2))
    6.         MyList.Add(New MyClassObj("Pie", 3))
    7.  
    8.         NewObj = findObject("Banana")
    9.         NewObj.MyStr = "Cheese Whiz"
    10.  
    11.         MessageBox.Show(DirectCast(MyList.Item(0), MyClassObj).MyStr)
    12.    end sub
    This is what you gave me and it works awesome. The problem is if I change this line:
    VB Code:
    1. NewObj.MyStr = "Cheese Whiz"
    with this one:
    VB Code:
    1. NewObj = someOtherObj.Clone
    it won't work any more... Do you have any ideas? It seems to me it's basically the same thing, if I call the Clone method...
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

  13. #13
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] Question about ArrayList

    clone returns an object, always... You have to directcast the object to the type:

    someotherobject = DirectCast(NewObj.Clone, MyClassObj)

    The first snippet you posted is changing a property of the object. The second example is creating a completely new copy of the object. It's really different.

  14. #14

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Re: [RESOLVED] Question about ArrayList

    Quote Originally Posted by zahadumy
    Oka, maybe I shouldn't have started another thread for this, but I thought this thread is already marked as resolved, so...
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim NewObj As MyClassObj
    3.  
    4.         MyList.Add(New MyClassObj("Banana", 1))
    5.         MyList.Add(New MyClassObj("Terecotta", 2))
    6.         MyList.Add(New MyClassObj("Pie", 3))
    7.  
    8.         NewObj = findObject("Banana")
    9.         NewObj.MyStr = "Cheese Whiz"
    10.  
    11.         MessageBox.Show(DirectCast(MyList.Item(0), MyClassObj).MyStr)
    12.    end sub
    This is what you gave me and it works awesome. The problem is if I change this line:
    VB Code:
    1. NewObj.MyStr = "Cheese Whiz"
    with this one:
    VB Code:
    1. NewObj = someOtherObj.Clone
    it won't work any more... Do you have any ideas? It seems to me it's basically the same thing, if I call the Clone method...
    When I said it won't work I meant it changes the object, but it doesn't change the list. That CType I think doesn't change anything, because VB does it implicitly, but I'm not sure though...

    *** edit ***
    Yes, you were right... I put the conversion there and forgot about it... But the question is still why doesn't it change the list? :-/
    Last edited by zahadumy; Apr 19th, 2006 at 11:23 AM.
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width