|
-
Apr 9th, 2006, 03:34 PM
#1
Thread Starter
Hyperactive Member
searching a collection of objects?
How can I search a collection full of objects to find 1 or more objects with a certain property that matches what was given? So far I have this code already written but the red line doesn't work
VB Code:
Friend Function Find(ByVal strMotorID As String)
Dim intCounter, intCollectionSize As Integer
intCollectionSize = Me.Count
Do While intCounter <= intCollectionSize
[COLOR=Red]If Me.List.Item(intCounter).MotorID = strMotorID Then[/COLOR]
intCounter += 1
End If
Loop
motorID is the property I'm looking at and intCounter is the index of the items added to the collection
I tried to end process on Visual Studio 2005
but PETA stopped me saying it's smart enough
to be a living creature 
-
Apr 9th, 2006, 06:34 PM
#2
Re: searching a collection of objects?
Usually Functions Dont refer to items outside of the function, because then you cant just copy and paste into another form and use them. You can modify it to accept the Listbox (or whatever you are using) as a parameter of the function, then pass the listbox in and use that inside of the function. That would make it easily reusable.
In regards to the question, you would just cast the item in the list into the type that you are using with DirectCast. Something like...
VB Code:
'replace MyType with whatever the objects are that are in the list...
If DirectCast(List.Item(intCounter), MyType).MotorID = strMotorID Then
-
Apr 9th, 2006, 08:22 PM
#3
Thread Starter
Hyperactive Member
Re: searching a collection of objects?
awesome! I can't quite do it that way cuz the listbox only hold a string of a number, space, -, space, then the motor ID but I think I can strip off the first part What's the purpose of the directcast thingy? I looked it up in the MSDN library and it confused me further as usual. How can you convert it without giving a type to convert it to? And what it is converting and why?
I tried to end process on Visual Studio 2005
but PETA stopped me saying it's smart enough
to be a living creature 
-
Apr 9th, 2006, 08:31 PM
#4
Re: searching a collection of objects?
Well are you just storing a string in the list? Or an actual object? I was assuming this was some object with a MotorID property. If this is just a string, then just using "Me.List.Item(intCounter).ToString" or something to that effect should work.
In regards to the directcast, the Items collection is of the general Object type, so you can essentially hold any assortment of object in it. The directcast is used when you know what type it is, and it casts it into the type specified (MyType), so you can then use the properties and methods of that specific object. You can also use CType instead of directcast, but directcast yields better performance, but directcast can only be used when you know the object is the type you are casting it to...
Last edited by gigemboy; Apr 9th, 2006 at 08:34 PM.
-
Apr 9th, 2006, 08:56 PM
#5
Thread Starter
Hyperactive Member
Re: searching a collection of objects?
ohhhh that's cool. Well I was dumb and assumed list meant listbox so ignore everything I wrote. What's really going on is "me" is a collection class and it's full of "motors" objects. The motors objects have 5 properties, one of which is MotorID. I need the function to "Load the property information about a motor given the MotorID" It would be fine if it returned just the index of whatever motor object has the matching motorID property because I have a listbox that lists all the motorID's. Oh and I might as well ask, what's the command to say if the listbox's 5th item is highlighted then do whatever?
I tried to end process on Visual Studio 2005
but PETA stopped me saying it's smart enough
to be a living creature 
-
Apr 9th, 2006, 08:59 PM
#6
Re: searching a collection of objects?
As I was saying, you specify the type of the object in DirectCast.. whatever your "motor object" type is...
VB Code:
If DirectCast(Me.List.Item(intCounter), TheMotorType).MotorID = strMotorID Then
You would replace "TheMotorType" with whatever type or class that you have defined for that object... then you should get your intellisense dropdown in the IDE once you add the first "period" right before I had specifed the MotorID above, which lists all of the properties and methods for "TheMotorType"...
-
Apr 9th, 2006, 10:58 PM
#7
Thread Starter
Hyperactive Member
Re: searching a collection of objects?
aha! well that works I shouldda tested it first, lol. I have it set up as:
If DirectCast(Me.List.Item(intCounter), Motors).MotorID = strMotorID Then
but Me.List.Item(intCounter) should already be a motors type object, why do I have to convert it to a motors object?
I tried to end process on Visual Studio 2005
but PETA stopped me saying it's smart enough
to be a living creature 
-
Apr 9th, 2006, 11:11 PM
#8
Re: searching a collection of objects?
because the Items collection is a general Object type, and the general Object type does not have a "MotorID" property. Since it is of a generic object type, you can really add any object you want to it. Using DirectCast allows you to "cast" the object into the specific type that it is, allowing you to access that specific objects properties and methods. You arent converting the object, you are casting it, which is different.
Last edited by gigemboy; Apr 9th, 2006 at 11:15 PM.
-
Apr 9th, 2006, 11:21 PM
#9
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|