Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# what is the point of using SET?

Tags:

c#

why do we do this:

 private string StatusText
 {
    set { toolStripStatusLabel1.Text = value; }
 }

instead of just this?

private string StatusText
{
   toolStripStatusLabel1.Text = value; 
}

i do not understand the point of using set?

like image 976
Alex Gordon Avatar asked Nov 27 '22 12:11

Alex Gordon


1 Answers

These are two completely different things.

This is a method:

private string StatusText()
{
    toolStripStatusLabel1.Text = value; 
}

which is called like this:

StatusText();

(and which will not compile, because the local variable value cannot be found). To make it work, you would need to write it like this:

private string StatusText(string value)
{
    toolStripStatusLabel1.Text = value; 
}

and call it like this:

StatusText("bla");

On the other hand, this is the definition of a property:

private string StatusText
{
    set { toolStripStatusLabel1.Text = value; }
}

whose setter (hence the keyword set) is called like this:

StatusText = "bla";
like image 168
Heinzi Avatar answered Dec 19 '22 02:12

Heinzi