-4

I have method defined like this

public static T FromValue<T>(int value) where T : Enumeration, new()
    {
        var matchingItem = parse<T, int>(value, "value", item => item.Value == value);
        return matchingItem;
    }

this is something from this example

What i am doing is to call that method and pass int and get matching string from enum. So i tried doing like

string matchedString = MyclassName.FromValue<int>(0);

but this is not correct syntax as i dont know what and how to pass to this method FromValue. Will also be nice to get examples so i can read about it and solve myself in future.

Update I created class from Enumeration class in example like

  public  class MyClassName: Enumeration
{

    public static readonly MyClassName Dog
        = new MyClassName(0, "KL");
    public static readonly MyClassName CAT
        = new MyClassName(1, "CT");


    private MyClassName(int value, string displayName) : base(value, displayName) { }


}
James
  • 1,827
  • 5
  • 39
  • 69
  • 6
    Your `T` has restrictions, look at the `where T : Enumeration, new()`. This means that your `T` has to follow those rules (i.e be of type `Enumeration` and have a public parameterless constructor [`new()`]) – maccettura Aug 09 '18 at 15:04
  • @maccettura so what to do here and what to pass ? will be nice of you really – James Aug 09 '18 at 15:05
  • If you expect a `string` as the return type then you need to specify that as the generic type `FromValue(0)` However that will still not work with the `Enumeration` constraint. – juharr Aug 09 '18 at 15:05
  • @Happy the tutorial you linked shows the `Enumeration` class, you are supposed to create it according to the tutorial. – maccettura Aug 09 '18 at 15:07
  • @maccettura but i cant find way to get string. If you see the Enumeration class it has FromValue method. Which i thought i can pass int value and get matching string. But i really dont know how to pass int and get string – James Aug 09 '18 at 15:10

2 Answers2

1

int is not a subtype of Enumeration, hence, the where T: Enumeration constraint is not satisfied.

Thus, you need to call MyclassName.FromValue<X>(0), with X being Enumeration or a subtype thereof, e.g.

MyclassName.FromValue<MyEnumeration>(0)

with MyEnumeration declared as

class MyEnumeration : Enumeration { ... }

Note that this will return a value of type MyEnumeration since you declared your return type to be the generic parameter:

public static T FromValue<T>(int value) where T : Enumeration, new()
              ^

To get a string representation, you will have to invoke ToString() or access a property of type string. The Enumeration class you linked to overrides ToString() as well as providing a DisplayName property.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • The tutorial OP linked defines that class FYI. – maccettura Aug 09 '18 at 15:08
  • 1
    @maccettura: I see, thanks. I have removed my second paragraph and added an example. – Heinzi Aug 09 '18 at 15:09
  • i need to create another class? Not sure if you understood my question. I already have created Enumeration class as in example. Then i inherited the class and created Enums. Now i want to get matching string by passing int – James Aug 09 '18 at 15:13
  • @Happy: *"Then i inherited the class and created Enums."* Great! Now just put the name of the "enum" you created into the brackets: `MyclassName.FromValue(0)`. – Heinzi Aug 09 '18 at 15:15
  • i cannot enum like you said. Please see how i created Enums from Enumeration class in example. Can you advice further? i updated questions – James Aug 09 '18 at 15:24
  • @Happy: You need to add an empty public constructor to your Enumeration, that's what `where T: ... new()` means. That's a empty public constructor: `public MyClassName() { }` – Heinzi Aug 09 '18 at 15:26
  • i used this and it works string d = Enumeration.FromValue(0).ToString(); but why i should call Enumeration.FromValue ? i want to call MyClassName.FromValue because eventually MyClassName has inherited from Enumeration ? – James Aug 09 '18 at 15:32
  • 1
    @Happy `FromValue` is a static method, and as such is not intended to be overridden in the way you are wanting. – Abion47 Aug 09 '18 at 15:36
  • @Happy: Unfortunately, C# does not support [return type covariance](https://stackoverflow.com/q/5709034/87698). – Heinzi Aug 09 '18 at 15:51
1

Generics can be a bit complicated to learn at first. There are two points to bear in mind when making a method generic.

  1. The generic parameter(s): (T)
  2. The constraints, if any: (where T:)

The generic method in your question looks like this:

public static T FromValue<T>(int value) where T : Enumeration, new()

This generic method declaration states that you have one generic parameter T, and that parameter is constrained to being a type that is an Enumeration and it must have a public parameterless constructor (new()). Constraints are useful in generics because it allows you to use generics while having a small set of rules or restrictions on it. Maybe you constrain on an interface so you know any T passed in will have the members in the interface? The possibilities are endless.

Your problem is because you are trying to use an int as your generic parameter, and now that I have just explained what constraints are you should know that this would not work because System.Int32 does not meet the requirements.

maccettura
  • 10,514
  • 3
  • 28
  • 35
  • could you give me good tutorials to be followed. Shameful to get so many negative points. TO be honest i was not even sure how i should ask question as i didnt know what is it called. – James Aug 09 '18 at 15:45