Question Add items to an array

hcwork

New member
Joined
Nov 30, 2010
Messages
2
Programming Experience
5-10
Hi Folks, I am hoping that someone can help me out here...
VB.NET:
// Create the service lines for this encounter
ServiceLineReq serviceLine1 = new ServiceLineReq();
serviceLine1.ServiceStartDate = DateTime.Today.AddDays(-1);
serviceLine1.ProcedureCode = "99201";
serviceLine1.DiagnosisCode1 = "600.00";
serviceLine1.Units = 1;
serviceLine1.UnitCharge = 3.4;
ServiceLineReq serviceLine2 = new ServiceLineReq();
serviceLine2.ServiceStartDate = DateTime.Today.AddDays(-1);
serviceLine2.ProcedureCode = "62311";
serviceLine2.DiagnosisCode1 = "495.4";
serviceLine2.Units = 2;
serviceLine2.UnitCharge = 6.5;
// Make sure you set the encounter's practice, service location, patient, case, and
// provider (and any other objects you create relating to the encounter)
newEncounter.Practice = practice;
newEncounter.ServiceLocation = location;
newEncounter.Patient = patient;
newEncounter.Case = encounterCase;
newEncounter.RenderingProvider = provider;
newEncounter.ServiceLines = new ServiceLineReq[] { serviceLine1, serviceLine2 };

I was given this example code above from the company who puts out this API. As you can see, it looks like you have to create a ServiceLineReq item for each item you are passing in. The issue for us is that we do not know how many items we are passing in. So, I assumed that I could pass in an array of items ServiceLineReq(). So, I tried the following
VB.NET:
   Private Function ReturnServiceRequest(ByVal patientId As Integer, ByVal pnoteID As Integer, ByVal pt As nextEMRDataLayer.Patients, ByVal pnList As List(Of nextEMRDataLayer.progressNotes))
        Dim svLineTemp() As ServiceLineReq = {}
        Dim cptList As New List(Of nextEMRDataLayer.Patients)
        Dim icd9List As New List(Of nextEMRDataLayer.Patients)
        Dim svLineCtr As Integer
        Dim billLineCtr As Integer
        ' get procedures associated with the pnote/encounter
        cptList = pt.selectPatientProgressNoteCPTAndBilling(patientId, pnoteID)
        If cptList.Count > 0 Then
            svLineCtr = cptList.Count
            'svLineTemp = New ServiceLineReq() {}
            For i = 0 To cptList.Count - 1
                Dim cmpDate As String = cptList(i).procedureDate.ToString
                If cmpDate.StartsWith("1/1/0001") Then
                    svLineTemp(i).ServiceStartDate = pnList(0).dos.ToShortDateString
                Else
                    svLineTemp(i).ServiceStartDate = cptList(i).procedureDate.ToString
                End If
                svLineTemp(i).ServiceStartDateSpecified = True
                svLineTemp(i).ProcedureCode = cptList(i).cptCode
                svLineTemp(i).UnitsSpecified = True
                svLineTemp(i).Units = 1


                svLineTemp(i).ServiceStartDate = svLineTemp(i).ServiceStartDate

                icd9List = pt.selectPatientICD9ByProgressNote(patientId, pnoteID)
                If icd9List.Count > 0 Then
                    For j = 0 To icd9List.Count - 1
                        If j = 0 Then
                            svLineTemp(i).DiagnosisCode1 = icd9List(j).icd9Code
                        End If
                        If j = 1 Then
                            svLineTemp(i).DiagnosisCode2 = icd9List(j).icd9Code
                        End If
                        If j = 2 Then
                            svLineTemp(i).DiagnosisCode3 = icd9List(j).icd9Code
                        End If
                        If j = 3 Then
                            svLineTemp(i).DiagnosisCode4 = icd9List(j).icd9Code
                        End If
                    Next
                End If
            Next

        End If
        Return svLineTemp
    End Function

The issue is that when I git svLineTemp(i).ServiceStartDate it always says the Index was outside the bounds of the array. How can I loop through and add items to the array and then return the whole array?
 
You have declared an empty array and not resized it, so there are no indexes in that array. To make things simpler array-wise use a List(Of ServiceLineReq) and add ServiceLineReq instances to it, use ToArray to convert that to array.

You're using a For-Next loop, but I don't see there is any need for the i indexer so you should use a For-Each loop instead.
About the j-Ifs, do you really need to check if j=1 when you know j=0 ? ElseIf can be used, so can a Select-Case.
The function is mission a return type also.
 
This is what I ended up with, thanks for the direction!

VB.NET:
    Private Function ReturnServiceRequest(ByVal patientId As Integer, ByVal pnoteID As Integer, ByVal pt As nextEMRDataLayer.Patients, ByVal pnList As List(Of nextEMRDataLayer.progressNotes))

        Dim MyArray As New ArrayList
        Dim CurrentItem As ServiceLineReq

        Dim cptList As New List(Of nextEMRDataLayer.Patients)
        Dim icd9List As New List(Of nextEMRDataLayer.Patients)
        Dim svLineCtr As Integer
        Dim billLineCtr As Integer
        ' get procedures associated with the pnote/encounter
        cptList = pt.selectPatientProgressNoteCPTAndBilling(patientId, pnoteID)
        If cptList.Count > 0 Then
            svLineCtr = cptList.Count
            For i = 0 To cptList.Count - 1

                CurrentItem = New ServiceLineReq()
                Dim cmpDate As String = cptList(i).procedureDate.ToString
                If cmpDate.StartsWith("1/1/0001") Then
                    CurrentItem.ServiceStartDate = pnList(0).dos.ToShortDateString
                Else
                    CurrentItem.ServiceStartDate = cptList(i).procedureDate.ToString
                End If
                CurrentItem.ServiceStartDateSpecified = True
                CurrentItem.ProcedureCode = cptList(i).cptCode
                CurrentItem.UnitsSpecified = True
                CurrentItem.Units = 1

                icd9List = pt.selectPatientICD9ByProgressNote(patientId, pnoteID)
                If icd9List.Count > 0 Then
                    For j = 0 To icd9List.Count - 1
                        If j = 0 Then
                            CurrentItem.DiagnosisCode1 = icd9List(j).icd9Code
                        ElseIf j = 1 Then
                            CurrentItem.DiagnosisCode2 = icd9List(j).icd9Code
                        ElseIf j = 2 Then
                            CurrentItem.DiagnosisCode3 = icd9List(j).icd9Code
                        ElseIf j = 3 Then
                            CurrentItem.DiagnosisCode4 = icd9List(j).icd9Code
                        End If
                    Next
                End If
                MyArray.Add(CurrentItem)
            Next

        End If
        Return DirectCast(MyArray.ToArray(GetType(ServiceLineReq)), ServiceLineReq())
    End Function
 
Back
Top