C# how to find item which item is clicked in a list? -


i trying check on item have clicked, making kind of inventory, if clicked on 1w, texture change clicked texture. list have made. can't seem find code works.

this.drivers = new list<driver>()                 {                     new driver(this.game, this, new vector2 (0, 62), "1w", "images/clubs/drivers/1w", this.inventory),                     new driver(this.game, this, new vector2 (62, 62), "2w", "images/clubs/drivers/2w", this.inventory),                     new driver(this.game, this, new vector2 (124, 62), "3w", "images/clubs/drivers/3w", this.inventory),                     new driver(this.game, this, new vector2 (186, 62), "4w", "images/clubs/drivers/4w", this.inventory),                     new driver(this.game, this, new vector2 (248, 62), "5w", "images/clubs/drivers/5w", this.inventory),                     new driver(this.game, this, new vector2 (310, 62), "6w", "images/clubs/drivers/6w", this.inventory),                     new driver(this.game, this, new vector2 (0, 124), "7w", "images/clubs/drivers/7w", this.inventory)                 }; 

i know can find check name :

if ( name == "1w") {    //do } 

but mean, if want check 7 7 if statements, , have 3 of lists, 21 if statements.

i know possible in 1 sentence per list can't remember code!

i hope can me!

try using linq:

this.drivers.where(d => d.name == name).tolist().foreach(delegate(driver d) {     // }); 

if names unique, easier use:

var driver = this.drivers.single(d => d.name == name); // driver 

alternatively, can use loop:

foreach(var d in this.drivers) {     if(d.name == name)     {         //     } } 

if items always ordered "1w","2w",..., identify index:

var index = int.parse(name[0].tostring()) - 1; // e.g. "3w"[0].tostring() = "3" var driver = this.drivers[index]; // 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -