IEnumerator实现问题

发布于 2024-09-11 08:49:42 字数 2962 浏览 4 评论 0原文

我有这样的代码:

public class Check21CSVRecord
{

    public string TransactionType;
    public string TransactionCurrency;
    public string Reference;
    public string PaymentType;

    // date of transaction                
    public string TransactionDate;

    public string Notes;

    // customer data goes here
    public string CustomerFirstName;
    public string CustomerInitial;
    public string CustomerLastName;
    public string CustomerStreetAddress;
    public string CustomerCity;
    public string CustomerState;
    public string CustomerZIPCode;

    [FieldConverter(ConverterKind.Date, "MM/dd/yy")] 
    public DateTime CustomerDateBirth;
    public string CustomerCountry;
    public string CustomerEmail;
    public string CustomerPhone;
    public string CustomerIPAddress;

    // MICR line goes here
    public string AuxiliaryOnUs;
    public string ExternalProcessingCode;
    public string PayorBankRoutingNumber;
    public string PayorBankRoutingNumberCheckDigit;
    public string OnUs;

    // check amount
    [FieldConverter(ConverterKind.Double)]
    public double Amount;

    // what account to credit
    public string CreditAccountNumber;

    public string FrontImagePath;
    public string BackImagePath;

    // used to define if we have image or should build it dynamically
    public bool IsDynamicImage{
        get
        {
            return (FrontImagePath.Length == 0 && BackImagePath.Length == 0);
        }
    }
}


public class DataProvider <T>:  IEnumerator
{
    private FileHelperEngine CSVReader;

    private T currentRecord;
    private int currentIndex;
    private string file;
    private T[] collection = new T[5000];

    public DataProvider(string CSVFile)
    {
        CSVReader = new FileHelperEngine(typeof(T));
        collection = CSVReader.ReadFile(file) as T[];

        currentRecord = default(T);            
        file = CSVFile;
        Reset();
    }

    public bool MoveNext()
    {
        if (++currentIndex >= collection.Count())
        {
            return false;
        }
        else
        {   
            currentRecord = collection[currentIndex];
        }
        return true;

    }

    public void Reset()
    {
        currentIndex = -1;
    }

    public void Dispose()
    {

    }

    public T Current
    {
        get
        {
            return currentRecord;
        }
    }

    object System.Collections.IEnumerator.Current
    {
        get { return currentRecord; }
    }
}

当我编译时 - 一切都很好,但是当我调用该类时:

 var input = new DataProvider<Check21CSVRecord>("../../../../Artifacts/IncomingData/Check21/check21.csv");
 while (input.MoveNext())
 {
       Console.WriteLine(input.Current.CustomerFirstName);
 }

我遇到以下问题: http://screencast.com/t/NDBkNTNkMjkt

有什么解决办法吗?

谢谢, 德米特里

I have this code:

public class Check21CSVRecord
{

    public string TransactionType;
    public string TransactionCurrency;
    public string Reference;
    public string PaymentType;

    // date of transaction                
    public string TransactionDate;

    public string Notes;

    // customer data goes here
    public string CustomerFirstName;
    public string CustomerInitial;
    public string CustomerLastName;
    public string CustomerStreetAddress;
    public string CustomerCity;
    public string CustomerState;
    public string CustomerZIPCode;

    [FieldConverter(ConverterKind.Date, "MM/dd/yy")] 
    public DateTime CustomerDateBirth;
    public string CustomerCountry;
    public string CustomerEmail;
    public string CustomerPhone;
    public string CustomerIPAddress;

    // MICR line goes here
    public string AuxiliaryOnUs;
    public string ExternalProcessingCode;
    public string PayorBankRoutingNumber;
    public string PayorBankRoutingNumberCheckDigit;
    public string OnUs;

    // check amount
    [FieldConverter(ConverterKind.Double)]
    public double Amount;

    // what account to credit
    public string CreditAccountNumber;

    public string FrontImagePath;
    public string BackImagePath;

    // used to define if we have image or should build it dynamically
    public bool IsDynamicImage{
        get
        {
            return (FrontImagePath.Length == 0 && BackImagePath.Length == 0);
        }
    }
}


public class DataProvider <T>:  IEnumerator
{
    private FileHelperEngine CSVReader;

    private T currentRecord;
    private int currentIndex;
    private string file;
    private T[] collection = new T[5000];

    public DataProvider(string CSVFile)
    {
        CSVReader = new FileHelperEngine(typeof(T));
        collection = CSVReader.ReadFile(file) as T[];

        currentRecord = default(T);            
        file = CSVFile;
        Reset();
    }

    public bool MoveNext()
    {
        if (++currentIndex >= collection.Count())
        {
            return false;
        }
        else
        {   
            currentRecord = collection[currentIndex];
        }
        return true;

    }

    public void Reset()
    {
        currentIndex = -1;
    }

    public void Dispose()
    {

    }

    public T Current
    {
        get
        {
            return currentRecord;
        }
    }

    object System.Collections.IEnumerator.Current
    {
        get { return currentRecord; }
    }
}

when I compile - everything is good, but when I call the class:

 var input = new DataProvider<Check21CSVRecord>("../../../../Artifacts/IncomingData/Check21/check21.csv");
 while (input.MoveNext())
 {
       Console.WriteLine(input.Current.CustomerFirstName);
 }

I'm getting the following problem:
http://screencast.com/t/NDBkNTNkMjkt

any ideas how to fix?

Thanks,
Dmitry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2024-09-18 08:49:42

您应该首先分配file

public DataProvider(string CSVFile)
{            
    file = CSVFile;
    CSVReader = new FileHelperEngine(typeof(T));
    collection = CSVReader.ReadFile(file) as T[];

    currentRecord = default(T);
    Reset();
}

You should assign file first.

public DataProvider(string CSVFile)
{            
    file = CSVFile;
    CSVReader = new FileHelperEngine(typeof(T));
    collection = CSVReader.ReadFile(file) as T[];

    currentRecord = default(T);
    Reset();
}
在风中等你 2024-09-18 08:49:42

是的,因为“file”是空的/null。

Yeah because, 'file' is empty/null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文