2013-05-21 84 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數組中。這種方法幾乎肯定會比它的價值更麻煩。