Question

Background information: I'm iterating through a site collection, which stores all the sites I'm looking for in the correct hierarchical order. When I try to display this information in a nested format, in addition to multiple columns on the same row format is where I run into the problem.

I have a for loop that adds items to an ArrayList. I have another for loop that iterates through the "example" ArrayList. I need to break or split this ArrayList each time "-----" occurs. Problem is ArrayList does not support .Split(), so I'm out of ideas. My overall goal is to display the information in the ArrayList in nested dynamic-columns that are based on the number of "-----".

ArrayList example = new ArrayList();
example.Add("Door");
example.Add("A1"); //nested
example.Add("A2"); //nested
example.Add("-----");
example.Add("House");
example.Add("A1"); //nested
example.Add("A2"); //nested
example.Add("-----");
example.Add("Fence");
example.Add("A1"); //nested
example.Add("A2"); //nested
example.Add("-----");

When I iterate through the list a table is built and displayed like the example below:

|Door| A1 | A2 | House | A1 | A2 | Fence | A1 | A2| 

However, I need the data in the table to be displayed like this example below:

|Door| House | Fence| <----This is the desired output that I'm trying to achieve.
|A1  | A1    | A1   | <----This is the desired output that I'm trying to achieve.
|A2  | A2    | A2   | <----This is the desired output that I'm trying to achieve.

Any help would be appreciated.

Was it helpful?

Solution

I would do it like this:

class Thing {
    public string name;
    public string a;     // This may also be a List<string> for dynamic Add/Remove
    public string b;
    // ...

    public Thing(string Name, string A, string B) {
        name = Name; a = A; b = B;
    }
}

Usage:

List<Thing> things = new List<Thing>();
things.Add(new Thing("Fence", "A1", "A2"));
things.Add(new Thing("Door", "A1", "A2"));
// ...

I always use a class to store a bunch of information that belongs together. The best example for this are the derivations of EventArgs, like the PaintEventArgs. All needed information comes with one instance.
This enables you also to implement more features. For example, I amost always override the ToString() method of that class, so I am able to display the objects contents while debugging or simply adding the objects to a ListBox or a ComboBox, because they call ToString() to display.

OTHER TIPS

Would it not make more sense to make a Data Structure that works with the type of data you're looking to store? I don't know if this is a specific restriction of the project or if it's homework, but it seems like using the ArrayList to store Objects that have the needed datamembers would be easier when it comes time to print it out.

This would be better solved using a List of Lists or something similar. For example:

List<List<string>> example = new List<List<string>>();
List<string> door = new List<string>();
door.Add("Door");
door.Add("A1");
door.Add("A2");
example.Add(door);
...so on and so forth...

Then looping through it is just a matter of the following:

foreach (List<string> list in example)
{
  foreach (string s in list)
  {
     //magic
  }
}

You can use Split method from moreLINQ library, but because ArrayList does not implement IEnumerable<T> you'll have to call Cast<T>() first.

var result = source.Cast<string>().Split("-----");

But first of all, I would suggest using List<string> instead of ArrayList at the first place.

You can convert your ArrayList into a list of lists as follows:

var list = new List<List<string>>();
var current = new List<string>();
list.Add(current);

foreach (string element in example)
{
    if (element.Equals("-----"))
    {
        current = new List<string>();
        list.Add(current);
    }
    else
    {
        current.Add(element);
    }
}

if (!current.Any())
{
    list.Remove(current);
}

But, as others have said, it's probably best to avoid ArrayList completely if you can.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top