Solved

pass data type into function

Posted on 2015-01-23
3
194 Views
Last Modified: 2015-01-23
I have the following two functions, their only difference is the type that it returns. is there any simple way to combine these two functions into a single function and pass the data type as a parameter?

        public List<CCG_OrderData> getCCG_ItemData(string url)
        {
            string jsonData = AddBracketsToJsonString(getJsonData(url));
            return JsonConvert.DeserializeObject<List<CCG_OrderData>>(jsonData);
        }

        public List<CCG_ReceivedOrders> getCCG_ItemData2(string url)
        {
            string jsonData = AddBracketsToJsonString(getJsonData(url));
            return JsonConvert.DeserializeObject<List<CCG_ReceivedOrders>>(jsonData);
        }

Open in new window

0
Comment
Question by:Big Monty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40566424
You can make a generic method:

public List<T> getCCG_ItemData<T>(string url)
{
    string jsonData = AddBracketsToJsonString(getJsonData(url));
    return JsonConvert.DeserializeObject<List<T>>(jsonData);
}

Open in new window


Then it's simply a matter of calling the method with the correct type argument:

CCG_OrderData a = getCCG_ItemData<CCG_OrderData>("www.example.com");
CCG_ReceivedOrders b = getCCG_ItemData<CCG_ReceivedOrders>("www.example.com");

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40566478
If both CCG_OrderData and CCG_ReceivedOrders had a common class they inherit from then you could use casting BUT the generic method suggested by kaufmed above is neater.

ps.  If you look closely you will see JsonConvert.DeserializeObject that you use is using the same, generic, technique.
0
 
LVL 33

Author Closing Comment

by:Big Monty
ID: 40566574
perfect, just what I was looking for
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question