2013-05-21 88 views
0

我在代码中创建了一个代表医疗患者图表的对象。每张图表对应一名患者。在每张图表中,病人可以有很多次访问,或者“遭遇”。在每次相遇中,病人可以有几个支持文件。通过索引0是第二维阵列的阵列循环故障

我无法循环访问第一个索引[0]是另一个数组的数组。在下面的代码中,编译器抱怨(int j = 0; j < chart.DocumentIDs [i] .Length; j ++)是无效的,因为type对象没有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; } 
    } 
} 

}

+4

1)花一些时间格式化你的代码。不应该有任何或至少不太多的水平滚动。 2)不要使用像这样的对象[]类型。这个问题根本。它看起来像在上下文中,您应该创建新的命名类型来表示一组属性而不是数组,因为它是已知静态(也是不同)类型的固定数量的项目。一个数组只是错误的;做一个新班。 – Servy

+0

你解释了为什么邂逅有文件。为什么图表有文件?为什么这些属性都是对象类型?是否有一个需要所有拳击的约束?不要在类标识符中放置“对象”......它没有意义。 –

+0

拳击的原因是因为每个属性中的第一个元素可能是另一个数组,有时可能不是。 –

回答

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

    } 
} 

Output

0

由于DocumentIDsObject一个数组,任何你根据索引检索将Object类型,以及 - 和需要类型转换,然后才能访问它的任何特殊属性。并试图通过迭代访问每个元素的Length属性将是危险的:一个元素是Array,一个是null,并且一个是Integer:其中只有一个 a Length方法!

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