10

I have an enum Pitch and a class Pitch, both naturally to me should be named as such, however it's confusing to have both with the same name. Are there any guidelines for when an enum's name clashes with a class type?

EXAMPLE:

public enum Pitch
{
    Fastball,
    Sinker,
    Curveball
}

public class Pitch
{
}
Rhubarb
  • 3,893
  • 6
  • 41
  • 55
  • 3
    If you have two types that you want to name the same, there is probably a problem with your design. Can you show us the code for the class and the enum? – Steven Dec 04 '11 at 13:44
  • Doesn't seem like anybody answered how to deal with an Enum with same name as clash - when the source code comes from someone else. – John Foll Jun 22 '22 at 19:37

6 Answers6

12

Name the enum PitchType, PitchKind, PitchMagnitude, PitchQuality, PitchShape, PitchSpeed, PitchStrength or whatever fits best.


Another consideration is whether the class design could be improved. Instead of having a PitchType property inside the class Pitch, you could also create a class hierarchy:

public abstract class Pitch {}

public class Fastball : Pitch {}

public class Sinker : Pitch {}

public class Curveball : Pitch {}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
4

Embed the enum in the class:

public class Pitch
{
    public enum Kind {
        Fastball, 
        Curveball, 
        Sinker
    }
}

You can then access it through the class:

Pitch.Kind.Fastball
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • I thought of that, but does that violate some convention of OO that I can't think of? Does making one go through the extra level of naming just to deal with a clash mean it's still good design? – Rhubarb Dec 04 '11 at 14:02
  • I see no problems here. But the enum should preferably be embedded in the class in which it will be used or at least it should have a strong logical relation to it. – Olivier Jacot-Descombes Dec 04 '11 at 14:10
  • See stackoverflow question [Pros and cons of using nested C++ classes and enumerations?](http://stackoverflow.com/questions/216748/pros-and-cons-of-using-nested-c-classes-and-enumerations). – Olivier Jacot-Descombes Dec 04 '11 at 14:44
3

Use namespaces to group them logically. For the framework the class name is the full name, which may be MusicPlayer.Notes.Pitch and no just Pitch. Classes in different name spaces thus cannot clash.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • the using statement would beg to differ. – Scott Solmer Apr 19 '18 at 14:10
  • @Okuma.Scott: The `using` statement is about deterministic resource management, are you confusing it with the `using` *directive*? In that case, you'd just have to fully qualify the type names when used if you end up `using` both namespaces. And if you're aliasing a type to an already existing name, well, that's your problem entirely. – Joey Apr 19 '18 at 19:15
  • Thank you for the correction - the using _directive_ is what I meant. – Scott Solmer Apr 23 '18 at 11:54
1

When an enum is not embedded in a class make sure you use some different name before the label so as to prevent name clashes. You can also use namespaces as suggested by joey.

0
public class Pitch
{
    public enum Enum {
        Fastball, 
        Curveball, 
        Sinker
    }
}

Pitch.Enum.Fastball

Greg Gum
  • 33,478
  • 39
  • 162
  • 233
0

In the immediate instance where the enum is actually the identifier for the class, I settled on nesting the enum inside the class and naming it with the name: Option. I named the Option property: Id.

public class Pitch
{
    public Option Id { get; set; }

    public enum Option
    {
        Invalid = 0,
        Fastball = 1,
        Sinker = 2,
        Curveball = 3
    }
}
llessurt
  • 555
  • 3
  • 14