Reputation: 3293
I need a class to store a mutable value. When an instance of this class is shared between different parts of code and one part changes the value inside that object, all the other parts see the new value.
This question isn't how I could write it. Here it is for anyone searching:
public class ValueHolder<T> { public T Value { get; set; } }
My question is, does this class exist anywhere in the standard library? This feels like something that would be included in the many small classes it provides.
Or, is there another way I could achieve my required outcome without littering my code with small classes?
Update/Commentary:
I'm a little bit surprised by the amount of negativity this question raised.
"This leads to a high WTF count!" "You are reinventing!"
Reinventing was something I explicitly set out to avoid in asking this question. If there was something in the standard library already, I could use that instead of writing a new class. (So long as the benefits outweighed the costs.)
The technique, passing an object and allowing it to be modified, is used all the time. Have you ever written a function that takes a List in order for that function to add/remove values? What about an event handler (such as Form.Closing) that allows the event handler to change the ongoing event by setting a value inside the event-args object?
In my case, the object was just a class with a single bool value in it. It gets passed into another piece of code in order to allow that flag to be raised or lowered. You might say that this should be a function's return value or a ref parameter, but I'm using a framework which allows for objects to be passed along to handlers (similar to ParameterizedThreadStart or the Tag properties) so I'm limited in what I can pass around.
"I wonder if the standard library already has a class that does this." I thought to myself. "Let's find out!"
Upvotes: 4
Views: 769
Reputation: 62054
Here is my VolatileRef
class.
I use it when I need to pass around a volatile variable by reference.
If you do not care for volatility, just get rid of the volatile
keyword and rename the class to just Ref
.
public class VolatileRef
{
private volatile object payload;
public VolatileRef( object payload )
{
this.payload = payload;
}
public object Payload { get => payload; set => payload = value; }
}
public class VolatileRef<T> : VolatileRef
{
public VolatileRef( T payload )
: base( payload )
{ }
public new T Payload { get => (T)base.Payload; set => base.Payload = value; }
}
Upvotes: -2
Reputation: 2210
To register and get objects anywhere on my project and at anytime on my app lifecycle, I use Lazy objects :
public static class ServiceContainer
{
static readonly Dictionary<Type, Lazy<object>> services = new Dictionary<Type, Lazy<object>>();
public static void Register<T>(Func<T> function)
{
services[typeof(T)] = new Lazy<object>(() => function());
}
public static T Resolve<T>()
{
return (T)Resolve(typeof(T));
}
public static object Resolve(Type type)
{
Lazy<object> lazy;
if (services.TryGetValue(type, out lazy))
return lazy.Value;
throw new Exception("Not found!");
}
}
For using :
public class Program
{
public void Main()
{
ServiceContainer.Register<IFoo>(() => new Foo()); /*register foo*/
}
public void UsingFoo()
{
var foo = ServiceContainer.Resolve<IFoo>(); /* do something with foo... */
}
}
You can find more about Lazy objects here :
Upvotes: -3