我在代码中创建了一个代表医疗患者图表的对象。每个图表对应一名患者。在每个图表中,患者可以进行多次访问,或“遇到”。在每次遭遇中,患者可以有几份证明文件。
我无法循环遍历第一个索引[0]是另一个数组的数组。在下面的代码中,编译器抱怨(int j = 0; j< chart.DocumentIDs [i] .Length; j ++)无效,因为类型对象没有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; }
}
}
}
答案 0 :(得分:0)
由于DocumentIDs
是Object
的数组,因此通过索引检索到的任何内容都将属于Object
类型 - 并且需要进行类型转换才能访问其中任何一个具体属性。尝试通过迭代访问每个元素的Length
属性将是危险的:一个元素是Array
,一个是null
,一个是Integer
:只有其中一个拥有 Length
方法!
我同意Servy的评论:在声明显式类型而不是将属性填充到Object
数组中会更好。这种方法几乎肯定会比它的价值更麻烦。
答案 1 :(得分: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; }
}
}