0

I'm creating a class that creates an object from a Json with multiple attributes and one of the attributes has a number that I would like to replace programmatically with the corresponding value associated with said number from an enum class. The Problem is i'd like to do it in the auto property.

Here is my JSON:

[{"reason":100000003}]

Here are my enum classes:

public enum option1
    {
        val1 = 100000001,
        val2 = 100000002,
        val3 = 100000003,
        val4 = 100000004


    }

    public enum option2
    {
        val1 = 100000001,
        val2 = 100000002,
        val4 = 100000003,
        val5 = 100000004

    }

Here is my Object class:

public class Value{
private string reason;
     [JsonProperty("reason")]
     public string Reason
     {
        get
        {
            return reason;
        }
        set
        {
            reason = Indexer(reason,"test2");
        }
     }
class RootObject
    {
        public List<Value> Value { get; set; }
    }
 }

Here is the function to convert string number to corresponding value

//for this example, index == "test2"
public static string Indexer(string input, string index)
        {
            string result = null;
            if (index.Equals("test") || index.Equals("test2"))
            {
                switch (input)
                {
                    case "100000001":
                        result = Value.option1.val1.ToString();
                        break;
                    case "100000002":
                        result = Value.option1.val2.ToString();
                        break;
                    case "100000003":
                        if (index.Equals("test1"))
                            result = Value.option1.val3.ToString();
                        else
                            result = Value.option2.val4.ToString();
                        break;
                    case "100000004":
                        if (index.Equals("test1"))
                            result =Value.option1.val4.ToString();
                        else
                            result = Value.option2.val5.ToString();
                        break;
                        default:
                        result = null;
                        break;
                 }
            }
            return result;
        }

For my result I would like the Reason field for my Object to be "val4" instead of "100000003" but it isn't working.

  • Possible duplicate of [.NET - JSON serialization of enum as string](https://stackoverflow.com/questions/2441290/net-json-serialization-of-enum-as-string) – Sinatr May 13 '19 at 14:53
  • 1
    Shouldn't `reason = Indexer(reason,"test2");` be `reason = Indexer(value,"test2");` Instead? Otherwise I think all you'll ever get is `null`. – Ron Beyer May 13 '19 at 15:07
  • I thought that the value I would be passing into my function would be the reason, which I am capturing in "private string reason;" @RonBeyer – Russell Quao May 13 '19 at 15:26
  • @RonBeyer Wow nevermind, it worked, Thank you!! – Russell Quao May 13 '19 at 15:29
  • 1
    @RussellQuao No, `value` is the value being given to you in the setter. `reason` is not automatically set anywhere, you need to set it. Since it isn't being set, all you ever do is pass `null` around. – Ron Beyer May 13 '19 at 15:30

0 Answers0