[RESOLVED] Question about ArrayList
I have an arrayList and I use a function to find some object in that list.
VB Code:
Private Function findObject(ByVal someValue As String) As myClass
For Each i As myClass In Me.myList
If aCondition Then
Return i
End If
Next
Return Nothing
End Function
I use this function as follows:
VB Code:
Dim myObject As myClass
myObject = Me.findObject(someValue)
... (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.
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. :)
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.
Re: Question about ArrayList
can you unabstract the code a little? Like, what is aCondition?
Re: Question about ArrayList
I compare two Strings: the one given as a parameter with a string in the Class...
Re: Question about ArrayList
and MyList only contains objects of type MyClass?
Re: Question about ArrayList
Yes, I forgot to mention that, but I see you got that...
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:
Private MyList As New ArrayList
Private Class MyClassObj
Private _str As String
Private _int As Int32
Public Sub New(ByVal str As String, ByVal int As Int32)
_str = str
_int = int
End Sub
Friend Property MyStr() As String
Get
Return _str
End Get
Set(ByVal Value As String)
_str = Value
End Set
End Property
Friend Property MyInt() As Int32
Get
Return _int
End Get
Set(ByVal Value As Int32)
_int = Value
End Set
End Property
End Class
Private Function findObject(ByVal someValue As String) As MyClassObj
For Each i As MyClassObj In Me.MyList
If i.MyStr = someValue Then
Return i
End If
Next
Return Nothing
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim NewObj As MyClassObj
MyList.Add(New MyClassObj("Banana", 1))
MyList.Add(New MyClassObj("Terecotta", 2))
MyList.Add(New MyClassObj("Pie", 3))
NewObj = findObject("Banana")
NewObj.MyStr = "Cheese Whiz"
MessageBox.Show(DirectCast(MyList.Item(0), MyClassObj).MyStr)
end sub
Re: Question about ArrayList
Re: [RESOLVED] Question about ArrayList
Last question: if you do it like this:
VB Code:
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.
Re: [RESOLVED] Question about ArrayList
There's probably a nicer way, but...
VB Code:
Private MyList As New ArrayList
Private Class MyClassObj
Implements ICloneable
Private _str As String
Private _int As Int32
Public Sub New(ByVal str As String, ByVal int As Int32)
_str = str
_int = int
End Sub
Friend Property MyStr() As String
Get
Return _str
End Get
Set(ByVal Value As String)
_str = Value
End Set
End Property
Friend Property MyInt() As Int32
Get
Return _int
End Get
Set(ByVal Value As Int32)
_int = Value
End Set
End Property
Public Function Clone() As Object Implements System.ICloneable.Clone
Return Me.MemberwiseClone
End Function
End Class
Private Function findObject(ByVal someValue As String) As MyClassObj
For Each i As MyClassObj In Me.MyList
If i.MyStr = someValue Then
Return i
End If
Next
Return Nothing
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim NewObj As MyClassObj
MyList.Add(New MyClassObj("Banana", 1))
MyList.Add(New MyClassObj("Terecotta", 2))
MyList.Add(New MyClassObj("Pie", 3))
NewObj = DirectCast(findObject("Banana").Clone, MyClassObj)
NewObj.MyStr = "Cheese Whiz"
MessageBox.Show(DirectCast(MyList.Item(0), MyClassObj).MyStr)
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim NewObj As MyClassObj
MyList.Add(New MyClassObj("Banana", 1))
MyList.Add(New MyClassObj("Terecotta", 2))
MyList.Add(New MyClassObj("Pie", 3))
NewObj = findObject("Banana")
NewObj.MyStr = "Cheese Whiz"
MessageBox.Show(DirectCast(MyList.Item(0), MyClassObj).MyStr)
end sub
This is what you gave me and it works awesome. The problem is if I change this line:
VB Code:
NewObj.MyStr = "Cheese Whiz"
with this one:
VB Code:
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...
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.
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim NewObj As MyClassObj
MyList.Add(New MyClassObj("Banana", 1))
MyList.Add(New MyClassObj("Terecotta", 2))
MyList.Add(New MyClassObj("Pie", 3))
NewObj = findObject("Banana")
NewObj.MyStr = "Cheese Whiz"
MessageBox.Show(DirectCast(MyList.Item(0), MyClassObj).MyStr)
end sub
This is what you gave me and it works awesome. The problem is if I change this line:
VB Code:
NewObj.MyStr = "Cheese Whiz"
with this one:
VB Code:
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? :-/