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.
Related
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:
When to use static vs instantiated classes
(10 answers)
How to decide between C# static and non-static methods?
(11 answers)
Should I go with static methods or non static methods?
(1 answer)
Static vs non-static class members
(7 answers)
Closed 5 years ago.
I am working on one of my projects in MVC. I have created a common class which contains common methods.
To access all methods without instantiating i have created static methods. It saved creating instance of class all the time.
Is it best practice? Can i have explaining scenarios for both uses?
This question already has answers here:
Use of var keyword in C#
(86 answers)
Closed 6 years ago.
If I use a IoC container and the strong implementation is instantiated at runtime, dunamically, which among the below initialization is a best practice?
var obj=FooIocContainer.Resolve<IInterface>();
or
IInterface obj=FooIoCContainer.Resolve<IInterface>();
Which one is better and why? Just being curious :)
It depends on the scope of the object you create. If its local then var will do. If you need to expose it using public properties, use specific Interface types.
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:
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.