469,647 Members | 1,789 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

Dictionary By Reference

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!

Aug 17 '06 #1
4 13441
Hi,

I'm not very clear of why you want this in the first place, but I can tell
you that you cannot do it. Only if the property is a reference type you
could do something like that.

Now a possible workaround could be if you instead of keeping a reference to
the property itself keep a reference to the instance containing the
property.

In the case that this is not possible (or desirable) you could keep a
reference to a PropertyInfo of that property.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<Nu********@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
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!

Aug 17 '06 #2
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
Aug 17 '06 #3
<Nu********@gmail.comwrote:
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.
You can't do that. Well, you can make the dictionary a map from a
string to a PropertyInfo, but that's the only way of doing it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 17 '06 #4
Let me first just say Thank You. That is by far the most thorough
response I've ever received from a posting.

I went forward with the approach of removing the private member and
having that relate back to the dictionary index. Before reading your
post I had begun implementing the PropertyInfo approach, but the other
way is much more straight forward (and I keep hitting myself in the
head saying 'Duh...why wasn't I doing that to begin with').

Thanks again for the excellent response!
Andreas Mueller wrote:
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
Aug 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by tgh003 | last post: by
14 posts views Thread by Arik Funke | last post: by
4 posts views Thread by Betina Andersen | last post: by
70 posts views Thread by jojoba | last post: by
2 posts views Thread by Steve | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.