I have this ArrayList that consist of list of structure and I would like to use the content of the ArrayList as the return type for the function bellow. Can anyone help please.

public struct DeviceSchedules
        {
            public string StartDate;
            public string EndDate;
            public string Interval;
            public string DeviceTimeZone;
            public string GLRID;
            public string TrackUID;
        }

        [WebMethod()]
        public DeviceSchedules theSchedules()
        {
            ArrayList ZoneArray = new ArrayList();
            ZoneArray.Add("PST");
            ZoneArray.Add("MST");
            ZoneArray.Add("CST");
            ZoneArray.Add("EST");

            DeviceSchedules Schedules = new DeviceSchedules();
            ArrayList GroupSchedule = new ArrayList();

            Int16 Zone = 1;
            DateTime Begining = DateTime.Now.AddHours(Zone);
            DateTime End = DateTime.Now.AddHours(Zone);
            //int Zone = 3;
            Int32 TrackID = 9999;
            Int32 ScheduleID = 400;
            int Frequency = 40;
            for (int index = 0; index <= 5; index++)
            {
                Schedules.StartDate = Convert.ToString(Begining);
                Schedules.EndDate = Convert.ToString(End);
                Schedules.Interval = Convert.ToString(Frequency);
                Schedules.DeviceTimeZone = Convert.ToString(ZoneArray[Zone]);
                Schedules.GLRID = Convert.ToString(ScheduleID);
                Schedules.TrackUID = Convert.ToString(TrackID);
                GroupSchedule.Add(Schedules);
            }
            return (GroupSchedule);
        }

you have to declare the function as an array/string. Something like this would be:

public string[] functionName()
{
}

enjoy :)

The suggested implementation did not work. I have also tried some other method to read the list and they don't work either.

have you tried to see if the array contains information?

yes the array have the information. I've also test for the size of the array.

The correct implementation to the above question is as follow

public struct DeviceSchedules
        {
            public string StartDate;
            public string EndDate;
            public string Interval;
            public string DeviceTimeZone;
            public string GLRID;
            public string TrackUID;
        }

        [WebMethod()]
        public DeviceSchedules[] theSchedules()
        {
            ArrayList ZoneArray = new ArrayList();
            ZoneArray.Add("PST");
            ZoneArray.Add("MST");
            ZoneArray.Add("CST");
            ZoneArray.Add("EST");

            DeviceSchedules Schedules = new DeviceSchedules();
            ArrayList GroupSchedule = new ArrayList();

            Int16 Zone = 1;
            DateTime Begining = DateTime.Now.AddHours(Zone);
            DateTime End = DateTime.Now.AddHours(Zone);
            //int Zone = 3;
            Int32 TrackID = 9999;
            Int32 ScheduleID = 400;
            int Frequency = 40;
            for (int index = 0; index <= 5; index++)
            {
                Schedules.StartDate = Convert.ToString(Begining);
                Schedules.EndDate = Convert.ToString(End);
                Schedules.Interval = Convert.ToString(Frequency);
                Schedules.DeviceTimeZone = Convert.ToString(ZoneArray[Zone]);
                Schedules.GLRID = Convert.ToString(ScheduleID);
                Schedules.TrackUID = Convert.ToString(TrackID);
                GroupSchedule.Add(Schedules);
            }
            return ((DeviceSchedules[])GroupSchedule.ToArray(typeOf(DeviceSchedule)));
        }

This is kind of "?!@#$%%^&*(@"...

yeah.. that's odd. but if it works.. however, i am surprised that my solution didn't work for you! I mean, you declared at the start, instead of formatting at the end. Made sense to me.

I am still trying to understand why the latter works. I agree with your suggestion and it should have worked; But I guessed there is a special convention about ArrayList we must understand. Information like that doesn't exist in books. I had to dig through the MSDN to get the aforementioned implementation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.