c# - Trouble Looping Through an Array Where Index 0 is a Second Dimension Array -
i've created object in code represents medical patient's chart. each chart corresponds 1 patient. within each chart, patient can have many visits, or "encounters". within each encounter, patient can have several supporting documents.
i having trouble looping through arrays first index [0] array. in code below, compiler complaining (int j = 0; j < chart.documentids[i].length; j++) invalid because type object has no length property. however, index @ documentids[i] int32[].
i trying generate string output list contents of patient's chart, broken down first encounter , document id. below code. if can point out i'm going wrong. i'd appreciate it. thanks.
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 following visits: " + environment.newline + environment.newline ; for(int =0; < chart.encounters.length; i++) { chartoutput += "visit number: " + chart.encounters[i].tostring() + environment.newline; if (chart.documentids[i] != null) { (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; } } }
}
since documentids
array of object
, retrieve indexing of type object
- , require typecast before can access of specific properties. , trying access length
property of each element iterating going dangerous: 1 element array
, 1 null
, , 1 integer
: 1 of has length
method!
i agree servy's comment: better off declaring explicit types instead of stuffing properties object
arrays. approach going more trouble it's worth.
Comments
Post a Comment