N
NullQwerty
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, object> MyDict = 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!
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, object> MyDict = 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!