This question already has answers here:
How to trigger event when a variable's value is changed?
(6 answers)
Closed 9 years ago.
I'd like to have it so that there is a function that is called anytime a variable inside my class is changed. I know I could do this with get-set variables but if I'd prefer not to set that up for every single variable in my class is there a better way?
No there's no better way.
You can use tools like PostSharp to make it easier for you, but you'll need properties.
There is no such mechanism available. Writing to an instance variable is pretty much like directly writing some bytes at a given memory address, and there is no hook notifying you when that is done. That is exactly what properties with their getters and setters are there for.
Related
This question already has answers here:
NumericUpDown differentiate up and down
(2 answers)
Closed 9 months ago.
Is there a way to detect with the Numeric Up-down control to see if the Up or Down button is pressed without coding?
I know I could keep track of the current value and write some code in the method ValueChanged. I was just thinking maybe there is an easier/shorter way to do this
If you want to track specifically the buttons being used and not the value being changed directly you can create your own UserControl that inherits from NumericUpDown and override the UpButton() and DownButton() methods. Otherwise if you want to track all value changes, using the ValueChanged event is the best way to cover all ways of changing the value, including programatically.
This question already has answers here:
C# Reflection: Is it possible to find an instance of an object at runtime?
(7 answers)
Closed 3 years ago.
I have a class implementing an interface. Is there a way to tell if that class has been instantiated (via AppDomain maybe) and then get a reference to the object via the known interface?
I suppose this is related to dependency injection. Rather than having a library with registered objects, I'm looking for an alternative.
You could set a static variable in the class that gets set when the constructor is called.
get a reference to the object via the known interface
This makes it sound like you only expect to have one instance of this class. If so, check out the Singleton pattern.
This question already has answers here:
Static binding doesn't update when resource changes
(2 answers)
Static property using INotifyPropertyChanged. C#
(1 answer)
Closed 5 years ago.
I need help with a curious problem. I haven't managed to find anything similar on SO.
I have a static class used for logging in the system and now I have to bind the state of the log on the UI.
Is there a solution to this since implementing INotifyPropertyChanged forces one to use an instance member(which is an event in this case) and the class itself is static.
I would like to avoid retailoring the class to use singleton or any of those workarounds if it can be avoided.
This question already has answers here:
What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
(12 answers)
Closed 7 years ago.
Does anybody know how to modify a static variable from a different script in Unity?
Please, provide more information about how are you trying to do this...
If you need to change between mono behaviours, for example, you will need to get current instance of behaviour you like to change in a way like
myGameObjectInstance.GetComponent<MyBehaviourWithStatic>().myStatic = value;
but note that user2320445 answer is not wrong, it depends on your context. Be more specific on your question and we could provide better and precise answers
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there an actual difference in the 2 different ways of attaching event handlers in C#?
I've been seeing a lot of code that looks like this:
foo.Drop += new DragEventHandler(fooHandler);
But in the past, I've always done this:
foo.Drop += fooHandler;
Is there a difference between these two syntaxes? If so, is there any advantage to doing it the long way?
The second is shorthand for the first; they will compile to indentical IL.
However, the second syntax is new to C# 2.0; C# 1 only supports the first.
They will both result in the same IL.
So, in answer to your question, no - there is no benefit of using the longer version.
No difference , since .Net 2 and you can use what is called Method Group Conversion which allow you to Register the method name directly to the event without making a delegate Object
They are the same, but in the second example, the compiler uses Method Group conversion to infer the delegate type for you. Syntactic sugar...