Click to See Complete Forum and Search --> : finding value arraylist
dave2k
December 18th, 2005, 10:47 AM
say i have an arraylist of Player objects. Every player has a String* Name value.
how can i find out whether a Player element exists with the name "david"?
is there an easier way than iterating through the whole list?
cheers
NoHero
December 18th, 2005, 12:04 PM
You can use the method IndexOf of your arraylist, now you only need a comparision operator (as described here (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/vcmanagedextensionsspec_20_1.asp)) which supports a string and you are there...
dave2k
December 18th, 2005, 01:12 PM
cheers
i am new to c++/windows programming in general. i was wondering whether you could explain abit further how to do this?
cheers
dave2k
December 18th, 2005, 01:31 PM
do i need to use a loop with your method?
NoHero
December 18th, 2005, 03:34 PM
i am new to c++/windows programming in general. i was wondering whether you could explain abit further how to do this?
If you are really new to C++ programming I suggest you to stay on C++ and learn C# for dotNet purposes. C# is a lot easier and does not contain that much pitfalls as Managed C++ has. If you really want to learn Managed C++ you should go and grab a book and one for .NET. This will help you to learn the basics.
dave2k
December 18th, 2005, 03:43 PM
hi,
i am looking through a book right now - but it doesn't have an answer for what i am trying to do.
so do i need to iterate through the list?
NoHero
December 18th, 2005, 03:55 PM
so do i need to iterate through the list?
It's a way though. Although if my solution is much simplier.
dave2k
December 18th, 2005, 03:59 PM
Cheers No Hero, i know this is cheaky but i'd be really grateful if you could post some simple code. I have been trying to solve this for hours and am getting nowhere :(
so far i have come up with for(i = 0; i < this->player->Count; i++)
if(__try_cast<PokerPlayer *>(this->player[i])->Name == "david")
Console::WriteLine("in list");my PokerPlayer class:__gc class PokerPlayer {
public:
PokerPlayer(void);
__property String* get_Name() { return name; }
__property void set_Name(String*);
private:
String *name;
};But it ain't working :(
NoHero
December 18th, 2005, 04:16 PM
Which errors, exceptions do you get?
/me sleepy, sleepy... will take a look at it tomorrow
dave2k
December 19th, 2005, 06:29 AM
NoHero wake up :lol:
NoHero
December 19th, 2005, 09:35 AM
heh... Right there at your service...
using namespace System;
using namespace System::Collections;
__gc class Player
{
private:
String * name;
int points;
public:
Player ( void )
{
name = "";
points = 0;
}
Player ( String *name, int points )
{
this->name = name;
this->points = points;
}
~Player ( void )
{
}
// Getters and setters for name
//
__property String* get_Name ( void )
{
return this->name;
}
__property void set_Name ( String *value )
{
this->name = value;
}
// Getters and setters for points
//
__property int get_Points ( void )
{
return this->points;
}
__property void set_Points ( int value )
{
this->points = value;
}
// an override of Equals, it is called by IndexOf() on every object
// so we need to compare the strings here. Comparision is not case
// sensitiv. If you need it case sensitive change true to false
//
bool Equals ( Object *other )
{
if ( other->GetType() == __typeof(String) )
{ // if we have a string to compare we compare the names
return (String::Compare(this->Name, __try_cast<String*>(other), true) == 0);
}
// else call base method
return Object::Equals(other);
}
};
int main(String* args __gc[])
{
ArrayList *arr = new ArrayList;
arr->Add(new Player(S"David", 6));
arr->Add(new Player(S"Florian", 4));
arr->Add(new Player(S"Marius", 1));
arr->Add(new Player(S"Ovidiu", 10));
int found = arr->IndexOf(S"marius");
Console::WriteLine(S"Marius is on {0}", __box(found));
Console::ReadKey();
return 0;
}
Don't be irritated by ReadKey(): It's only available at .NET 2.0 and above.
dave2k
December 19th, 2005, 10:29 AM
:lol: cheers :lol:
dave2k
December 19th, 2005, 10:55 AM
btw why wont this compile under 'CLR application'?
cheers
NoHero
December 19th, 2005, 11:21 AM
Which compiler errors?
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.