0

我在我的代码中创建了一个代表医疗患者图表的对象。每个图表对应一位患者。在每个图表中,患者可以进行多次访问或“遭遇”。在每次会面中,患者可以有几个支持文件。

我在遍历第一个索引 [0] 是另一个数组的数组时遇到问题。在下面的代码中,编译器抱怨 (int j = 0; j < chart.DocumentIDs[i].Length; j++) 无效,因为类型对象没有长度属性。但是,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; }
    }
}

}

4

2 回答 2

0

由于DocumentIDs是 的数组Object,因此您通过索引检索到的任何内容也将是类型Object- 并且在您可以访问其任何特定属性之前需要进行类型转换。并且试图Length通过迭代来访问每个元素的属性将是危险的:一个元素是 an Array,一个是null,一个是 an Integer:只有其中一个有方法Length

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

于 2013-05-21T20:06:11.627 回答
0

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

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; }

    }
}

输出

于 2013-05-22T14:41:23.783 回答