FAQ: find variable in an array of objects
My brain's gone finishing this morning, so I need a bit of help. Let's say I have a class that looks like this:
Code :
Class Foo
{
int bar;
int mooose;
void Foo(int bar, int moose)
{
this.bar = bar;
this.moose = moose;
}
}
And then somewhere I create an array of objects, fooObj, of the type Foo and populate the variables of all objects in the array. How would I find which object has moose=42?
Re: FAQ: find variable in an array of objects
The simple answer is to create a getter method for moose and see if that value is 42. If it's not, look at the next fooObj to see if it's moose is 42.
Code :
public int getMoose()
{
return moose;
}
Note: If you have multiple fooObj's in the list with moose=42, this method will return the first one it finds. Depending on your application, this may or may not be what you want.