【问题标题】:C# getting item from classC# 从类中获取项目
【发布时间】:2012-05-23 06:32:28
【问题描述】:

我有一个班级叫rateTime

class rateTime
{
   private List<string> t = new List<string>();
   private List<string> s = new List<string>();

   public rateTime(string[] time, string[] sender)
   {
       for (int i = 0; i < time.Length; i++)
       {
           t.Add(time[i]);
           s.Add(sender[i]);
       }
    }

    ~rateTime() { }

    public List<string> Time
    {
       get { return t;}
       set { t = value; }
    }

    public List<string> Sender
    {
       get { return s; }
       set { s = value; }
    }
}

我的组合框的DataSource设置如下:

rateTime rt = new rateTime(time, rateSender);
cb_rateTime.DataSource = rt.Time;
cb_rateTime.DisplayMember = "time";

在这两个列表中,我都有 28 个字符串。我将 List t 中的项目设置为组合框项目。如果我从索引为 10 的组合框中选择了一个项目,我想知道如何从索引为 10 的列表中获取字符串。

【问题讨论】:

  • 这是一个可怕的问题。在发布一堆只说“我有上课时间”的代码之前,您需要讨论一下您正在做什么以及您想要实现的目标。
  • 一个名为“YourMother”的用户的适当的、谴责的回应;)
  • 哇,自从我使用 c++ 以来,我还没有看到使用过析构函数 ~...()

标签: c# list class


【解决方案1】:

尝试 elementAt(index) - http://msdn.microsoft.com/en-us/library/bb299233.aspx
或索引器 - yourList[index]

【讨论】:

    【解决方案2】:

    我不知道我是否理解得很好,但是:

    var index = cb_rateTime.SelectedIndex;
    var itemS = rt.Sender.elementAt(index);
    

    var selected = cb_rateTime.SelectedText;
    var itemS = rt.Sender[selected];
    

    应该可以解决。

    【讨论】:

    • 我也是这么理解的。
    • 我猜的问题是rt 是在本地以不同的方法创建的,在回发中不可用。
    【解决方案3】:

    通过索引访问它,如

    MyRateTime.Sender[10]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 2014-06-20
      • 2016-06-24
      • 2019-07-03
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      相关资源
      最近更新 更多