String representation of a property

  • Thread starter Thread starter chrisbarber1
  • Start date Start date
C

chrisbarber1

Can anyone help??
How can i get the string representation of a property?
For example if I have a Client object that has a FirstName property,
how in code can I get "FirstName" from Client.FirstName ?

Chris
 
Can anyone help??
How can i get the string representation of a property?
For example if I have a Client object that has a FirstName property,
how in code can I get "FirstName" from Client.FirstName ?

You can't. If you've got the value returned from Client.FirstName,
that's just a reference to a string. You could have got it in any
number of ways.

What problem are you actually trying to solve? There's probably a
better way.
 
You can't. If you've got the value returned from Client.FirstName,
that's just a reference to a string. You could have got it in any
number of ways.

What problem are you actually trying to solve? There's probably a
better way.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

You can't. If you've got the value returned from Client.FirstName,
that's just a reference to a string. You could have got it in any
number of ways.

What problem are you actually trying to solve? There's probably a
better way.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

Thanks for helping. My situation is this:


Public Sub test()
Dim desc As String = Description("Forname")
End Sub

Private Function Description(ByVal val As String) As String
' I know the class
Dim t As New T()
' create a property info for this property of the class
Dim pi As PropertyInfo = t.GetType.GetProperty(val)
' get my custom description attribute
Dim atts As DescriptionAttribute() =
DirectCast(pi.GetCustomAttributes(GetType(DescriptionAttribute),
False), DescriptionAttribute())
' return it
If atts IsNot Nothing Then Return atts(0).Description
Return "No Description"

End Function

What i desparately want to avoid is passing the string:
Description("Forname") but rather pass Client.Forename to the
function, which will realise Forename is the property to interigate.

Does this make sense? any ideas?
 
Thanks for helping. My situation is this:


Public Sub test()
Dim desc As String = Description("Forname")
End Sub

Private Function Description(ByVal val As String) As String
' I know the class
Dim t As New T()
' create a property info for this property of the class
Dim pi As PropertyInfo = t.GetType.GetProperty(val)
' get my custom description attribute
Dim atts As DescriptionAttribute() =
DirectCast(pi.GetCustomAttributes(GetType(DescriptionAttribute),
False), DescriptionAttribute())
' return it
If atts IsNot Nothing Then Return atts(0).Description
Return "No Description"

End Function

What i desparately want to avoid is passing the string:
Description("Forname") but rather pass Client.Forename to the
function, which will realise Forename is the property to interigate.

But you wouldn't be passing Client.Forename really - you'd be passing
the value which is just the result of evaluating Client.Forename.
Does this make sense? any ideas?

In C# there isn't an operator which would help you - although one has
been considered before (infoof) which hasn't made it to the top of the
design team's priority list. I *suspect* there isn't anything in VB
either, but I can't be sure.

I suspect you're best off creating a load of constants for the property
names, and write unit tests to make sure you haven't made any typos.
 
But you wouldn't be passing Client.Forename really - you'd be passing
the value which is just the result of evaluating Client.Forename.


In C# there isn't an operator which would help you - although one has
been considered before (infoof) which hasn't made it to the top of the
design team's priority list. I *suspect* there isn't anything in VB
either, but I can't be sure.

I suspect you're best off creating a load of constants for the property
names, and write unit tests to make sure you haven't made any typos.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

ok, thanks
 
Back
Top