I’m making a graphic control class Info which should display some text on screen. The text is some object’s string. I’d like to be able to get that object’s latest value from within an instance of Info class.
class Info
{
public string Text;
}
void Program()
{
ClassA obj = new ClassA();
obj.name = "Instance of ClassA";
Info wind1 = new Info();
wind1.Text = obj.name; // this just copies current value, but should be a reference or something
/* obj.name value changes several times before it's time to display it again */
// Info window drawing method
foreach (var item in Windows) // Windows is List<Info>
Draw(item.Text); // this doesn't get the latest value
}
How should I change the code so I can get the latest string value from within the drawing section?
Update: If you need something that’ll work for any type, you’ll have to use delegates. For example:
In this case,
Infois given an anonymous function that returns a string. When you access itsTextproperty, the function is evaluated to retrieve that string. How the string is retrieved, and where it comes from, is determined by the client code (i.e. theProgrammethod). This way,Infodoesn’t rely on any particular type.You could pass the
ClassAobject into yourInfoinstance, so that it can get the value of.nameitself.Something like this, perhaps?