-4

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>
  .....
Frank Pytel
  • 127
  • 16
  • 4
    Why would expect an error? You are returning a string from the method and assigning it to string. – Piyush Parashar Nov 24 '14 at 15:08
  • I think that perhaps `Frank Pytel` you should read up on the difference between `string && string[]` [string](http://msdn.microsoft.com/en-us/library/362314fe.aspx) && [string`[]`](http://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx) – MethodMan Nov 24 '14 at 15:13

2 Answers2

6

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;
    }
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
  • 2
    Also for anyone else that comes along change the `for()` loop to read `(int i = 0; i < nodeStart.Count; i++)` The <= was looping and checking for = which was giving me a null reference because I was incrementing higher than the actual `XmlNodeList nodeStart` length. – Frank Pytel Nov 24 '14 at 16:03
2

You are returning only one string, not an array of it. Change your function like this:

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

    fileExistsWeek(xmldoc);

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

    string[] starttxtWeek = new string[nodeStart.Count];

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

    return starttxtWeek;
}

Also you have to set array capacity to nodeStart.Count. You won't need count2 variable.

In addition, I changed the contition in for loop to i < nodeStart.Count

Paweł Reszka
  • 1,557
  • 4
  • 20
  • 41
  • Pawel, you still missed the point that his code is wrong in terms of the `new string[count2];` logic... – John Bustos Nov 24 '14 at 15:10
  • 1
    @JohnBustos But that's the one that gets it to throw errors. I fought with this last week as well. I have to define `elementStartWeek()` as `string[]` to get the intellisense to follow through. At least now I can keep debugging. :-D – Frank Pytel Nov 24 '14 at 15:12
  • 1
    @FrankPytel fix the dimension of starttxtWeek , string[] starttxtWeek=new string[nodeStart.Count]; – Monah Nov 24 '14 at 15:13