How to return class member as string?

B

Brett Romero

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
 
M

Martijn Mulder

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";}
}
 
P

Pete Davis

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
 
L

Lebesgue

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.
 
S

SP

Brett Romero said:
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
 
B

Brett Romero

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
 
J

Jon Skeet [C# MVP]

Brett said:
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
 

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

Top