Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Class instance

Tags:

c#

class

events

I'm a beginner in programming and I've read several tutorials. I'm still unclear about the following:

When clicking on a button, that event creates an instance of a class:

private void button2_Click(object sender, RoutedEventArgs e)
{
    int a = 1;
    myClass test = new myClass(a);
}

myClass is doing a long processing job (several minutes). If I click 5 times on my button, is it gonna create 5 instances? Or is the "test" going to be "overwritten" 4 times?

Thanks

like image 998
snipef Avatar asked Apr 09 '26 18:04

snipef


1 Answers

If I click 5 times on my button, is it gonna create 5 instances ? Or the "test" instance will be "overwritten" 4 times ?

Yes its going to create 5 separate instances. You are creating an object that immediately falls out of scope after it is constructed, so the next time a different instance of the same class is constructed.

I assume you were planning to do the processing as part of your constructor, keep in mind this will block the UI thread, your program will "freeze" - if you are looking to do a long processing job, you shouldn't do it on the UI thread - look into i.e. the BackgroundWorker.

like image 133
BrokenGlass Avatar answered Apr 11 '26 08:04

BrokenGlass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!