-1

I have a ViewModel that I pass from my server to my client:

public class SomeViewModel
{
    public string ViewName {get;set;}
}

I have the following static class that I use to represent enum values as strings conveniently throughout my business logic:

public static class AnimalType
{
    public static string Dog => "dog-string";
    public static string Cat => "cat-string";
}

I'd like to attach this static class to the instance of the ViewModel so the client has access to the same enum values, without me having to duplicate code on the client:

public class SomeViewModel
{
    public string ViewName {get;set;}
    public AnimalType AnimalTypes {get { return AnimalType; }} // doesn't work
}

How can I concisely share this static "enum" solution with my client, via a ViewModel property?

BLAZORLOVER
  • 1,971
  • 2
  • 17
  • 27
  • Could you add a constructor to your `SomeViewModel` class with an instantiation of new `AnimalType`? – Coded Container Jan 23 '21 at 02:40
  • 1
    @CodedContainer - that would require `AnimalType` to be non-static, which would require me to create an instance of it any time I want to reference one of the pseudo-enum values, right? – BLAZORLOVER Jan 23 '21 at 02:41
  • 1
    If the class is static, y would you need to associate that with another class this way? You can access the static class without referencing it through another class anyway – Jawad Jan 23 '21 at 02:43
  • @Jawad to avoid having to represent the enum values on both my server, and my client, with two separate enum specifications that both need to be updated whenever the spec changes. In other words, I'm trying to reuse the stringy enums on both server + client. – BLAZORLOVER Jan 23 '21 at 02:46
  • @BLAZORLOVER You could assign this in your controller when you create a new instance of `SomeViewModel` or just change the signature of `AnimalType` – Coded Container Jan 23 '21 at 02:46
  • @CodedContainer sorry for the density here, but what would that assignment look like? – BLAZORLOVER Jan 23 '21 at 02:48
  • 1
    `AnimalType` is not an enum. If you want an enum, create an enum. – mjwills Jan 23 '21 at 02:50
  • Add `LabelAttribute` (or `EnumMemberAttribute`) to it if you need an associated string value, and use https://github.com/xin9le/FastEnum#3-adds-multiple-label-annotations-to-a-field to read it. Will that fulfill your requirements? If so, which _specific_ requirement won't it fulfill? – mjwills Jan 23 '21 at 02:52
  • Make class not static, change property to public string Dog { get; private set' } = "dog-string"; and inherit it on your class – Aleksa Ristic Jan 23 '21 at 02:58
  • 2
    @BLAZORLOVER Seems like you are confused between static variables and enum types. If not, can you put a reference to the 'static enum' you are trying to define in your static class `AnimalType`. – SJaka Jan 23 '21 at 03:06
  • 2
    Seems like an [X-Y problem](https://meta.stackexchange.com/q/66377/241853) – Aluan Haddad Jan 23 '21 at 03:09
  • 2
    This is an XY problem at best, unclear at worst. Show how you want the client to use said values and what client are you referring to. Based on the comments and answers there is a bit of confusion about what it is you actually want – Nkosi Jan 23 '21 at 03:13
  • The duplicate seems to cover every idea discussed here (well, it covers a slower version of my idea - but the principle is the same). – mjwills Jan 23 '21 at 03:16

2 Answers2

1

Would something like this work?

public class AnimalType
{
    public AnimalType()
    {
        Dog = "Dog string";
        Cat = "Cat String";
    }
    public string Dog {get; set;}
    public string Cat {get; set;}
}

public class SomeViewModel
{
    public SomeViewModel()
    {
        AnimalType = new AnimalType();
    }
    public string ViewName {get;set;}
    public AnimalType AnimalType {get; set;}
}
Coded Container
  • 863
  • 2
  • 12
  • 33
1

If you are trying to use static variables, I'd do it like this:

public class SomeViewModel
{
    public string ViewName {get;set;}
    public static void Main()
    {
        Console.WriteLine(AnimalType.Dog);
    }
    
}

public static class AnimalType
{
    public static string Dog = "dog-string";
    public static string Cat = "cat-string";
}

// if someone is hell-bent to use enums then something like this would work with a bit of work:

public enum AnimalType
{
    [EnumMember(Value = "dog-string")]
    Dog,
    [EnumMember(Value = "cat-string")]
    Cat
    }
}
SJaka
  • 712
  • 13
  • 40
  • In the context of the question. I'd say `AnimalType.Dog` or `AnimalType.Cat` would provide the values, as along as the viewmodel has access to AnimalType class. – SJaka Jan 23 '21 at 03:16
  • As far as I know, enum can't contain string values, what is your suggestion then? – SJaka Jan 23 '21 at 03:22
  • I guess using a EnumMemberAttribute is a possible solution, as you suggested, but a long winded way to achieve the desired result. Unless enums are an absolute requirement, and a usecase has to be presented for that, I'd keep solution simple. Either way, OP presented enum in a way, it is not possible to work with. – SJaka Jan 23 '21 at 03:26
  • How to use the property is already in the solution `AnimalType.Dog` or `AnimalType.Cat` as long as AnimalType is accessible. – SJaka Jan 23 '21 at 03:34
  • I believe, it is an assumption you are making that the OP wants a property what should be one or the other (like an enum), since OP used the word enum, while describe it as static strings, hence all the confusion in various answers and comments. I'd let the OP decide what works for him/her. I've edited the answer to add your suggestion of `EnumMemberAttribute ` if your assumption is correct and that is what OP wants. – SJaka Jan 23 '21 at 03:39