2016-09-24 4 views
-1

C#では、呼び出し元のプログラムに配列を返すことは可能ですか?それが不可能な場合は、すべて可能ではないと言ってください。もう1つの方法は、長い文字列を作成し、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

_return yearsfiled_をwhileループの外側に移動すると、おそらくこのコードから何かを得る可能性が高くなります。 – Steve

+0

リターンは外側に移動する必要があります。それは私の間違いでした。完了しました。ありがとう – Unnikrishnan

+0

もちろん、配列を返すことは可能です。しかし、そのロジックのためにコードを完全に修正する必要があります。たとえば、空白行の役割や、空白以外の行のチェックは何ですか。 –

答えて

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には本当に感謝しています。

関連する問題