skip to Main Content

I ran into this legacy code that has static class that contains an enum and extension method with its own namespace. I have simplied it to below:

namespace AuxValidator
{

    public static class AuxValidator
    {

        public enum BadDataSentinal { UPPER = 0, LOWER = 1, WHATEVER = 2 }

        public static string Upper(this string field, BadDataSentinal badData, List<string> NewDataWatchlist = null)
        {
            if(badData == BadDataSentinal.UPPER)
               return field.Upper();

            return field
        }   
        

    }
}

This above class sits inside App_Code.

It is used in one of my aspx.cs file,

using AuxValidator;

public partial class SimplePage : RootPage
{
   private void updateBoxes(object source, ServerValidateEventArgs args, string controlName, ValidationFunction f)
    {
        CustomValidator cv = (CustomValidator)source;
        TextBox src = ((TextBox)cv.FindControl(controlName));
        
        src.Text = args.Value.Upper(AuxValidator.AuxValidator.BadDataSentinal.UPPER);

    }
}

This works. However, notice in order to access the Enum, it has to first declare the namespace AuxValidator, then the class AuxValidator just to use the enum. If I take out the namespace and try to use it like AuxValidator.BadDataSentinal.UPPER, I will get:

The type or namespace name ‘BadDataSentinal’ does not exist in the
namespace ‘AuxValidator’

It is a bit awkward, so intuitively, I take BadDataSentinal out of the class AuxValidator so I can access it simply with BadDataSentinal.UPPER.

Or,I can get rid of the namespace inside my AuxValidator class, so I can access like AuxValidator.BadDataSentinal.UPPER.

My question:

1: Why does a public enum inside static class needs to declare the namespace where it is used, despite already imported it with "using"

2: Is there any reason for public enum in static class?

2

Answers


  1. 1: Why does a public enum inside static class needs to declare the
    namespace where it is used, despite already imported it with "using"

    This is because the namespace and the class share the same name AuxValidator therefore the compiler will interpret AuxValidator as namespace. If they have different name you will not need to specifie the namespace twice

    2: Is there any reason for public enum in static class?

    Not really.

    Login or Signup to reply.
  2. All what you have is available to broad interpretation. But if you apply certain rules, you can standardize your code

    "Why does a public enum inside static class needs to declare the namespace where it is used, despite already imported it with "using"" – a good coding standards would make it illegal to have namespace and class to have same name. And here you have great example why this should be done – so you don’t need to use full-qualifying name and for the code clarity.

    "Is there any reason for public enum in static class?" – by good standards public enums can be declared in static or instance classes when such enums are used only by these classes and nowhere else.

    "Or,I can get rid of the namespace" – you should rename the namespace to something different from your class. Besides, the classes and namespaces should have different naming styles. If your class is named in correct style – AuxValidator, the namespace should be something like SomeValidation. Since this is in APP_Code, you should have no issues renaming it because this is not something like redistribute library where you don’t want to upset customers with breaking changes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search