将不同类型的列表传递给函数 [英] Passing Lists of different types to a function

查看:55
本文介绍了将不同类型的列表传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要你的帮助。所以我有6个不同类型的列表,这些列表填充了不同类型的收据。现在我有一个函数,它将获取每个列表,从中获取数据并将数据导出到数据库。问题是我不想创建6个完全相同的功能,但唯一的区别是传递给函数的列表。这是代码



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

// 这段代码只是用收据弹出列表
cashreceipts.AddRange(CashReceipt.loadReceipts(school.getNextBatchNumber(),school.getLocationCode()));
miscreceipts.AddRange(MiscReceipt.loadReceipts(school.getNextBatchNumber(),school.getLocationCode()));

/ * 依此类推* /



private static void ExportCashReceiptsToSBS(List< CashReceipt>收据,字符串 sbsBatchNumber,字符串 generatedDate)
{
foreach var 收据 收据)
{
receipt.exportStoredProc(sbsBatchNumber);
}
}

私有 静态 void ExportMiscReceiptsToSBS(List< MiscReceipt> receipts, String sbsBatchNumber, String generatedDate)
{
foreach var 收据收据中的code-keyword>)
{
receipt.exportStoredProc(sbsBatchNumber);
}
}





我不想创建锅炉板代码,其中唯一的变化元素是列表。我知道有更好的方法,但我很难找到解决这个问题的方法。



谢谢你的帮助。

解决方案

exportStoredProc方法在哪里?它是在每个类继承的共同祖先中,还是每个类独立实现它?



如果是前者,则使用列表模板类中的基类:

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





如果是后者,则声明一个包含该exportStoredProc方法的接口,添加每个类声明的接口,并使用方法中的接口:

  public   interface  IExportItem 
{
void exportStoredProc( string batchNumber);
}

public class CashReceipt:IExportItem
{
...
}

private static void ExportItemsToSBS(List< IExportItem> itemsToExport, String sbsBatchNumber,字符串 generatedDate)
{
foreach var item < span class =code-keyword> in itemsToExport)
{
item.exportStoredProc(sbsBatchNumber);
}
}





希望有所帮助。


我当时最近受过教育(告知:-))因为没有使用扩展方法和泛型。



这样的东西可能适合你的目的

 静态  MyExtensions 
{
public static void ExportReceipts< T>( IEnumerable< T>列表)
{
foreach var item in list)
{
Debug.Print(item.ToString()); // 或您要做的任何事情
}

}
}

我使用此

列表<  字符串 >  lS =新列表<   string  > (); 
lS.Add(第1项);
lS.Add(第2项);
lS.Add(第3项);

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

列表< double > ld =新列表< double > ();
ld.Add(1.1);
ld.Add(2.2);
ld.Add(3.3);

MyExtensions.ExportReceipts(lS);
MyExtensions.ExportReceipts(li);
MyExtensions.ExportReceipts(ld);

获取这些结果

 item  1  
item 2
item 3
1
2
3
1 1
2 2
3 3


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

 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.

解决方案

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:

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:

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.


I was recently "educated" ("told off" :-)) for not using Extension Methods and Generics.

Something like this might suit your purpose

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

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

item 1
item 2
item 3
1
2
3
1.1
2.2
3.3


这篇关于将不同类型的列表传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆