skip to Main Content

I want to assign a string value got from a restful API to my Designation value which is an “enum” class variable.I was trying to do it like this but it’s not working I guess.Can somebody please tell me how to do it correctly?
People.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;

namespace ISTE.Models
{
    public enum Designation
    {
        Lecturer,
        Professor
    }
    public class Faculty
    {
        public BitmapImage _image;
        public Uri _IconUri;
        public string username { get; set; }
        public string name { get; set; }
        public string tagline { get; set; }
        public string imagePath { get; set; }
        public Uri IconUri
        {
            get;

            set; }

        public BitmapImage image { get; set; }


        public string title { get; set; }
        public string interestArea { get; set; }
        public string office { get; set; }
        public Uri website { get; set; }
        public string phone { get; set; }
        public string email { get; set; }
        public string twitter { get; set; }
        public string facebook { get; set; }
        public Designation desig { get; set; }
    }

    public class Staff
    {
        public string username { get; set; }
        public string name { get; set; }
        public string tagline { get; set; }
        public string imagePath { get; set; }
        public string title { get; set; }
        public string interestArea { get; set; }
        public string office { get; set; }
        public Uri website { get; set; }
        public string phone { get; set; }
        public string email { get; set; }
        public string twitter { get; set; }
        public string facebook { get; set; }
    }


    public class People
    {
        public string title { get; set; }
        public string subTitle { get; set; }
        public List<Faculty> faculty { get; set; }
        public List<Staff> staff { get; set; }

    }
}
------------------------------------------------------------------------
DataService

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using ISTE.Models;
using System.Net.Http;

namespace ISTE.Services
{
    public class PeopleDS
    {
        public List<Faculty> GetItemDetails()
        {
            People facItem = new People();
            Faculty fa = new Faculty();

            List < Faculty > fac = new List<Faculty>();

            try
            {
                using (var client = new HttpClient())
                {
                    //client.DefaultRequestHeaders.Add("X-API-Key", "9ef8ddfc6d254dc3a7b2cac337c6d837");
                    string uri3 = $"https://ist.xyz.edu/api/people";

                    var response1 = client.GetAsync(uri3).Result;
                    var content1 = response1.Content.ReadAsStringAsync().Result;
                    dynamic item1 = Newtonsoft.Json.JsonConvert.DeserializeObject(content1);
                    facItem.faculty = item1.faculty.ToObject<List<Faculty>>();

                    fac = item1.faculty.ToObject<List<Faculty>>();

                    foreach (Faculty fy in fac)
                    {
                        Console.WriteLine("designation t" + fy.title);
                        fy.desig = (Designation)Enum.Parse(typeof(Designation), fy.title, true);

                        // try to parse the string as a TestEnum without throwing an exception
                        var designation = fy.desig;
                        if (Enum.TryParse(fy.title, true, out designation))
                        {
                            // success
                        }
                        else
                        {
                            // the string isn't an element of TestEnum
                        }

// ...


                        fy.imagePath = fy.imagePath;
                        fy.IconUri = new Uri(fy.imagePath);
                        fy.image = new System.Windows.Media.Imaging.BitmapImage(fy.IconUri);
                    }




                }
            }
            catch (System.Exception ex)
            {
                return fac;
            }
            return fac;
        }
    }
}

2

Answers


  1. How about something like this, off the top of my head and untested:

     Designation designation = (Designation) Enum.Parse(typeof(Designation), designationString); 
    

    You would want to add error trapping or perhaps use Enum.TryParse instead and let that trap exceptions for you…

    Login or Signup to reply.
  2. To cast a string to an enum, you can do this:

    SomeEnum bar;
    if (Enum.TryParse(incoming, true, out bar))
    {
        // parsing succeeded
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search