• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

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;
0
paulwhelan
Asked:
paulwhelan
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now