471,317 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Referencing Property as variable?

If I have a collection such as:
Person.addresscollection[0].Address1

and have a variable
myAddressField = "Address1"

how can I do something such as
Person.addresscollection[0].myAddressField

to reach the field "Address1"?

Thanks,
Brett

Jan 20 '06 #1
3 978
You cannot the way you have it, but you can use reflection to dynamically
investigate and invoke objects. Take a look at the Type.GetProperty( ... ).
You will most likly have to wrap whatever you are doing in some method as
there is not built-in syntax for this.
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
If I have a collection such as:
Person.addresscollection[0].Address1

and have a variable
myAddressField = "Address1"

how can I do something such as
Person.addresscollection[0].myAddressField

to reach the field "Address1"?

Thanks,
Brett

Jan 20 '06 #2
> If I have a collection such as:
Person.addresscollection[0].Address1
and have a variable
myAddressField = "Address1"
how can I do something such as
Person.addresscollection[0].myAddressField
to reach the field "Address1"?

Thanks,
Brett


public class Address
{
private string myAddressField;

...

public string Address1
{
get{ return myAddressField; }
}
}
Jan 20 '06 #3
Peter, I could try something with Type.GetPropery but that means
needType.DLL will need to include all the DLLs of every type it needs.

I have an EXE project that file references this needType.DLL and A.DLL,
B.DLL, C.DLL. Those last three DLLs contain the types I'd need to use
with GetProperty in needType.DLL. If I were to reference the three
DLLs in needType.DLL, I'd have a lot of reduncy and complexity.
There'd probably be circular references.

Thanks,
Brett

Jan 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by DC Gringo | last post: by
6 posts views Thread by martin | last post: by
3 posts views Thread by DC Gringo | last post: by
2 posts views Thread by HankD | last post: by
reply views Thread by rosydwin | last post: by

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.