2016-04-10 5 views
1

PrintCounters()メソッドを使用してmycounters[0]を印刷しようとしていますが、PrintCounters(mycounters[0])が機能していないようですが、それは構文の問題ですか?私はプログラムをデバッグしていながら、そこまでenter image description herePrintCounters()を呼び出すことは実行されていません、なぜですか?

ショーに注目し、これは私のクラスファイルであるが、すべてのものを固定しているので、

using System; 
using System.Collections.Generic; 
namespace Counter 
{ 
class MainClass 
{ 
    List<Counter> counters = new List<Counter>(); 
    public static void PrintCounters (IEnumerable<Counter> counters) 
    { 
     foreach (Counter c in counters) 
     { 
      Console.WriteLine("{0} is {1}", c.Name,c.Count); 

     } 
    } 
    public static void Main (string[] args) 
    { 
     List<Counter> mycounters = new List<Counter>(); 
     mycounters.Add(new Counter ("counter1")); 
     mycounters.Add (new Counter ("counter2")); 
     mycounters [2] = mycounters [0]; 

     for (int i = 0; i < 4; i++) { 

      mycounters[0].increment(); 

     } 

     for (int i = 0; i < 9; i++) { 
      mycounters[1].increment(); 

     } 

     PrintCounters (mycounters); 
     mycounters [2].reset(); 
     PrintCounters (mycounters); 
    } 

} 

は、エラーがありません。

namespace Counter 
{ 
public class Counter 
{ 
    private int _count; 
    public int Count 
    { 
     get{ 
      return _count; 

     } 


    } 
    private string _name; 
    public string Name 
    { 
     get { 
      return _name; 
     } 

     set{ 
      _name = value; 
     } 

    } 

    public Counter (string name) 
    { 
     _name = name; 
     _count = 0; 

    } 

    public void increment() 
    { 
     _count++; 
    } 

    public void reset() 
    { 
     _count = 0; 
    } 




} 

}

+0

あなたの 'PrintCounters'メソッドは' Counter'の配列を必要とします。あなたはそれをCounter(myCou nter [0] ==あなたの 'mycounters'リストの最初の要素):あなたは何を印刷したいですか?リスト全体か、最初の要素か? –

答えて

2

PrintCountersを期待カウンタオブジェクト、Counter[]のアレイであることを唯一の引数ので、動作しないことが予想されます。引き数のタイプをIEnumerable<Counter>に変更するだけで済みます。そうすることで、それは動作します。

問題を解決するもう一つの方法は、ToArrayメソッドを呼び出してリストに基づいて配列を作成し、これをPrintCountersの引数として渡すことです。それがより一般的であるため、

PrintCounters(mycounters.ToArray()); 

しかし、私は最初のアプローチを好むだろう。一般に、インプリメンテーションではなくインターフェイスでプログラミングするのがよい方法です。 PrintCountersについて考えてみましょう。 Counterオブジェクトのコレクションのアイテムを繰り返し処理し、各アイテムの印刷はNameCountとしたいだけです。これが配列かリストなのかどうかはまったく重要ですか?私たちが欲しい唯一のものは、アイテムを考え直すために列挙子です。それでは、これの前提条件は何ですか?唯一の前提条件は、PrintCountersで渡されるタイプがIEnumerable<Counter>を実装することです。一度List<Counter>を渡してから、私たちの心が変わってCounter[]を渡したい場合は、PrintCountersメソッドで何も変更する必要はありません。

+0

私はプログラミングに新しいので、次の質問については申し訳ありません。どのように私は引数の種類を変更することができます、これはprintcountersで変更する必要がありますか私のクラスで修正する必要がありますか? –

+0

あなたは申し訳ありません。これはメソッドのシグネチャ、 'public static void PrintCounters(IEnumerable counters)'で変更する必要があります。 – Christos

+0

私はちょうどタイプを変更するために言ったが、それでもまだprintcounter(mycounter [0])にエラーが起こっています。 'Counter.mainclass.PrintCounters(system.collection.generic IEnumerable )」に最適なオーバーロードされたメソッドがいくつかの引数を持っています。 –

0

あなたは関数定義その後、インデックスすなわち mycounters [0]を使用して関数にパラメータを渡すされた場合は、パラメータとして全体mycountersリストを渡したい場合は

public static void Main (string[] args) 
    { 
    List<Counter> mycounters = new List<Counter>(); 
    mycounters.Add(new Counter ("counter1")); 
    mycounters.Add (new Counter ("counter2")); 
    mycounters [2] = mycounters [0]; 

    for (int i = 0; i < 4; i++) { 

     mycounters[0].increment(); 

    } 

    for (int i = 0; i < 9; i++) { 
     mycounters[1].increment(); 

    } 

    PrintCounters (mycounters); 
    } 
この

public static void PrintCounters(Counter counter) 
    { 
     Console.WriteLine("{0} is {1}", counter.Name, counter.Address); 
    } 

のように変更する必要がありますか

public static void PrintCounters(IEnumerable<Counter> counters) 
    { 
     foreach (Counter c in counters) 
     { 
      Console.WriteLine("{0} is {1}", c.Name, c.Address); 

     } 
    } 
関連する問題