Do not use on any
shared computer
July 25, 2008 02:33pm pdt
null
[x]
Attachment Details

How to get the name of property

Tags: Microsoft, C#, 2.0, Windows Form
How do I make the method shown below to return the name of the property (see comment below)?


    public class MyClass
    {
        private string _MyProperty = "";
        public string MyProperty
        {
            get { return _MyProperty; }
            set { _MyProperty = value; }
        }

        public string GetName()
        {
            return _MyProperty;  // <- should return "MyProperty";
        }
    }
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: sepknow
Solution Provided By: jaime_olivares
Participating Experts: 7
Solution Grade: A
Views: 63
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by HuyBD
Expert Comment by HuyBD:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sepknow
Author Comment by sepknow:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by Dabas
Expert Comment by Dabas:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by DrAske

Rank: Master

Expert Comment by DrAske:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by p_davis

Rank: Guru

Expert Comment by p_davis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by jaime_olivares

Rank: Sage

Accepted Solution by jaime_olivares:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by p_davis

Rank: Guru

Expert Comment by p_davis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by Priest04

Rank: Master

Expert Comment by Priest04:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sepknow
Author Comment by sepknow:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by Dabas
Expert Comment by Dabas:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by jaime_olivares

Rank: Sage

Expert Comment by jaime_olivares:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by Dabas
Expert Comment by Dabas:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
null
Comment by johnaylwin
Hmmm. Signed up for EE to get the answer to this question... and its only an answer if your property is unique or the first one in the class. That's almost like saying I can tell you the name of anybody called John.
What is required is something that tells the *compiler* to use the name of the property, rather than access the property itself. What that allows is compile-time checking that some name (string) you're going to use IS the name of such-and-such a property.
For example: if I have a typed DataSet that has a column with name "Fred", then I know there is a column in that table that can be accessed by specifying a name index of "Fred" (yes, I know I can access that field by the property). If I could get the name of the property from the property itself, I could access that field using the name index without having to use a literal "Fred" string (which would become invalid if the field name were to be changed to something else.

I want to be able to do GetPropertyName(thing.Property1) to get the string "Property1" and I don't want to have limitations like its the only property on the class.
 
null
Comment by jaime_olivares
There are some ways to achieve this, but you have to post your own question.
try with GetProperty():

this.GetType().GetProperty("prop_name")

 
null
Comment by johnaylwin
Hi Jaime,
Thanks for your suggestion, but it still codes a literal string of the property name, "prop_name"
I have posted a new question - and got a solution!
see "How to really get the name of a property or method"
Cheers
 
 
20080723-EE-VQP-34 / EE_QW_2_20070628