-1

I hope not to make a duplicate but I didn't find what I'm searching for.

I'm developing a Asp.Net MVC website with a personal area where you can buy a subscription for the application. I have 3 plans for subscription, they are all objects of the same class.

I'm thinking if is possible to create these 3 objects as constants with different values and put them in a static class. What is the best way to do this?

I'd like to retrieve the subscription object with something like Subscriptions.ONE_MONTH or Subscriptions.SIX_MONTHS.

maccettura
  • 10,514
  • 3
  • 28
  • 35
  • 2
    You basically just described how to do it, what is the problem? What have you tried? – maccettura Jan 08 '18 at 22:21
  • So did you create a static class with three constants? Did it work? You also might consider using an [enum](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum). – Rufus L Jan 08 '18 at 22:23
  • If you want any help you will have to let us know what you actually need help with. – maccettura Jan 08 '18 at 22:30
  • I don't know if I explained correctly. I have a Subscription class and I need to have 3 instances of this class with different values but without creating an empty instance and setting every field, just three instances of this class with pre-defined values.. – Daniele Lewis Jan 08 '18 at 22:35
  • Do you want other instances of the subscription class to be created? Or only ever the 3 types you defined? – maccettura Jan 08 '18 at 22:52

2 Answers2

1

The ideal way to do this is to have the three plans as public static getter-only properties of the static class. You can create the instances either inline, or in the static constructor of the class.

public static PlanA { get; } = new Plan(...);
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
-1

There are a few possible ways to do this. You could create a static class with public fields or properties:

public static class Subscriptions
{
    public static whateverType OPTION_1 = whateverValue;
    public static whateverType OPTION_2 = whateverValue;
    public static whateverType OPTION_3 = whateverValue;
}

public static class Subscriptions
{
    public static whateverType OPTION_1 => whateverValue;
    public static whateverType OPTION_2 => whateverValue;
    public static whateverType OPTION_3 => whateverValue;
}

or if the values are of a basic type an enum:

public enum Subscriptions
{
   Option_1,
   Option_2,
   Option_3 
}

If you want to use an enum with the possibility of having more than one value you can use the Flags attribute:

[Flags]
public enum Subscriptions
{
   Option_1 = 1,
   Option_2 = 2,
   Option_3 = 4
}

One thing you should probably avoid is having public constant values in a non static class (for the reasons see this question):

public class Subscriptions
{
    public const whateverType OPTION_1 = whateverValue;
    public const whateverType OPTION_2 = whateverValue;
    public const whateverType OPTION_3 = whateverValue;
}
SBFrancies
  • 3,987
  • 2
  • 14
  • 37