Horje
c# button click gets assigned the last value Code Example
c# button click gets assigned the last value
List<object> asd = new List<object>();

for (int i = 0; i < asd.Count; i++)
{  
  	// Let's say you are trying to create buttons
  	// Because the memory address gets overwritten
  	// the button will always use the last value looped
  	Button btn = new Button() { Click += SomeMethod(asd[i]) };
 
  	// This is crucial as this will reference
  	// a new memory address.
	object obj = asd[i];
  
  	// So do it like this
  	Button btn = new Button() { Click += SomeMethod(obj)};
  
  	// do something..
}




Csharp

Related
C# multiple button click event to textbox Code Example C# multiple button click event to textbox Code Example
c# windows forms open directory in explorer Code Example c# windows forms open directory in explorer Code Example
universities in greece Code Example universities in greece Code Example
c# read large file Code Example c# read large file Code Example
how to split string into a list ignoring number of spaces c# Code Example how to split string into a list ignoring number of spaces c# Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7