Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can only display array by iterating through a for loop

Tags:

c#

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;
}
like image 954
Alex Gatti Avatar asked Jan 21 '12 02:01

Alex Gatti


People also ask

Can an array be used in a for loop?

Each time the for loop runs, it has a different value – and this is the case with arrays. A for loop examines and iterates over every element the array contains in a fast, effective, and more controllable way. This is much more effective than printing each value individually: console.

Can you iterate array by using forEach loop How?

You can iterate over an array using for loop or forEach loop. Using the for loop − Instead on printing element by element, you can iterate the index using for loop starting from 0 to length of the array (ArrayName. length) and access elements at each index.

Is forEach loop only for array?

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Why do we use for loops with arrays?

- [Instructor] When you have values in an array, it is common to want to perform actions on each one of the items. You can use a for loop to iterate through all the items in the array and access each element individually.


2 Answers

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;
        }
    }
}
like image 176
Maggie Avatar answered Sep 27 '22 16:09

Maggie


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

like image 40
Orkun Ozen Avatar answered Sep 27 '22 15:09

Orkun Ozen