Name of a property?

Hi
I want to call my property NameOrDescription and have it of type enum NameOrDescription.
Is this possible?
It was working - the commented first line - put that name looks quite bad.
Does it have to be a different name to the enum?

Thanks
Paul


//public NameOrDescription NameOrDescriptionProperty
        public NameOrDescription NameOrDescription
        {
            get { return _eNameOrDescription; }
            set { _eNameOrDescription = value; }
        }




// ENUM
        public enum NameOrDescription { Name, Description };

//FIELD
private NameOrDescription _eNameOrDescription;
paulwhelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andrewjbCommented:
Perfectly allowed.

I've got several..

0
paulwhelanAuthor Commented:
Thanks.

Is there a reason why I can't rename the property from
NameOrDescriptionProperty
to
NameOrDescription
?
Is it just because of the enum?
Thanks
Paul
0
andrewjbCommented:
Should be fine...
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

andrewjbCommented:
The following builds fine for me. What's your problem?

  public enum NameOrDescription { Name , Description };

  public class MyClass
  {
    private NameOrDescription e_Name;
    public NameOrDescription NameOrDescription
    {
      get { return e_Name; }
      set { e_Name = value; }
    }
  }
0
mrichmonCommented:
No you shouldn't do that.  You are naming a property the exact same as the return type.  Now you can use different capitalization.  However this still is not recommended.

In general you need to consider that you will be coming back at a later time to update or work on the code - or someone else will.  If you have the identical names in identical cases you may not know if you meant the type or the property.

The only reason that it works is that the complete namespace is different.  The property would be accessed as

ClassInstance.NameOrDescription

Where the class would be just NameOrDescription

So while it is allowed it is not recommended as a best practice.
0
andrewjbCommented:
... There are examples all over the Microsoft components.

Button.FlatStyle is the first I happened to spot...

I don't see there's anything wrong, personally. They are semantically different constructs. There's no chance of actually getting the two confused 'cos they're never interchangable..

0
vlitCommented:
I'm guessing the reason it's not working for you, is that you have the enum defined in the class as well. So instead of andrewjb's:

  public enum NameOrDescription { Name , Description };

  public class MyClass
  {
    private NameOrDescription e_Name;
    public NameOrDescription NameOrDescription
    {
      get { return e_Name; }
      set { e_Name = value; }
    }
  }

you probably have:

  public class MyClass
  {
    public enum NameOrDescription { Name , Description };

    private NameOrDescription e_Name;
    public NameOrDescription NameOrDescription
    {
      get { return e_Name; }
      set { e_Name = value; }
    }
  }

Now, the MyClass class has to different things that are both named NameOrDescription and that's probably giving you an error. If you really want to name them the same, just move the enum definition out of the class like in andrewjb's example.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.