5

Hello I am trying to make a C# program that downloads files but I am having trouble with the array.

I have it split up the text for downloading and put it into a 2 level jagged array (string[][]).

Now I split up the rows up text by the | char so each line will be formatted like so: {filename}|{filedescription}|{filehttppath}|{previewimagepath}|{length}|{source}

when I use short test text to put it into a text box it displays fine in the text box.

IE: a string like test|test|test|test|test|test

but if I put in a real string that I would actually be using for the program to DL files the only way I get the string to display is to iterate through it with a for or foreach loop. If I try to access the data with the index I get an index missing error. (IE array[0])

So this is the code that gets the array to display:

public Form2(string[][] textList, string path)
{
    InitializeComponent();
    textBox1.Text = textBox1.Text + path + Environment.NewLine;
    WebClient downloader = new WebClient();
    foreach (string[] i in textList)
    {
        for(int j=0;j<i.Length;j++)
        {
            textBox1.Text = textBox1.Text + i[j] + Environment.NewLine + @"\\newline" + Environment.NewLine;
        }
    }
}

And then this is the code that gives an index missing error:

public Form2(string[][] textList, string path)
{
    InitializeComponent();
    textBox1.Text = textBox1.Text + path + Environment.NewLine;
    WebClient downloader = new WebClient();
    foreach (string[] i in textList)
    {
        textBox1.Text = textBox1.Text + i[0] + Environment.NewLine;
        textBox1.Text = textBox1.Text + i[1] + Environment.NewLine;
        textBox1.Text = textBox1.Text + i[2] + Environment.NewLine;
        textBox1.Text = textBox1.Text + i[3] + Environment.NewLine;
        textBox1.Text = textBox1.Text + i[4] + Environment.NewLine;
        textBox1.Text = textBox1.Text + i[5] + Environment.NewLine;
    }
}

Any help is this is apreciated I don't see why I can access they data through a for loop but not directly it just doesn't make any sense to me.

Also, here is the code that generates the array:

public String[][] finalList(string[] FileList)
{
    String[][] FinalArray = new String[FileList.Length][];
    for (int i = 0; i<FinalArray.Length;i++)
    {
        string[] fileStuff = FileList[i].Split(new char[] {'|'});
        FinalArray[i] = fileStuff;
    }
    return FinalArray;
}
17
  • Is the Length of string[] i > 1? Commented Jan 21, 2012 at 2:41
  • I would have to assume that there aren't 6 items in the i array. Commented Jan 21, 2012 at 2:45
  • index missing error ?? is it smth like "index out of range" ?? Commented Jan 21, 2012 at 2:50
  • yes index out of range but I know the index is there because when I go through it with the for loop it displays them no problem but when I try to access the data directly I get the error. When I loop for it displays all 6 values Commented Jan 21, 2012 at 2:57
  • 1
    which line is the error occurring on? Commented Jan 21, 2012 at 3:04

3 Answers 3

4

In your first example you are using the actual length of each inner array to do the concatenation. In your second example you are hard coded to the same length yet you said in the intro it was a jagged array.

Can you show what your input text looks like?

you are not doing the same concatenation in first and second example so the resulting stings are very different.

        first = "\r\n Crazy Video\r\n\\\\newline\r\nThis Video is absolutly crazy!\r\n\\\\newline\r\nhtt://fakeurl.fake/vidfolder/video.flv\r\n\\\\newline\r\nhtt://fakeurl.fake/imgfolder/img.j‌​pg\r\n\\\\newline\r\n300\r\n\\\\newline\r\nhtt://fakeurl.fake \r\n\\\\newline\r\n"

        second = "\r\n Crazy Video\r\nThis Video is absolutly crazy!\r\nhtt://fakeurl.fake/vidfolder/video.flv\r\nhtt://fakeurl.fake/imgfolder/img.j‌​pg\r\n300\r\nhtt://fakeurl.fake \r\n" 


using System;
using NUnit.Framework;

namespace ClassLibrary5
{
    public class Class1
    {
        [Test]
        public void test()
        {
            var temp = new[]
                           {
                               " Crazy Video|This Video is absolutly crazy!|htt://fakeurl.fake/vidfolder/video.flv|htt://fakeurl.fake/imgfolder/img.j‌​pg|300|htt://fakeurl.fake "
                           };
            var final = finalList(temp);
            var first = Form1(final, "path");
            var second = Form2(final, "path");
            Assert.IsTrue(first.CompareTo(second) == 0);
        }

        public string Form1(string[][] textList, string path)
        {
            string textString = path + Environment.NewLine;

            foreach (string[] i in textList)
            {
                for (int j = 0; j < i.Length; j++)
                {
                    textString = textString + i[j] + Environment.NewLine + @"\\newline" + Environment.NewLine;
                }
            }
            return textString;
        }

        public string Form2(string[][] textList, string path)
        {
            string textString = path + Environment.NewLine;

            foreach (string[] i in textList)
            {
                textString = textString + i[0] + Environment.NewLine;
                textString = textString + i[1] + Environment.NewLine;
                textString = textString + i[2] + Environment.NewLine;
                textString = textString + i[3] + Environment.NewLine;
                textString = textString + i[4] + Environment.NewLine;
                textString = textString + i[5] + Environment.NewLine;
            }
            return textString;
        }

        public String[][] finalList(string[] FileList)
        {
            String[][] FinalArray = new String[FileList.Length][];
            for (int i = 0; i < FinalArray.Length; i++)
            {
                string[] fileStuff = FileList[i].Split(new char[] {'|'});
                FinalArray[i] = fileStuff;
            }
            return FinalArray;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

the test input text that works just fine that I used for testing while writing it was strings like test|test|test|test|test|test the final strings is like Crazy Person Video|This video shows someone doing something crazy|fakeurl.fake/videofolders/video.flv|http://fakeurl.fake/…
okay it cut off the final string I try this again:: Crazy Video|This Video is absolutly crazy!|htt://fakeurl.fake/vidfolder/video.flv|htt://fakeurl.fake/imgfolder/img.jpg|300|htt://fakeurl.fake
1

Are you sure each String[] in string[][] textList has 6 elements?

1 Comment

Yes, 100% sure I enter the strings manually in a text box before proccessing them. the string test|test|test|test|test|test works find but when I put in actual values rather than test ones it stop working unless I iterate through a loop instead of accessing them directly.
1

Try to replace:

  for(int j=0;j<i.Length;j++)
     {
         textBox1.Text = textBox1.Text + i[j] + Environment.NewLine + @"\\newline" + Environment.NewLine;
     }

with:

 for(int j=0;j<6;j++)
     {
         textBox1.Text = textBox1.Text + i[j] + Environment.NewLine + @"\\newline" + Environment.NewLine;
     }

And see if you get the same result. Your middle one has different logic than your first one. To troubleshoot, first make the logic the same, and then continue troubleshooting from there.

1 Comment

Okay I now get the same out of index error after adding in J<6 I'm not sure why I'm still new to C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.