Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

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..
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to make a chunk loader in c# 
Csharp :: f sharp global variable 
Csharp :: c sharp xml prettier 
Csharp :: unity mathf.clamp 
Csharp :: c# null accessor 
Csharp :: c# get program version 
Csharp :: c# list find null 
Csharp :: read only variable in c# 
Csharp :: erewt 
Csharp :: how to hide cell in epplus 
Csharp :: c# entity mvc get user from razor layout 
Csharp :: how many zeros in quinnonagintillion 
Csharp :: actual vector3 object unity 
Csharp :: close an open form when you open it again c# 
Csharp :: parent to children nextJs 
Csharp :: async method out parameter c# 
Csharp :: drop column with code first asp.net core 
Csharp :: UnityEngine.Mesh:get_vertices() 
Csharp :: c# does readonly improve performance 
Csharp :: jtoken null or exists c# 
Csharp :: f# print array strings 
Csharp :: how to write a ello world program in c# 
Csharp :: how to call void unity 
Csharp :: blazor data annotation diaply name 
Csharp :: unity check if animator has parameter 
Csharp :: Xamarin Forms iOS Picker done 
Csharp :: add css class based on model value razor 
Csharp :: hardcode dropdown cshtml 
Csharp :: c# order by descending on 2 values 
Csharp :: unity dictionary foreach remove 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =