Nu********@gmail.com wrote:
Hi folks,
I have a Dictionary which contains a string key and an object value. I
want the object value to point to a property in my class and I want it
to be by reference, so that later on I can change the value of the
property through the dictionary.
I am having difficulty making the value be by reference. Is this
possible? I've even tried unsuccessfully to use unsafe code with
pointers.
It's easier to understand what I'm looking for if you just look at the
below. Here's what I have:
private int _MyProp1 = -1;
public int MyProp1
{
get
{
return _MyProp1;
}
set
{
_MyProp1 = value;
}
}
public Dictionary<string, objectMyDict = new Dictionary<string,
object>();
//Note value is of type object because it could be an int property or
string property or...
public void AddPropsToDict()
{
MyProp = 1;
// Want below to take MyProp by Ref
MyDict.Add("propID", MyProp);
}
public void UpdateValues()
{
MyDict["propID"] = 3;
// Below Prints 3
Console.WriteLine(MyDict["propID"]);
// Below Prints 1....Want it to Print 3
Console.WriteLine(MyProp);
/*
// Alternative way does not work either
object oProp = MyDict["propID"];
oProp = 3
// Below Prints 3
Console.WriteLine(oProp);
// Below Prints 1....Want it to Print 3
Console.WriteLine(MyDict["propID"]);
// Below Prints 1....Want it to Print 3
Console.WriteLine(MyProp);
*/
}
Thanks!
Hi,
without knowing more details about what you want to achieve I can think
of 3 different approaches.
1) If your dictionary is used inside your class, you can store your
values inside the dictionary and let the property access the values of
the dictionary:
class Test1
{
private IDictionary<string, objectmyDict =
new Dictionary<string, object>();
public int MyProp
{
get
{
object o = MyDict["propID"];
if(o == null)
{
o = -1;
MyDict.Add("propID", o);
}
return (int)o;
}
set{ MyDict["propID"] = value; }
}
private IDictionary<string, objectMyDict
{
get { return myDict; }
}
public void UpdateValues()
{
MyDict["propID"] = 3;
Debug.Assert(MyProp == 3);
}
}
// ... Usage
Test1 t1 = new Test1();
t1.UpdateValues();
2) Use Reflection to access the properties in the same manner as a
function pointer map in C/C++:
class Test2
{
private IDictionary<string, PropertyInfomyDict =
new Dictionary<string, PropertyInfo>();
private int prop = -1;
public int MyProp
{
get { return prop; }
set { prop = value; }
}
private IDictionary<string, PropertyInfoMyDict
{
get { return myDict; }
}
public void AddPropsToDict()
{
MyProp = 1;
MyDict.Add("propID", GetType().GetProperty("MyProp"));
}
public void UpdateValues()
{
MyDict["propID"].SetValue(this, 3, null);;
Debug.Assert(MyProp == 3);
}
}
// ... Usage
Test2 t2 = new Test2();
t2.AddPropsToDict();
t2.UpdateValues();
3) Build a reference type integer that (almost) behaves like a normal
integer. By almost I mean that this class behaves like a primitive, but
is *not* immutable. This might be error prone, so if possible I would
recomend to hide it from the public interface.
class Integer
{
public Integer(int val)
{
IntValue = val;
}
private int val;
public static implicit operator int(Integer intRef)
{ return intRef.IntValue; }
public static implicit operator Integer(int i)
{ return new Integer(i); }
public static bool operator==(Integer lhs, Integer rhs)
{ return Equals(lhs, rhs); }
public static bool operator !=(Integer lhs, Integer rhs)
{return !Equals(lhs, rhs); }
internal int IntValue// This one is smells.
{
get { return val; }
set { val = value; }
}
public override bool Equals(object o)
{
if(o is int)
return val == (int) o;
Integer rhs = o as Integer;
return rhs != null
? IntValue.Equals(rhs.IntValue)
: false;
}
public override int GetHashCode()
{ return IntValue.GetHashCode(); }
}
class Test3
{
private IDictionary<string, objectmyDict = new
Dictionary<string, object>();
private Integer prop = -1;
public Integer MyProp
{
get { return prop; }
set { prop = value; }
}
private IDictionary<string, objectMyDict
{
get { return myDict; }
}
public void AddPropsToDict()
{
MyProp = 1;
MyDict.Add("propID", MyProp);
}
public void UpdateValues()
{
((Integer)MyDict["propID"]).IntValue = 3;
Debug.Assert(MyProp == 3);
}
}
Test3 t3 = new Test3();
t3.AddPropsToDict();
t3.UpdateValues();
HTH,
Andy