By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

How to return class member as string?

P: n/a
I'd like to get the string name of a specific public class property.
Say I have a class named Person with a public property named PersonId
(Int32 type). I create a new person object and do
p.PersonId.ToString(). This returns the default value of PersonId -
zero. I want it to return the string "PersonId". Any suggestions?

Thanks,
Brett

Apr 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> I'd like to get the string name of a specific public class property.
Say I have a class named Person with a public property named PersonId
(Int32 type). I create a new person object and do
p.PersonId.ToString(). This returns the default value of PersonId -
zero. I want it to return the string "PersonId". Any suggestions?

class Person
{
//don't forget the keyword override
public override string ToString(){return "PersonId";}
}
Apr 8 '06 #2

P: n/a
That doesn't make any sense to me. You already know that's it's PersonId
because you're using it to reference the field.

Maybe if you can describe what it is you're trying to accomplish more
generally, we can give you a better idea of how to do it.

PersonId is an int, so you want it to return an int. ToString() is being
applied to that int and converting it to an string, just as it should.

The other response would allow you to do p.ToString(); and have it return
"PersonId", but that doesn't sound like it's what you want either.

So, again, if you can tell us generally what it is you're trying to
accomplish, maybe we can tell you how to do it.

Pete

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
I'd like to get the string name of a specific public class property.
Say I have a class named Person with a public property named PersonId
(Int32 type). I create a new person object and do
p.PersonId.ToString(). This returns the default value of PersonId -
zero. I want it to return the string "PersonId". Any suggestions?

Thanks,
Brett

Apr 8 '06 #3

P: n/a
So you want to do something like
string fieldName = GetPersonIdNameFromPersonSomehow(p);

here is the implementation of GetPersonIdNameFromPersonSomehow method:

public static string GetPersonIdNameFromPersonSomehow(Person p)
{
return "PersonId";
}

Your question makes the same sense as this answer.

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
I'd like to get the string name of a specific public class property.
Say I have a class named Person with a public property named PersonId
(Int32 type). I create a new person object and do
p.PersonId.ToString(). This returns the default value of PersonId -
zero. I want it to return the string "PersonId". Any suggestions?

Thanks,
Brett

Apr 9 '06 #4

P: n/a
SP
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
I'd like to get the string name of a specific public class property.
Say I have a class named Person with a public property named PersonId
(Int32 type). I create a new person object and do
p.PersonId.ToString(). This returns the default value of PersonId -
zero. I want it to return the string "PersonId". Any suggestions?


It might be best to tell us what you are trying to achieve. This type of
question is common when people are trying to map properties to column names
for mapping between your objects and a database. If this is the case then
common approaches are to use Reflection and/or Atttributes on the
properties. Another approach is to generate this code from your database's
metadata

SP
Apr 9 '06 #5

P: n/a
It's just as well for me to leave the object property alone and get the
name from intellisense. The other part I was interested in was the
type. I can do it by: p.PersonId.GetType().ToString().

Thanks,
Brett

Apr 10 '06 #6

P: n/a
> Brett Romero wrote:
It's just as well for me to leave the object property alone and get the
name from intellisense. The other part I was interested in was the
type. I can do it by: p.PersonId.GetType().ToString().


Just be aware that that will throw an exception if the type of PersonId
is a reference type and the property returns null. Also, if it's a
reference type, it may not be the same type that PersonId is declared
as. For instance:

object PersonId
{
get { return "hello"; }
}

Using your code you would get "System.String" where you might have
wanted/expected "System.Object".

Jon

Apr 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.