473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 14622
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: tgh003 | last post by:
anyone know of a english dictionary component I can use to retrieve word definitions from? So something like: http://dictionary.reference.com/search?q=test but it either sends back XML or...
15
by: Andy C | last post by:
I am new to python, so please bear with me if I am making some conceptual error. Basically I want to create a graph with an adjacency list representation, but I don't want any of the adjacency...
14
by: Arik Funke | last post by:
Hi together, following code does compile fine on MS VC but not on gcc. What am I doing wrong? Is there a universal coding standard? Cheers, Arik --- class abc {
4
by: Betina Andersen | last post by:
I have a dictionary object, then I create a new dictionary object and sets it equal to my original, then I pass the new dictionary object to a function that changes some of my values - but then my...
7
by: bonk | last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and often in a foreach loop. While I am within one of the foreach loops the other threads must not modify the collection itself...
70
by: jojoba | last post by:
Hello! Does anyone know how to find the name of a python data type. Conside a dictionary: Banana = {} Then, how do i ask python for a string representing the name of the above dictionary...
7
by: noro | last post by:
Is it possible to do the following: for a certain class: ---------------------------- class C: def func1(self): pass def func2(self):
2
by: Steve | last post by:
Kind of a strange question... I have a VB.NET 2.0 solution containing a main project (my EXE) and a number of other projects (class DLLs) that are "plug-ins" to the main app. These plugins get...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.