2016-09-24 45 views
-1

是否有可能在C#中將數組返回給調用程序?如果不可能,請說這不是全部可能。另一種方法是創建一個長字符串並使用string.split()。但那看起來不太好。 ExamnationOfReturnsFiled(「ABCDE1234E」)//調用程序。是否有可能在C#中將數組返回給調用程序?

public yearsfiled[] ExamnationOfReturnsFiled(string panreceived) //function. 
{    
    int k = 0; //to increment the array element. 
    string item = panreceived; //string value received call program. 

    string[] yearsfiled = new string[20];//Declaring a string array. 
    Regex year = new Regex(@"[0-9]{4}-[0-9]{2}");//to capture 2012-13 like entries. 
    using (StreamReader Reader1 = new StreamReader(@"C: \Users\Unnikrishnan C\Documents\Combined_Blue_Book.txt")) 
    { 
     Regex tofindpan = new Regex(item);//Regular Expression to catch the string from the text file being read. 
     bool tosearch = false; 
     Regex blank = new Regex(@"^\s*$"); //to detect a blank line. 
     while ((str.line1 = Reader1.ReadLine()) != null) 
     { 
      Match Tofindpan = tofindpan.Match(@"[A-Z]{5}[0-9]{4}[A-Z]{1}"); 
      Match Blank = blank.Match(line1); 
      if (Blank.Success) 
      { 
       tosearch = false; 
      } 
      if (Tofindpan.Success) 
      { 
       tosearch = true; //when true the 
      } 
      if (tosearch == true) 
      { 
       Match Year = year.Match(str.line1); 
       if (Year.Success) 
       { 
        yearsfiled[k] = Year.Value; 
        k++; 
       } 
      } 
     } 
     return yearsfiled; 
    } 

}

+0

如果您在while循環之外移動您的_return年份_filed_,您可能有更好的機會從此代碼中獲取某些內容。 – Steve

+0

返回應該移到外面。這是我的錯誤。完成。謝謝 – Unnikrishnan

+0

當然可以返回一個數組。但是,您應該徹底修改您的代碼以獲取其邏輯。例如,panreceived的作用是什麼,或者檢查非空行。 –

回答

1

//功能

你是返回類型不變量的名稱像上面

0

你應該返回一個string[]。你的返回類型yearsfiled[]是一個變量名,而不是一個類型名稱

0

//從調用程序更改方法簽名。測試成功。

string[] yearsfiled = new string[20]; 
yearsfiled = ExamnationOfReturnsFiled(item1); 

//函數名稱修改如下。

public static string[] ExamnationOfReturnsFiled(string panreceived) 
{ 
    Everything else as in the original post. 
} 

//已經過測試。並找到成功。非常感謝@Midhun Mundayadan和@Eavidan。

相關問題