473,386 Members | 1,795 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,386 software developers and data experts.

Looking for NameOf() type of functionality.

Hi,

I'm looking some way to get the name of a specific property of an object.

For example instead of having to specify the property name as a literal like
this:
MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, "MyProperty");
I would like to be able to look up the property name by doing somehing like
this:
MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, NameOf(obj.MyProperty));

The NameOf() method (which doesn't exist) is what I'm looking for.

The main reason I don't want to use literals is to reduce bugs if a
property's name is changed.

Thanks,
Erik
Nov 17 '05 #1
3 1169
ESPNSTI wrote:
Hi,

I'm looking some way to get the name of a specific property of an object.

For example instead of having to specify the property name as a literal like
this:
MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, "MyProperty");
I would like to be able to look up the property name by doing somehing like
this:
MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, NameOf(obj.MyProperty));

The NameOf() method (which doesn't exist) is what I'm looking for.

The main reason I don't want to use literals is to reduce bugs if a
property's name is changed.

Thanks,
Erik

ermm
Dont change the property name without fully refactoring all your code
(and anyone else's that depends on yours)?

You could do this with reflection but what would be the point?
(reflection is slow)
You would have to have some way to indicate which property you wanted
and then what?

What does your dosomethingwithaproperty method do?
Maybe an interface would be appropriate?

JB
Puzzled
Nov 17 '05 #2
ESPNSTI wrote:
For example instead of having to specify the property name as a literal
like this:

DoSomethingWithAProperty(obj, "MyProperty");

I would like to be able to look up the property name by doing somehing
like this:

DoSomethingWithAProperty(obj, NameOf(obj.MyProperty));

The NameOf() method (which doesn't exist) is what I'm looking for.


We are too (so far without success).

Everywhere in reflection where you want to access a specific member or
field, you have to identify it with a string. Some keyword, like NameOf(),
would be nice to have. I hate it when things aren't checked for errors at
compile time.

Max

Nov 17 '05 #3
Hi,

I don't think that that is a good idea, you should not change the interface
of a class, if so you can break any code that use it.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"ESPNSTI" <ES*********@Hotmail.com> wrote in message
news:el**************@TK2MSFTNGP14.phx.gbl...
Hi,

I'm looking some way to get the name of a specific property of an object.

For example instead of having to specify the property name as a literal
like
this:
MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, "MyProperty");
I would like to be able to look up the property name by doing somehing
like
this:
MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, NameOf(obj.MyProperty));

The NameOf() method (which doesn't exist) is what I'm looking for.

The main reason I don't want to use literals is to reduce bugs if a
property's name is changed.

Thanks,
Erik

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mosfet | last post by:
Hi I would like to write a template method, something like : template <typename Type> int MyClass::_Foo(vector<Type> MyVec) { if (int type) ..... if (long type)
0
by: Vaughn | last post by:
Hi, I'm looking for a reliable Web Hoster that has full Dot Net functionality. I was thinking of iPowerWeb.com but I wanted to check with you guys first. I'm basically looking for something that's...
17
by: Jedrzej Miadowicz | last post by:
I recently (re)discovered data binding in Windows Forms thanks to its advances in Visual Studio 2005. As I looked a little deeper, however, I realize that it still suffers from an irksome tendency...
14
by: Ron M. Newman | last post by:
Hi group, I need similar functionality I'm getting from the hashtable, but without the value .all I need is keys. this of course negates the need for "hashing", but I wanted to know if there's a...
1
by: JDeats | last post by:
I'm doing Windows Mobile 5 programming and since MS has not exposed most of the functionality i have to call DllImport a lot, I haven't done much interop with unmanaged code, I'm looking for the...
2
by: Jason | last post by:
Greetings, When my data is updated in my sql server database there are a number of jobs that my current process performs that analyzes the new data. This only happens once a day very early in...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
3
by: cutecutemouse | last post by:
---------------------------This is my task:-------------------------------------- There are hundred of similar classes and a large file(in processing) with thousand of instances of these classes....
2
by: cowznofsky | last post by:
My needs are relatively simple. I'm putting together a web site prototype that has some very minimal e-commerce type stuff. I've been away from asp.net for a little while. It's not a store, so...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.