2010-07-24 76 views
0

我有這樣的代碼:IEnumerator的實現問題

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

任何想法如何解決?

感謝, 梅德

回答

1

你應該分配file第一。

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

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

謝謝你! ))真的幫了我,我是盲人) – Dmitry 2010-07-24 02:41:46