Search code examples
c#xmlwinformsclass-structure

Why is string[] recognized as just a string


I'm trying to keep my XML class separate from my Math class. I'm reading an xml file looking for what time the person started and putting that in a string[] (I think??)

    public static string elementStartWeek()
    {
        XmlDocument xmldoc = new XmlDocument();

        fileExistsWeek(xmldoc);

        XmlNodeList nodeStart = xmldoc.GetElementsByTagName("Start");

        int count2 = 0;
        string[] starttxtWeek = new string[count2];

            for (int i = 0; i <= nodeStart.Count; i++)
            {
                starttxtWeek[count2] = nodeStart[i].InnerText;
                count2++;
            }

        return starttxtWeek[count2];
    }

I want to bring the array into my Math class and convert the time to a decimal value for calculation. My Math class seems to be recognizing this as a string instead of an array.

    public static void startHour()
    {
        string weekStart = WidgetLogic.elementStartWeek();

        string startTime = "";

        if (1 == 1)
        {
            startTime = weekStart;
            MessageBox.Show(weekStart);
        }
    }

I would have expected weekStart in Math.cs to throw an error. Why is this not throwing an error?

I'm making the call to startHour() on the UI dialog DetailerReport

    public DetailerReports()
    {
        InitializeComponent();

        Math.startHour();
    }

EDIT1 this is the XML structure

<?xml version="1.0" encoding="utf-8"?>
<Form1>
  <Name Key="11/19/2014 11:26:13 AM">
    <Date>11/19/2014</Date>
    <JobNum></JobNum>
    <RevNum></RevNum>
    <Task></Task>
    <Start>11:26 AM</Start>
    <End>11:26 AM</End>
    <TotalTime>55870781</TotalTime>
  </Name>
  .....

Solution

  • Your method is returning just a string not an array. That's the first problem, the second is that you're initializing the array with 0.

    public static string[] elementStartWeek()
        {
            XmlDocument xmldoc = new XmlDocument();
    
            fileExistsWeek(xmldoc);
    
            XmlNodeList nodeStart = xmldoc.GetElementsByTagName("Start");
    
            int count2 = 0;
            string[] starttxtWeek = new string[nodeStart.Count];
    
                for (int i = 0; i < nodeStart.Count; i++)
                {
                    starttxtWeek[i] = nodeStart[i].InnerText;
                    count2++;
                }
    
            return starttxtWeek;
        }