2012-05-04 6 views
0

C#で最初のUIプログラムを構築しているので、いくつかの問題に直面しています。持ってUI C#sub ArrayList

public static ArrayList CountryList = new ArrayList(); 

Country c1 = new Country(string name); // Creating Country object 

CountryList.Add(c1); // Store the Object c1 in ArrayList 

Countryごととして、独自のCitiesので、私は作成できません:
、私はArrayList内の各新しいCountryオブジェクトは、次のようCountryListを前に作成され、名前付きセット私は2つのクラス(CountryCity)を持っていると仮定すると、すべてのCityオブジェクトをまとめて保存するArrayList
次に、ArrayListにSub-ArrayListのようにしたいと思います。

+3

あなたの '新しい国(文字列名)を持っていると思います'違法です。そして、 'Arraylist'を使わないでください。代わりに' List 'を使ってください。 –

答えて

3

これはいかがですか?

public class City 
{ 
    public string Name {get; set;} 
} 

public class Country 
{ 
    public string Name {get; set;} 

    public List<City> Cities {get; set;} 
} 
1

国を文字列にしないでください。オブジェクトにしてください。

public class Country 
{ 
    public string Name {get; set;} 
    public IList<string> Cities {get; set;} 
} 

次にあなたが

Country country = new Country{Name = "England"}; 
country.Cities.Add("London"); 
CountryList.Add(country); 
+0

申し訳ありませんが、 "England"(国オブジェクト)を使用して "London"(都市オブジェクト)を呼び出す方法はありますか?私は新しい都市がそれぞれオブジェクトだと思います! – wisdom

+0

はい、Cityオブジェクトが必要な場合は、各都市をCountry.Cities.FirstOrDefault(x => x.Name == "London")の行に沿って何かのように呼び出すことができます。 – taylonr