0

This is probably a very simple question but I am new to C# and can't figure out how to get the value of my enum?


 public enum Status
    {
        OK,
        Bad,
    }

I want to display it as follows (hard coded at the moment):

 public Task<ResultData[]> GetResults()
        {
            var dummyData = new ResultData();
            dummyData.Title = "title";
            dummyData.Status = Status.OK;
            dummyData.Value = 5;

            var results = new ResultData[] { dummyData };
            return Task.FromResult(results);
        }

However this shows the status as 0 not OK. I want it to show OK in the array when I use this GetResults() method. I have tried Status.OK.ToString() as many other posts have suggested but I get the error that it cannot convert the string to the type Status. I have looked at other articles but cannot seem to figure out a simple way of doing this - can anyone assist?

Thanks

Jordan1993
  • 864
  • 1
  • 10
  • 28
  • 1
    https://stackoverflow.com/questions/483794/convert-enum-to-string – skjagini Nov 14 '19 at 17:12
  • @Skjagini Thank you, but I get the error that it cannot convert a `string` to the type `Status` with this method. – Jordan1993 Nov 14 '19 at 17:17
  • @Jordan1993 how are you using the results ? Could you share the code where you access the results and the exact line where you get the exception – Anu Viswan Nov 14 '19 at 17:22
  • 1
    If you want to send these results over the wire, do not convert to String, keep it as enum only, while sending over the wire, you can apply Json Enum to String converter, https://stackoverflow.com/questions/2441290/javascriptserializer-json-serialization-of-enum-as-string – skjagini Nov 14 '19 at 17:24

0 Answers0