Need "get" accessor to return property name

G

Guest

Hello,
I've got a simple shared property, e.g.

Public Class dbObject
Private Const m_ID As String = "ID"
Public Shared ReadOnly Property ID() As String
Get
Return m_ID
End Get
End Property
End Class

Rather than use the private variable m_ID, I'd rather just return the name
of the property as a string, e.g. return me.PropertyName, to save on code and
typos. But, "me" is not valid since this is not an instance (it's shared),
and I can't figure out the syntax to do what I know is possible via
reflection. Any thoughts on how to get a string for the name of a shared
property within the get accessor?

Thanks,
Bill
 
K

Kevin Spencer

Hi Bill,

Watch out for that word "Simple!" What you have is not simple; in fact, it
shouldn't even compile. You can't reference an instance member from a static
(shared) member.

But you don't want to do that. Just keep that in mind for future reference.

Now, I may be missing something, but how about:

Public Shared ReadOnly Property ID() As String
Get
Return "ID"
End Get
End Property

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
G

Guest

That works, it's just that I need to type the property name and the string
the same for the several hundred of these that I'll do, and thought I could
eliminate some code and reduce errors by making them the same "by
definition". I'm going to keep poking around at it, because I know the system
knows which get accessor I'm in, and therefore must know the name of the
property (seems even easier if it's static). Anyway, not my biggest problem
at the moment; all I'm trying to do is create a shared class that has my db
column names, to give me some intellisense in my loosely-typed system. Thanks
for the help.
 
K

Kevin Spencer

Hi Bill,

The problem with using Reflection to do this is using Reflection to do this.
;-)

Reflection can be pretty darned expensive.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
G

Guest

Agreed that it's not worth it in this case. I guess I'll just have to
proofread better and test for a change. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top