2
Answers

Error passing list and array to web service

Ask a question
I am trying to convert VB code to C# for consuming a web service, but something is wrong in my C# syntax.

This is the VB code: formattedBlockList.AddRange(MDM_Ref.GetLatestReadingsByMeterListFormattedBlock(meterList.ToArray, startdate.AddSeconds(-1), enddate, interval, {""}, ""))

This is my C# code: formattedBlockList.AddRange(MDM_Ref.GetLatestReadingsByMeterListFormattedBlock(meterList.ToArray, startdate.AddSeconds(-1), enddate, interval, arr1, ""));

C# does not allow me to use {""} for the empty string array, so I created an empty string array arr1.

Here is the complete code:

public List<formattedBlock> getReadingsByMeterListFormattedBlock()
    {
        List<formattedBlock> formattedBlockList = new List<formattedBlock>();
        System.DateTime startdate = Convert.ToDateTime("05/01/2012");
        System.DateTime enddate = Convert.ToDateTime("05/04/2012");
        string interval ="D";
        List<meterID> meterList = new List<meterID>();
        int i = 0;
        int meter = 30000;
        int lastMeter = 30000;
        for (i = meter; i <= lastMeter; i++) {
            meterList.Add(new meterID { meterNo = i.ToString() });
        }
        string[] arr1 = {""};
        try {
            formattedBlockList.AddRange(MDM_Ref.GetLatestReadingsByMeterListFormattedBlock(meterList.ToArray, startdate.AddSeconds(-1), enddate, interval, arr1, ""));           
        }
         catch (Exception ex) {     
        }

        return formattedBlockList;
}

Here is the web service:
public formattedBlock[] GetLatestReadingsByMeterListFormattedBlock(meterID[] meterIDs, System.DateTime startDate, System.DateTime endDate, string formattedBlockTemplateName, string[] fieldName, string lastReceived) {
            object[] results = this.Invoke("GetLatestReadingsByMeterListFormattedBlock", new object[] {
                        meterIDs,
                        startDate,
                        endDate,
                        formattedBlockTemplateName,
                        fieldName,
                        lastReceived});
            return ((formattedBlock[])(results[0]));
        }

Answers (2)