Solved

pass data type into function

Posted on 2015-01-23
3
192 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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