Home > Net >  What would be the return type of DataTable for custom method
What would be the return type of DataTable for custom method

Time:12-01

Initially, I have consumed REST API in C# and converted the response into a DataTable.

namespace pg
{
    class Program
    {
        static void Main(string[] args)
        {
            data1();
        }

        public static void data1()
        {
            var client = new RestClient("https://dummy.restapiexample.com/api/");
            var request = new RestRequest("giveData");
            var response = client.Execute(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string rawResponse = response.Content;
                var root = JsonConvert.DeserializeObject<Rootobject>(rawResponse);
                DataTable dt = root.notifications.ToDataTable();

            }
        }

        public class Rootobject
        {
            public List<Notification> notifications { get; set; }
            public class Notification
            {
                public string Name { get; set; }
                public DateTime DOB { get; set; }
                public string email { get; set; }
            }
        }
    }

    public static class IListExtensions
    {
        public static DataTable ToDataTable<T>(this IList<T> list)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            DataTable dataTable = new DataTable();
            foreach (PropertyDescriptor property in properties)
                dataTable.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);

            if (list == null)
                return dataTable;

            foreach (T item in list)
            {
                DataRow row = dataTable.NewRow();
                foreach (PropertyDescriptor property in properties)
                row[property.Name] = property.GetValue(item) ?? DBNull.Value;
                dataTable.Rows.Add(row);
            }
            return dataTable;
        }
    }
}

I will add a new method to my code to do some operations and for that, I require the method data1() to return the DataTable. My question is what return type should I specify for data1() method, which will return me the DataTable 'dt'?

I tried to change the return type of data1() to DataTable but I get error- " Program.data1(): Not all code path return a value".

class Program
{
    static void Main(string[] args)
    {
        data1();
        newMethod();
    }

    public static DataTable data1()
    {
        var client = new RestClient("https://dummy.restapiexample.com/api/");
        var request = new RestRequest("giveData");
        var response = client.Execute(request);

        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            string rawResponse = response.Content;
            var root = JsonConvert.DeserializeObject<Rootobject>(rawResponse);
            DataTable dt = root.notifications.ToDataTable();
            return dt;

           else
            {
                Console.WriteLine(response.ErrorMessage);
            }
       }

    }

    public static void newMethod()
    {
            operations...
    }

CodePudding user response:

Your error has nothing to do with the data type (which is fine), but with the fact that you only return a data table in the if. What "if not"? You cannot return "nothing". You could make DataTable nullable and return null, or your could throw an exception. But you cannot just leave your compiler hanging here. You have to define what happens "if not".

  • Related