Question

well, i'm almost new to c# and i couldn't figure out how multi-leveled-arrays work in c#.

i've made a treeView with menus in it like:


  • menu_1
  • --child_1.1
  • --child_1.2
  • ----child_1.2.1
  • ----child_1.2.2
  • ----child_1.2.3
  • --child_1.3
  • Menu_2
  • --child_2.1
  • --child_2.2
  • ----child_2.2.1

every single MenuItem should have 6 proppertys / attributes / values like this :

Item = { ID:int , "NAME:String , POSITION:String , ACTIVE:Bool , ACTION:bool , PATH:string }

so :

Menu_1 = { 1, "File", "1", true, false, "" }
child_1.1 = { 2, "Open", "1.1", true, true, "./open.exe" }

... and so on

so far :

i've manually set some String-Arrays ( String[] ) for eath menuItem and filled it with the informations.

String[] Item_1 = {"1", "File", "1", "1", "0", ""};
String[] Item_2 = ...

...

now i want to put all those String-Array inside an ArrayList[] and Sort() them using the "POSITION" value of each Item ( Item_1[2] )

also i want the code to dynamicly create that Array of the Item itself, reading the values from a sql table. those arrays should NOT be just String-Arrays as i did for now, cuz i want the ID to stay a int & the ACTIVE and ACTION value to stay a bool.

the final product should look like this :

MenuItems = ArrayList(
    item_1 = Array(Int, String, String, Bool, Bool, String)  // \
    item_2 = Array(Int, String, String, Bool, Bool, String)  //  \
    item_3 = Array(Int, String, String, Bool, Bool, String)  //  / all sortet by the 3rd value, the position )
    item_4 = Array(Int, String, String, Bool, Bool, String)  // /
    ...
    )
)

thanx all of you who can help me out.

Was it helpful?

Solution

Assuming you're on C# 2.0 or later, I'd use a generic list instead of an ArrayList and a class container rather than just arrays. Assuming you're on .NET 3.5 or later, I'd suggest using LINQ for the sorting as well.

First, make a class container for the type of the menu items:

public class MenuItem
{
    public int ID {get;set;}
    public string Name {get;set;}
    public string Position {get;set;}
    public bool Active {get;set;}
    public bool Action {get;set;}
    public string Path {get;set;}
} 

Then you can store instances of this class in a generic List:

var items = new List<MenuItem>();
items.Add(new MenuItem{ID="1", Name="File", Position="1", Active=true, Action=false, Path=""});

Then, to sort this list by position, you can use LINQ:

var sorted = items.OrderBy(i => i.Position);

OTHER TIPS

You cannot do it with arrays alone. Create a class containing subitems as array or as list, if you want to be flexible

public class MenuItem
{
    public MenuItem()
    {
        SubItems = new List<MenuItem>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
    public bool Active { get; set; }
    public bool Action { get; set; }
    public string Path { get; set; }
    public List<MenuItem> SubItems { get; private set; }
}

You can then add subitems like this

var child_1_1 = new MenuItem{ 2, "Open", "1.1", true, true, "./open.exe" };
Menu_1.SubItems.Add(child_1_1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top