I have string which I create like:
string label = Name + " " + Number;
where Name and Number are properties. I want this label string to change whenever the Name or Number is updated. I tried to use the ref keyword but C# tells me that I can’t use ref for properties. Is there a way to achieve this ?
Create it as another property, with only a
getmethod:This way, every time you call the property it will create the return value based on the current values of
NameandNumber.This needs to be defined at a class level though, and
Labelis probably not an appropriate name either.Of course, the question now is, why call it
Labelin the first place?If you are using this value to set a WinForms style label control, and you want to update that dynamically then you will need a different approach. you could modify your current properties for
NameandNumberto do a “little extra work” in the setters.For example:
This might be overkill for this question, but just something to think about.