0

これはメソッド内にある List<> です

public void MenuList()
{
    List<string> flavors = new List<string>();
    flavors.Add("Angus Steakhouse");
    flavors.Add("Belly Buster");
    flavors.Add("Pizza Bianca");
}

今、私は新しい方法を入れました

public int GetSizePrices(int num)
{
   this.MenuList ???    
}

GetSizePrices メソッド内でフレーバー オブジェクトを使用するにはどうすればよいですか? ありがとう。

4

2 に答える 2

0

これを実現するには明らかにさまざまな方法があり、設計要件に基づいています。

ここでは、C# の初心者であり、この旅を始めるための次の 2 つの簡単な方法を検討してください (既存のコードにできるだけ慣れるために、慣用的な C# は意図的に除外しました)。


オプション 1 - パラメータとして渡します。

public List<string> BuildMenuList()
{
    List<string> flavors = new List<string>();
    flavors.Add("Angus Steakhouse");
    flavors.Add("Belly Buster");
    flavors.Add("Pizza Bianca");

    return flavors;
}

public int GetSizePrices(int num, List<string> menuList)
{
   // access to menuList
   var cnt = menuList.Count();
}


オプション 2 - プロパティとして利用できるようにする

public class Menu
{
    // A public property that is accessible in this class and from the outside
    public List<string> MenuList { get; set;}

    public Menu()
    {
        // Populate the property in the constructor of the class
        MenuList = new List<string>();
        MenuList.Add("Angus Steakhouse");
        MenuList.Add("Belly Buster");
        MenuList.Add("Pizza Bianca");
    }

    public int GetSizePrices(int num)
    {
        // your implementation details here...
        return MenuList.Count();
    }
}

それが役に立てば幸い。

于 2012-04-21T04:31:57.370 に答える
0

次のようなものを探していると思いますか?:

public Class SomeClass
{
  public IEnumerable<string> MenuList()
  {
    List<string> flavors = new List<string>();
    flavors.Add("Angus Steakhouse");
    flavors.Add("Belly Buster");
    flavors.Add("Pizza Bianca");
    return flavors;
  }

  public int GetSizePrices(int num)
  {
   // No idea what to do with num
   return this.MenuList().Count();
  }
}
于 2012-04-21T03:10:49.687 に答える