2012-07-14 36 views
4

有沒有簡單的方法來使用排序的並行數組? 我有2個數組:並行數組C#

public class ApiArray 
    { 
     //.. Arrays were filled (E.g. Id:{1,2,7,8,10}, Names:{name1, name2, name3, name4, name5} 
     public Array Id { get; set; } 
     public Array Names { get; set; } 
    } 

    //.. 
    foreach (var N in ApiArray.Names) 
    { 
     listBox1.Items.Add(N); 
    } 

所選名稱陣列具有被鏈接到的Id陣列中的相同的位置。 Id數組不包含後續數字。

當名稱被選中時,必須使用Id。

感謝您的建議。

+0

@nhahtdh - .NET 4.0引入了['Tuple'(http://msdn.microsoft.com/en-us/library/system.tuple.aspx)班這一點,但他們患可讀性。 – Oded 2012-07-14 13:41:08

+0

@Oded:我不用C#編寫代碼,但它似乎有一些有趣的功能。 – nhahtdh 2012-07-14 13:42:56

+0

@nhahtdh - C#(和.NET一般)確實有一些非常好的功能。 – Oded 2012-07-14 13:43:28

回答

0

你必須要

Class User 
{ 
    String name;int id; 
    User(name,id){this.id=id; this.name=name;} 
} 

現在,你必須保持單一陣列中的每個數組索引包含用戶對象的櫃面,如果你這個排序整個數組對象將改變它們的索引位置。由於

3

如果IdName屬於同一個對象,你應該組他們在一個類:

class IdName 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

public IdName myArray[]; 
+0

對,現在是列表框... – 2012-07-14 13:45:15

1

是否有一個原因,爲什麼你不能打破IdName到它自己的階級?

public class MyObject 
{ 
    public string ID; 
    public string Name; 
} 

public class ApiArray 
{ 
    public List<MyObject> MyObjects; 
}