I am working on a web service. I have a form with a button. When I click the button it calls my service class and I populate a list from an xml document with stock information. Then from the form class it calls another method inside the service class to populate a second list with a range of dates and double values and displaying it. It displays the list fine. That list I am adding to a series so I can chart. But before that I am calling a method that uses the second list, but it appears to be empty. Help?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Diagnostics;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : IService
{
    List<DailyPrices> dailyList = new List<DailyPrices>();
    List<GraphData> newList = new List<GraphData>();
    //The caller passes two dates (first, last) to this method, and receives an array of objects
    public GraphData[] GetDateRange(DateTime firstDate, DateTime lastDate)
    {
        dailyList.Reverse();   

        foreach (DailyPrices p in dailyList) 
        {
            if (p.date.CompareTo(firstDate) >= 0 && p.date.CompareTo(lastDate) <= 0)
            {
                newList.Add(new GraphData(p.date, p.adj_close));

            }
        }


        foreach (GraphData d in newList)
        {

            Debug.WriteLine("From newList: " + d.Date + " " + d.Value);


        }


        GraphData [] graph = newList.ToArray();
        return graph;
    }

    public double GetMin()
    {

        foreach (GraphData d in newList)
        {

            Debug.WriteLine("From newList2: " + d.Date + " " + d.Value);


        }

        double currentMin = 0;
        foreach (GraphData d in newList)
        {




            if (d.Value < currentMin)
            {
                Debug.WriteLine(d.Value);
                currentMin = d.Value;
            }


        }

        return currentMin - 2.5;
    }

    public double GetMax()
    {
        double currentMax = 0;
        foreach (GraphData d in newList)
        {

            if (d.Value < currentMax)
            {
                currentMax = d.Value;
            }


        }
        return currentMax + 2.5;
    }

    public GraphData[] CalcMovingAverages(DateTime firstDate, DateTime lastDate, int numDays)
    {
        return null;
    }

    public String GetData(int v1, int v2) {
        //Debug.WriteLine("Debug : Called GetData");
        return "This is from get Data";
    }

    public Service() {
        string path = "http://cis.fiu.edu/~irvinek/cop4814/data/ulti.xml";
        bool fileFound = Read(path);

        foreach (DailyPrices P in dailyList)
        {
            //Debug.WriteLine(P);
        }
    }


    /*
     Main is not running
     */


    public bool Read(string path)
    {
        try
        {
            System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(path);
            reader.Read();
            while (reader.Read())
            {
                reader.MoveToContent();
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    if (reader.Name == "Row")
                    {
                        DailyPrices S = readDailyPrices(reader);
                        dailyList.Add(S);
                    }
                }
            }
            return true;
        }
        catch (System.IO.FileNotFoundException)
        {
            return false;
        }
    }

     string nextValue(System.Xml.XmlTextReader reader)
    {
        while (reader.Read())
        {
            if (reader.NodeType == System.Xml.XmlNodeType.Text)
                return reader.Value;
        }
        return null;
    }

     DailyPrices readDailyPrices(System.Xml.XmlTextReader reader)
    {
        // Excel dates are encoded as integers, where 1/1/1900 = 1. There is a bug
        // in their date calculations because they consider 1900 to be a leap year. This
        // bug was never corrected because it first appeared in Lotus 1-2-3 and Microsoft
        // wanted the two products to be compatible.

        int offset = 2;
        int excelDays = Convert.ToInt32(nextValue(reader)) - offset;
        DateTime start = new DateTime(1900, 1, 1);
        TimeSpan ts = new TimeSpan(excelDays, 0, 0, 0);
        DateTime D = start.Add(ts);

        double open = Convert.ToDouble(nextValue(reader));
        double high = Convert.ToDouble(nextValue(reader));
        double low = Convert.ToDouble(nextValue(reader));
        double close = Convert.ToDouble(nextValue(reader));
        double volume = Convert.ToDouble(nextValue(reader));
        double adj_close = Convert.ToDouble(nextValue(reader));

        DailyPrices S = new DailyPrices(D, open, high, low, close, volume, adj_close);
        return S;
    }
}
class DailyPrices
{

    public DateTime date { get; set; }

    public double open { get; set; }

    public double high { get; set; }

    public double low { get; set; }

    public double close { get; set; }

    public double volume { get; set; }

    public double adj_close { get; set; }

    public DailyPrices(DateTime pDate, double pOpen, double pHigh, double pLow, double pClose, double pVolume, double pAdj_Close)
    {
        date = pDate;
        open = pOpen;
        high = pHigh;
        low = pLow;
        close = pClose;
        volume = pVolume;
        adj_close = pAdj_Close;
    }



    public override String ToString()
    {
        return date.ToString("d") + ", " + open + ", " + high + ", " + low + ", " + close + ", v=" + volume + ", " + adj_close;
        //return "This is some Data -" + high;
    }

}





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace StockServiceConsumer
{
    public partial class GuiBoard : Form
    {
        public GuiBoard()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            localhost.ServiceClient client = new localhost.ServiceClient();
            localhost.GraphData[] graph = client.GetDateRange(DateTime.Parse("1/1/2010"), DateTime.Parse("10/10/2010"));

            Console.WriteLine(client.GetMax() + " " + client.GetMin());   

                // A Series object  represents a single line on the graph. A graph may contain 
                // multiple Series objects. 

                Series S = new Series("Series 1");
                S.Color = Color.Navy;
                S.XValueMember = "Date";
                S.YValueMembers = "Value";
                S.BorderWidth = 2;                              // line thickness
                S.BorderDashStyle = ChartDashStyle.Dash;        // line style

                // Experiment with different types of charts
                S.ChartType = SeriesChartType.Line;
                //S.ChartType = SeriesChartType.Bar;
                S.XValueType = ChartValueType.Date;
                S.YValueType = ChartValueType.Double;


                MyChart.Series.Clear();

                // This is where you add the Series object to the Series collection for the graph.
                MyChart.ChartAreas["ChartArea"].AxisY.Maximum = client.GetMax();
                MyChart.ChartAreas["ChartArea"].AxisY.Minimum = client.GetMin();
                MyChart.Series.Add(S);

                // A Series object contains DataPoint objects. The AddXY method adds a new DataPoint
                // to the Points collection (a DataPointCollection object).
                foreach (localhost.GraphData data in graph)
                {
                    S.Points.AddXY(data.Date, data.Value);
                }

            //MyChart.Legends.Clear();
        }

    }
}

The one thing that jumps out at me, is that your class is called Service but the only place you try to call GetDateRange is with an object declared as type ServiceClient. If you do indeed have a class called ServiceClient you'll be seeing the output from that class not necessarily Service

It is a web service so when you make a new ServiceClient it is referenced to the Service class.

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.