2017-03-20 18 views
0

私はメソッドがあり、パラメータを使用します。しかし、私は見つけることができなかった何かが足りない。私は3つのパラメータが最初に長いIDを持っている、私はそのIDを送信して、私はそれを処理していると私はworkerName(第2パラメータ)とworkerTitle(第3パラメータ)を作成しています。 私の方法はです。無効な引数 "out"パラメータ

public static void GetWorkerInfo(long workerID, out string workerName, out string workerTitle) 
{ 
    // Some code here 
} 

ここで私のメソッドを呼び出しています。

GetWorkerInfo(workerID, out workerName, out workerTitle) 
+1

"workerName"と "workerTitle"はメソッドを呼び出す前にどのように定義されていますか? – Jure

+2

このエラーが発生しています**現在のメソッドを離れる前にoutパラメータ 'workerTitle'を割り当てる必要があります** –

+0

@Jureは例外を受けている理由を教えてください – Ilaria

答えて

3

のようにそれを呼び出す:

GetWorkerInfo(workerID, out var workerName, out var workerTitle); 

ただし、C#7に切り替える前に、呼び出しの外にoutのパラメータとして渡す変数を宣言する必要があります。

string workerName; 
string workerTitle; 
GetWorkerInfo(workerID, out workerName, out workerTitle); 
+0

ありがとう私がそれを逃した理由を、私が使っているのは初めてのことです。もう一度ありがとう – Ilaria

1
public static void GetWorkerInfo(long workerID, out string workerName, out string workerTitle) 
{ 
    workerName = ""; 
    workerTitle = ""; 
} 

、あなたはこのように、メソッド呼び出しの一部として出力パラメータを宣言することができますC#7でこの

long workerID = 0; 
string workerTitle; 
string workerName; 
GetWorkerInfo(workerID, out workerName, out workerTitle); 
1

エラーは、outパラメータとして指定されたパラメータに値を割り当てなかったためです。念頭に置いて、メソッド本体の中にこれらのパラメータにいくつかの値を割り当てる必要があります。

public static void GetWorkerInfo(long workerID, out string workerName, out string workerTitle) 
{ 
    workerName = "Some value here"; 
    workerTitle = "Some value here also"; 
    // rest of code here 
} 

これでコードは問題なくコンパイルされます。