繁体   English   中英

麻烦遍历数组,其中索引0是第二维数组

[英]Trouble Looping Through an Array Where Index 0 is a Second Dimension Array

我在代码中创建了一个代表医疗图表的对象。 每个图表对应一个患者。 在每个图表中,患者可以进行多次探访或“遭遇”。 在每次相遇中,患者可以拥有多个证明文件。

我在遍历第一个索引[0]是另一个数组的数组时遇到麻烦。 在下面的代码中,编译器抱怨(int j = 0; j <chart.DocumentIDs [i] .Length; j ++)无效,因为类型object没有length属性。 但是,DocumentIDs [i]的索引是Int32 []。

我正在尝试生成一个字符串输出,该输出将列出患者图表的所有内容,首先按相遇分类,然后按文档ID进行分类。 下面是我的代码。 如果有人可以指出我要去哪里。 我会很感激的。 谢谢。

public partial class MainWindow : Window
{    
    string ChartOutput = "";
    public MainWindow()
    {
        InitializeComponent();

        //initialize new chart object
        var charts = new[]
        {
            new 
            { 
                  MRN= 745654, 
                  Encounters = new int?[]
                  {
                      10,11,12
                  }, 
                  DocumentIDs = new object []
                  { 
                      new int[]
                      {
                          110, 1101
                      }, null, 112 
                  }, 
                  DocumentTypes = new object[]
                  {
                      new string[]
                      {
                          "Consents", "H&P"
                      }, null, "Intake Questionnaire"
                  }, 
                  DocumentNames = new object[]
                  { 
                      new string[]
                      {
                          "Eartube Surgery", 
                          "Well-Visit Physical"
                      }, null, "Health Survey"
                  } 
              }                        
        };

        foreach (var chart in charts)
        {
            ChartOutput += " Patient MRN#: " +  
                           chart.MRN.ToString() +
                           " Has the following visits: 
                           " + Environment.NewLine + 
                           Environment.NewLine ; 

            for(int i =0; I < chart.Encounters.Length; i++)
            {
                ChartOutput += "Visit Number: " + 
                               chart.Encounters[i].ToString() + 
                               Environment.NewLine;

                if (chart.DocumentIDs[i] != null)
                {  
                    for (int j = 0; j < chart.DocumentIDs[j].Length; j++)
                    {
                        ChartOutput += "  Document ID:" + 
                                          chart.DocumentIDs[j].ToString() + 
                                          Environment.NewLine + 
                                          "  Document Type: " + 
                                          chart.DocumentTypes[j].ToString() +
                                          Environment.NewLine + "  Document Name: " +
                                          chart.DocumentNames[j].ToString() +
                                          Environment.NewLine + Environment.NewLine;
                    }                                        
                }
                else 
                {
                    ChartOutput += "  Has No Documents" + 
                                   Environment.NewLine + 
                                   Environment.NewLine; 
                }
            } 
        }
    }
}




//ChartObject Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTester
{
    public class ChartObject
    {
        public ChartObject()
        {
            RecordClass = "Medical";
        }

        public string RecordClass {get; private set; }
        public int MRN { get; set; }
        public object [] Encounters { get; set; }
        public object [] DocumentIDs { get; set; }
        public object [] DocumentTypes { get; set; }
        public object [] DocumentNames { get; set; }
    }
}

}

由于DocumentIDs是一个Object数组,因此您通过索引对其检索的所有Object也将都是Object类型-并且在您访问其任何特定属性之前都需要进行类型转换。 尝试通过迭代访问每个元素的Length属性将很危险:一个元素是Array ,一个元素为null ,一个元素是Integer :只有其中一个具有 Length方法!

我同意Servy的评论:您最好声明显式类型,而不是将属性填充到Object数组中。 这种方法几乎肯定会带来更多麻烦,而不是值得的。

我通过使用锯齿状数组而不是对象数组完成了我打算要做的事情。 下面是更新和格式化的代码,以及生成的输出图像。

namespace CodeTester
{


    public partial class MainWindow : Window
    {    
        string ChartOutput = "";
        public MainWindow()
        {
            InitializeComponent();

            //initialize new chart object
            var charts = new[]
         {
            new             
            { 
                  MRN= 745654, 

                  Encounters = new int?[]
                  {
                    10,11,12 
                  }, 

                  DocumentIDs = new int?[][]
                  { 
                      new int?[]{110, 1101}, null, new int?[] { 112 }
                  }, 

                  DocumentTypes = new string[][]
                  {
                      new string[]{ "Consents", "H&P"}, null,  new string[] 
                      { "Intake Questionnaire" }
                  }, 

                  DocumentNames = new string[][]
                  { 
                      new string[]{ "Eartube Surgery", "Well-Visit Physical"}, 
                      null, new string[] { "Health Survey" }
                  } 
              }                        
         };

                foreach (var chart in charts)
                {
                    ChartOutput += "Patient MRN#: " +  chart.MRN.ToString() +
                    " Has the following visits: " 
                   + Environment.NewLine + Environment.NewLine ; 

                        for(int i =0; i< chart.Encounters.Length; i++)
                        {
                            ChartOutput += "Visit Number: " +
                           chart.Encounters[i].ToString() + Environment.NewLine;

                            if (chart.DocumentIDs[i] != null)
                            {
                                for (int j = 0; j < chart.DocumentIDs[i].Length; j++)
                                    {
                                        ChartOutput += "    Document ID:" +  
                                        chart.DocumentIDs[i][j].ToString() + 
                                        Environment.NewLine + 
                                        "    Document Type: " + 
                                        chart.DocumentTypes[i][j].ToString() + 
                                        Environment.NewLine + 
                                        "    Document Name: " + 
                                        chart.DocumentNames[i][j].ToString() + 
                                        Environment.NewLine + 
                                        Environment.NewLine;                                     
                                    }                                        
                            }

                            else { ChartOutput += "    Has No Documents" +
                            Environment.NewLine + Environment.NewLine; }
                        }

                }

        }

        private void Run_Click(object sender, RoutedEventArgs e)
        {  
            MessageBox.Show(ChartOutput);            
        }
    }
}



// ChartObject Class

namespace CodeTester
{
    public class ChartObject
    {
        public ChartObject()
        {
            RecordClass = "Medical";
        }

        public string RecordClass { get; private set; }
        public int MRN { get; set; }
        public int[] Encounters { get; set; }
        public int?[][] DocumentIDs { get; set; }
        public string[][] DocumentTypes { get; set; }
        public string[][] DocumentNames { get; set; }

    }
}

输出量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM