【问题标题】:Passing a List<string[]> into a method accepting string[] as parameter将 List<string[]> 传递给接受 string[] 作为参数的方法
【发布时间】:2013-12-04 02:55:39
【问题描述】:

伙计们,我正在将一些参数传递给 WPF 应用程序的新窗口,如下所示

List<string[]> liststat = conf.getlistbytype(type);
if ((liststat == null) || (liststat.Count == 0))
{
    MessageBox.Show("There is no stat of that type in this stat server");
}
else
{
    CalculatedStat3 prompt = new CalculatedStat3(statwin, conf, liststat, this);
    prompt.Show();

但是目标页面的构造器是as

 public CalculatedStat3(statwindow statwin, ConfigLayer conf, string[] statsname, CalculatedStat2 backscreen)
 {
 }

当我遇到错误时,我应该怎么做才能纠正这个问题

'RCCV_Version_2.CalculatedStat3.CalculatedStat3(RCCV_Version_2.statwindow, rmad_wpf_lib.ConfigLayer, string[], RCCV_Version_2.CalculatedStat2)' has some invalid arguments

【问题讨论】:

  • List和string[]有很大区别。您需要一个处理列表下所有不同字符串 [] 的 foreach 循环。

标签: c# wpf methods parameter-passing


【解决方案1】:

liststat 是字符串数组的列表,而不是 CalculatedStat3 方法接受的单个字符串数组,这就是您收到编译错误的原因。

要么传递列表的第一个元素

CalculatedStat3 prompt = new CalculatedStat3(statwin, conf, liststat[0], this);

或更改方法以接受字符串数组列表

public CalculatedStat3(statwindow statwin, ConfigLayer conf, List<string[]> statsname, CalculatedStat2 backscreen)

选择最适合您和您的需求的解决方案。

【讨论】:

    【解决方案2】:

    第一个解决方案,只获取第一个数组

    CalculatedStat3 prompt = new CalculatedStat3(statwin, conf, liststat.First(), this);
    

    其次,扁平化你的收藏

    var stats = liststat.SelectMany(x=>x).ToArray();
    CalculatedStat3 prompt = new CalculatedStat3(statwin, conf, stats, this);
    

    在第一个解决方案中,您将仅获得第一个统计数据数组,在第二个解决方案中,您将获得连接所有统计数据的大数组。我认为秒解决方案符合您的要求。

    【讨论】:

    • 是的,这是我正在寻找的第二种解决方案。谢谢+1
    【解决方案3】:

    不清楚你想为构造函数使用哪个string[],所以我只取第一个:

    else
    {
       string[] firstStat = liststat.First();
       CalculatedStat3 prompt = new CalculatedStat3(statwin, conf, firstStat, this);
    

    如果要创建多个CalculatedStat3

    else
    {
       CalculatedStat3[] allCalculatedStat3 = liststat
          .Select(s => new CalculatedStat3(statwin, conf, s, this))
          .ToArray();
       foreach(CalculatedStat3 stat in allCalculatedStat3)
           stat.Show();
    

    【讨论】:

      【解决方案4】:
      if ((liststat == null) || (liststat.Count == 0))
      {
          MessageBox.Show("There is no stat of that type in this stat server");
      }
      else
      {
          var list = new List<string>();
          liststat.ForEach(q => list.AddRange(q));
          CalculatedStat3 prompt = new CalculatedStat3(statwin, conf, list.ToArray(), this);
          prompt.Show();
      

      【讨论】:

      • 您需要传递list.ToArray() 而不是list,因为该方法采用string[],而不是List&lt;string&gt;
      【解决方案5】:

      您还可以按如下方式连接所有数组:

      List<string> finalList = new List<string>();
      
      foreach (string[] strings in liststat)
          finalList.AddRange(strings);
      
      string[] finalArray = finalList.ToArray();
      

      然后将finalArray传递给方法。


      这与daryal 所做的相同,只是没有 LINQ。随心所欲。

      【讨论】:

        猜你喜欢
        • 2015-11-20
        • 2019-08-05
        • 2014-04-06
        • 2015-07-02
        • 2012-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多