Solved

pass data type into function

Posted on 2015-01-23
3
168 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
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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 32

Author Closing Comment

by:Big Monty
Comment Utility
perfect, just what I was looking for
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now