Click here to Skip to main content
15,029,958 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guy, i need you help. So i have 6 lists of different types that are populates with different types of receipts. Now i have a function that will take each of these lists, get the data out of them and export the data to a database. The problem is that i don't want to create 6 different functions that do the exact same thing but the only difference being the list passed into the function. Here is the code

C#
 List<CashReceipt> cashreceipts = new List<CashReceipt>();
 List<MiscReceipt> miscreceipts = new List<MiscReceipt>();
 List<StateGrantHeader> stategrantheader = new List<StateGrantHeader>();
 List<CashReceiptCollectionHeader> cashreceiptcollectionheader = new List<CashReceiptCollectionHeader>();
 List<ChargesAdjustmentHeader> chargesadjustmenheader = new List<ChargesAdjustmentHeader>();
                List<CashAdjustmentReceipt> cashadjustreceipt = new List<CashAdjustmentReceipt>();

// this piece of code just popluates the list with  receipts
cashreceipts.AddRange(CashReceipt.loadReceipts(school.getNextBatchNumber(), school.getLocationCode()));
                        miscreceipts.AddRange(MiscReceipt.loadReceipts(school.getNextBatchNumber(), school.getLocationCode()));

/* and so on*/



private static void ExportCashReceiptsToSBS(List<CashReceipt> receipts, String sbsBatchNumber, String generatedDate)
        {
            foreach (var receipt in receipts)
            {
                receipt.exportStoredProc(sbsBatchNumber);
            }
        }

private static void ExportMiscReceiptsToSBS(List<MiscReceipt> receipts, String sbsBatchNumber, String generatedDate)
        {
            foreach (var receipt in receipts)
            {
                receipt.exportStoredProc(sbsBatchNumber);
            }
        }


i dont want to create boiler plate code where the only changing element is the list. i know there is better way, but i am having a hard time finding a way to solve this.

Thank you for your help.
Posted
Updated 15-Jul-14 5:35am
v3
Comments
PIEBALDconsult 15-Jul-14 11:17am
   
Seems like there should be an Interface involved.
Sergey Alexandrovich Kryukov 15-Jul-14 11:51am
   
It is possible that you can create one generic function instead of 6 functions. If you cannot, the difference between element types is fundamental enough to create at least some different piece of code for 6 cases. (In other words, there is no such thing as miracle.) You may also want to review your design, looking at the root of the problem: where the element types are essentially different?
—SA
rudolph098 15-Jul-14 12:49pm
   
Damn Sergey!!!! thats ice cold. Probably the truth.
Sergey Alexandrovich Kryukov 15-Jul-14 13:40pm
   
Well, probably. To give exact suggestions to the design, I would need more information of the existing designs, goals, and a lot more...
—SA

Where does the exportStoredProc method exist? Is it in a common ancestor that each class inherits from or does each class implement it independently?

If the former, then use the base class in the list's template class:
C#
private static void ExportItemsToSBS(List<YourBaseClass> itemsToExport, String sbsBatchNumber, String generatedDate)
{
        foreach (var item in itemsToExport)
        {
                item.exportStoredProc(sbsBatchNumber);
        }
}


If the latter, then declare an interface that has that exportStoredProc method in it, add the interface to each classes' declaration, and use the interface in the method:
C#
public interface IExportItem
{
        void exportStoredProc(string batchNumber);
}

public class CashReceipt : IExportItem
{
        ...
}

private static void ExportItemsToSBS(List<IExportItem> itemsToExport, String sbsBatchNumber, String generatedDate)
{
        foreach (var item in itemsToExport)
        {
                item.exportStoredProc(sbsBatchNumber);
        }
}


Hope that helps.
   
v2
Comments
rudolph098 15-Jul-14 11:30am
   
Bless you, i will try this out , and it is the latter.
CHill60 15-Jul-14 11:38am
   
Better response than mine - 5'd
I was recently "educated" ("told off" :-)) for not using Extension Methods and Generics.

Something like this might suit your purpose
C#
static class MyExtensions
{
    public static void ExportReceipts<T>(this IEnumerable<T> list)
    {
        foreach (var item in list)
        {
            Debug.Print(item.ToString());   // or whatever you will do
        }

    }
}
which I tested with this
XML
List<string> lS = new List<string>();
lS.Add("item 1");
lS.Add("item 2");
lS.Add("item 3");

List<int> li = new List<int>();
li.Add(1);
li.Add(2);
li.Add(3);

List<double> ld = new List<double>();
ld.Add(1.1);
ld.Add(2.2);
ld.Add(3.3);

MyExtensions.ExportReceipts(lS);
MyExtensions.ExportReceipts(li);
MyExtensions.ExportReceipts(ld);
to get these results
VB
item 1
item 2
item 3
1
2
3
1.1
2.2
3.3
   
Comments
rudolph098 15-Jul-14 11:27am
   
Thank you for you help, one small problem. each of these classes CashReceipt, MiscReceipts etc have a function called "exportStoredProc". Implementing your logic, how do i access this function. Thank you
CHill60 15-Jul-14 11:38am
   
See Solution 2 for a fuller answer to that depending on how you've implemented your classes and method. Some references to the Factory pattern and Decorator patterns will also give some clues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900