Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

in my example i have a datagrid and some pictureboxes. In the datagrid you can leave your name. With your name, an radiobutton gets generated. If you active your radiobutton you can "bet" on a picturebox (picturebox is moving), which one would be first on the finishline.

Now i had this example solved with arrays, but now with lists(pictureboxes and players are in a list), i dont know how to tell, ok, this radiobutton is active, so its player "jim" who wants to bet on "picbox2".
But i solved this, not on my own. Thats the problem, because i dont really understand what this code means

C#
string name = ((RadioButton)sender).Name;
Player pl = nPlayer.FirstOrDefault(p => p.Name == name);


But this just canges a label.text into the playersname.

For this solution i have:
Class:

-Pictureboox,
-Player,
-Bet

the key funktion is a Method in the class "BET" -> Betnow()
the meth just creates a new bet, and knows on wich dog and from wich player the bet is generated.

But i cant now just tell, like i imagine: nplayer.Betnow(BettingDog, nPlayer, amount)


I have recieved some help from a friend, i know that i have to use a sort of called "Lamda" but im not sure, how this works.

Thank you

Greets Niko

ps.: let me know if you need more expressions! or sample code
Posted
Updated 4-Nov-12 21:24pm
v2
Comments
lukeer 5-Nov-12 2:55am    
From the user's point of view, lists are quite similar to arrays. In which respect does your array-based solution not work for the list-based one?

1 solution

Arrays and lists are very similar - certainly you can access a list by an index in the same way that you access an array:
XML
List<string> list = new List<string>();
list.Add("hello");
list.Add("goodbye");
list.Add("merry christmas");
Console.WriteLine(list[1]);
will compile fine, and will print "goodbye".

So whatever code you used for your arrays, will probably work file with the list.
Without knowing a lot more about what you are doing, and how you are doing it, we can't tell you anything else useful!
 
Share this answer
 
Comments
niko_tells 5-Nov-12 3:25am    
Question is now updated for more details
OriginalGriff 5-Nov-12 3:35am    
The code you have provided will work with anything that supports IEnumerable - so arrays and lists will both work exactly the same.
What the code does is look at the sender parameter to the Event handler, and convert it to a Radiobutton, then get the Name of the button. It then uses a Linq method to look through your array or list, or whatever called nplayer, and return the first entry which has a matching Name property, as an instance of your Player class.
All you need to do is identify which dog he wants to bet on, and you are there.
And your code doesn't say how to do that, but it should be the same as the previous version.
Or am I looking at this wrong?
niko_tells 5-Nov-12 3:38am    
ah not really right, but close!

i solved to get the betting dog like that:
pl.BetNow(Wete, nHunde.FirstOrDefault(dog => dog.ID == Dog), pl, Bank);

everything is working, about dogs, i cant just identify the players.
OriginalGriff 5-Nov-12 3:50am    
You already have your player, don't you - the "pl" is your player, isn't it?
niko_tells 5-Nov-12 3:56am    
Ok looks like i have troubles expressing myself, sorry for that!

no i dont have my player, pl is a thought of my friend, and i dont understand why, i think just generated to find it by, like you said, linq

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900