-1

I'm a beginner in C#. I have a class named config with one string field named kye.

When I apply the GET property of the class, the property has to return one variable kye in different types (Int or bool or String).

I need to implement this with help of enum operator. Any idea how can I do this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Michael
  • 13,950
  • 57
  • 145
  • 288
  • 8
    Seems to be an [X-Y problem](http://www.perlmonks.org/?node_id=542341) – L.B Jul 15 '12 at 08:13
  • 1
    Related: [making a generic property](http://stackoverflow.com/questions/271347/making-a-generic-property/271356#2713560) – Mahmoud Gamal Jul 15 '12 at 08:18
  • @Michael Please may I ask why do you want to keep a property in config that can be of 3 different types – HatSoft Jul 15 '12 at 08:27

2 Answers2

1

There is no such thing built into the language as far as I know so you'll have to do it manually. One straightforward way will be:

public object GetKye(KyeType type)
{
    switch (type)
    {
        case KyeType.String:
            return this.kye;
        case KyeType.Int32:
            return Int32.Parse(this.kye);
        case KyeType.Bool:
            return this.kye.ToLower().Equals("true");
    }
    return null;
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

This removes the need to do a cast in code but is not safe. It needs plenty of error handling around it.

public T Kye<T>(KyeEnum Key)
{
    return (T)kye;
}

Or is this what you're after:

public Tuple<int, bool, string> Kye(KyeEnum Key)
{
    return new Tuple<int, bool, string>(nKye, bKye, sKye);
}
Paul Fleming
  • 24,238
  • 8
  • 76
  • 113