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

String representation of a property

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
Aug 5 '08 #1
5 1465
<ch**********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 5 '08 #2

<ch**********@gmail.comwrote in message
news:f3**********************************@34g2000h sh.googlegroups.com...
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 ?
<http://www.devarticles.com/c/a/C-Sharp/Understanding-Properties-in-C-Sharp/>
<http://www.vbdotnetheaven.com/Uploadfile/rajeshvs/PropertiesInVbDotNet04192005060237AM/PropertiesInVbDotNet.aspx>

Aug 5 '08 #3
On Aug 5, 9:46*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
*<chrisbarb...@gmail.comwrote:
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.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
On Aug 5, 9:46 am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
<chrisbarb...@gmail.comwrote:
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.

--
Jon Skeet - <sk...@pobox.com>
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(Descript ionAttribute),
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?
Aug 5 '08 #4
<ch**********@gmail.comwrote:
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(Descript ionAttribute),
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 5 '08 #5
On Aug 5, 11:25*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
*<chrisbarb...@gmail.comwrote:
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(Descript ionAttribute),
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.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
ok, thanks
Aug 5 '08 #6

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

Similar topics

0
by: Hessam | last post by:
Hello, I am designing a .net custom control in VS.net 7.1 and my control exposes an array of strings which are supposed to be the items to show. To do this have declared a private string variable...
1
by: dpakpaul | last post by:
Hi, I have an XSD schema where I have attributes that are declared to contain non string values such as integers etc. Take for example, this declaration - <xs:attribute name="IsThisTrue"...
0
by: Hessam | last post by:
Hello, I am designing a .net custom control in VS.net 7.1 and my control exposes an array of strings which are supposed to be the items to show. To do this have declared a private string variable...
1
by: sonic | last post by:
Hi, Is there a signature that would allow me to set values for such properties as NameValueCollection or StringList within ASPX xml definition ? example: myControl has a Values property of...
16
by: Crirus | last post by:
I have a graphics path composed from multiple circles that may overlap... That graphics path I need it converted to a string and that string I whould like to be as small as possible Any ideeas?...
0
by: Rajesh soni | last post by:
hi friends i got an error while building a web control.... this error is as follows. "Cannot create an object of type 'System.String' from its string representation 'String Array' for the...
31
by: Peter Michaux | last post by:
Hi, I want to know the name of an object's constructor function as a string. Something like this <script type="text/javascript"> function Foo(){}; var a = new Foo(); alert('"' +...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
1
by: Allan Ebdrup | last post by:
I get the error: "Cannot create an object of type 'CustomWizard' from its string representation 'CustomWizard1' for the CustomWizard Property." when I view my custom server web control in...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...

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.