aaronmallen
aaronmallen

Reputation: 1438

Passing just the enum value in a method

I have an enum called SemverPartOption which has the following values:

public enum SemverPartOption
{
    Full,
    Version,
    Major,
    Minor,
    Patch,
    Micro,
    Pre,
    Meta
}

and then I have a method which uses this enum as a parameter:

public static string SemverPart(SemverPartOption? Option = SemverPartOption.Full)

Both the enum and the method live in AppName.Helpers namespace and the AppInfo class. I am trying to be able to call the method and just pass the enum value. Example:

What I am having to do:

Helpers.AppInfo.SemverPart(Helpers.AppInfo.SemverPartOption.Major);

What I would be ok with:

Helpers.AppInfo.SemverPart(Major);

MOST IDEAL:

SemverPart(Major);

Is there a way to do this without constantly having to use a using statement?

To be clear I don't want to have to use a using statement because I would like to call this method in several Razor views belonging to different controller classes.

Upvotes: 0

Views: 166

Answers (2)

Brad Christie
Brad Christie

Reputation: 101614

Given the additional information (about this being used in a razor page) you can add a "using" to razor using the ~/Views/web.config file. The node would be as follows:

<configuration>
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="Web" />
        <add namespace="AppName.Helpers.AppInfo" /> <!-- new addition -->
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>

This is the facsimile of adding using AppName.Helpers.AppInfo to a code file, but applies specifically to @Razor pages.

Upvotes: 1

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44448

No, you'll have to add the using statement.

This shouldn't be a problem; your IDE will give you the option to just import it when you type Major instead of having to manually add it.

If you have another enum/class with the same name (Major) then it will act up but you can get past this by adding more information (like SemverPartOption.Major or however much you have to go up), or by using an alias.

In order to import this enum into every one of your razor views, go to your /Views/Web.config file and add (or adjust) the following XML:

 <system.web.webPages.razor>
    <host
      factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="AppName.Helpers.AppInfo" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Upvotes: 1

Related Questions