2017-04-14 10 views
0
public static int getInfo(string info) 
    { 
     string inputValue; 
     int infor; 
     Console.WriteLine("Information of the employee: {0}", info); 
     inputValue = Console.ReadLine(); 
     infor = int.Parse(inputValue); 
     return infor; 

    } 

上記のコードでは、人の名前(文字列)と給与(int)を取得するにはどうすればよいですか?仕様では、情報検索のためにメソッドを2回呼び出す必要があります。同じメソッドから文字列とintを返す

+0

したがって、メソッドで文字列またはintのいずれかを返すようにしますか? –

+2

C#7 +タプルを使用する:https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/#user-content-tuples。 –

+1

これはXYの問題を叫びます(あなたが意味するものがわからない場合は、Googleで、携帯では便利なリンクはありません)。あなたはこれで何を達成しようとしていますか? –

答えて

5

あなたが情報を保持しているクラスや給与

public static int getInfo(string info,int salary) 

を作成したり、クラスを作成する場合は、別々にまたはより良い、それを渡すことができ、

public class MyInfo 
    { 
     public int info { get; set; } 
     public int salary { get; set; } 

    } 

とメソッドのシグネチャはようになり、

public static int getInfo(MyInfo info) 

文字列とintの両方を返す場合は、型クラスMyInfo

public static MyInfo getInfo(MyInfo info) 
+1

私はOPが彼の方法で情報を返すことを望んでいると思います。 –

+0

@PieterWitvoet okそのまま編集されます。 2つ以上のC#で金を得る! :) – Sajeetharan

+0

@Sajeetharan 2ゴールド!嫉妬しい!幸運にも、あなたはそれを速く得るでしょう。 – EpicKip

1

あなたはそれがこのように両方の値でタプルを返す作ることができます:

internal Tuple<int, string> GetBoth(string info) 
{ 
    string inputValue; 
    int infor; 
    Console.WriteLine("Information of the employee: {0}", info); 
    inputValue = Console.ReadLine(); 
    infor = int.Parse(inputValue); 
    return new Tuple<int, string>(infor, inputValue); 
} 

internal void MethodImCallingItFrom() 
{ 
    var result = GetBoth("something"); 
    int theInt = result.Item1; 
    string theString = result.Item2; 
} 
-1

は、なぜあなたはわずか2のサイズで文字列配列を返すいけませんか?最初に(配列[0])文字列があり、第二に(配列[0])INTがある...私は願っています

public static string[] GetInfo (string info) 

を私はあなたの質問を理解して右^^

+0

私はTryParseを使ってこれを行うことができました。それ以外の場合、私はメソッドを2回呼び出す必要があります。 :( –

+0

もできます 'string stringname + Convert.ToString(int intname);' –

-1

あなたは人のクラスを作成する必要があり、名前と給与

を読ん
public class Person 
{ 
    public string Name {get; set;} 
    public decimal Salary {get; set;} 
} 

とあなたの関数は次のようになります。

public static Person getInfo(string info) 
{ 
    string inputName; 
    string inputSalary; 

    Console.WriteLine("Information of the employee: {0}", info); 

    Console.WriteLine("Name:"); 
    inputName = Console.ReadLine(); 

    Console.WriteLine("Salary:"); 
    inputSalary = Console.ReadLine(); 

    Person person = new Person(); 
    person.Name = inputName; 
    person.Salary = int.Parse(inputSalary); 
    return person; 

} 
-1

あなたは1つのメトをしたい場合D、私はジェネリックを使用し、異なる種類の情報を返すために:

public static T GetInfo<T>(string name); 

// This can be called as following: 
string name = GetInfo<string>("name"); 
int salary = GetInfo<int>("salary"); 

一つの問題は、しかし、あります:私たちの方法は、任意の型を返すことができながらConsole.ReadLineは、stringを返します。どのように文字列を 'ターゲット'型に変換できますか? Tをチェックして、サポートしたいすべてのタイプのカスタムロジックを書くことができますが、それは面倒で扱いにくいものです。

public static T GetInfo<T>(string name, Func<string, T> convert); 

// This can be called as following: 
string name = GetInfo<string>("name", s => s); 
int salary = GetInfo<int>("salary", int.Parse); 

今どのようにあなたがそのメソッドを実装します:より良い解決策は、発信者が特定の型に文字列を変換する方法を知っている小さな機能に渡すようにするのですか?

public static T GetInfo<T>(string name, Func<string, T> convert) 
{ 
    Console.WriteLine("Please enter " + name); 
    string input = Console.ReadLine(); 
    return convert(input); 
} 

いくつかの注意事項:

  • タイプのパラメータは、多くの場合、ちょうどTを命名しているが、私はそれが便利な彼らに、このようなTInfoなど、より説明的な名前を与えることを見つけます。
  • コンパイラに型を推測するのに十分な情報があるため、<string><int>部分は除外できます。
  • 例を短く簡単にするためにエラー処理を残しました。プロダクションコードでは、無効な入力を処理する戦略を考え出すことになります。
0
public static void Main(string[] args) 
    { 
     string eName, totalSales; 
     double gPay, tots, fed, sec, ret, tdec,thome; 
     instructions(); 
     eName = getInfo("Name"); 
     totalSales = getInfo("TotalSales"); 
     tots = double.Parse(totalSales); 
     gPay = totalGpay(tots); 

}

public static string getInfo(string info) 
    { 
     string inputValue; 
     Console.WriteLine("Information of the employee: {0}", info); 
     inputValue = Console.ReadLine(); 

     return inputValue; 

    } 

これは、要求されたものです。皆さんに言及された他のトリックでやっていたかもしれません。とにかくありがとうございました。

関連する問題