2015-08-30 22 views
2

所以我試圖使計算的時間,你在一個軌道交通承擔量的應用程序,所以我做站類和正常形態我做包含三個交錯數組行(每一行表示不同的地鐵線路),所以我聲明數組這樣的:station[][] lines = new station[3][];,然後我聲明這樣不能鋸齒狀排列申報對象

lines[0] = new station[34]; 
lines[1] = new station[19]; 
lines[2] = new station[35]; 

行和i中的交錯數組這樣在聲明中的對象內的名稱和值:

lines[0][0].name = "stationname"; 
lines[0][0].number = 1;` 

但是當我跑它給我的程序NullReferenceException, 我知道我沒有聲明對象(我認爲),我不知道如何聲明它們是正確的。 嗯,這裏是一個更完整的代碼示例:

 station[][] lines = new station[3][]; 
     lines[0] = new station[34]; 
     lines[0][0] = new station {name = "حلوان", number = 1 }; 
     lines[0][0].number = 1; 
     lines[0][1].name = "عين حلوان"; 
     lines[0][1].number = 2; 
     lines[0][2].name = "جامعة حلوان"; 
     lines[0][2].number = 3; 
     lines[0][3].name = "وادى حوف"; 
     lines[0][3].number = 4; 
     lines[0][4].name = "حدائق حلوان"; 
     lines[0][4].number = 5; 

連續兩個

  lines[1] = new station[19]; 
     lines[1][0].name = "المنيب"; 
     lines[1][0].number = 36; 
     lines[1][1].name = "ساقية مكى"; 
     lines[1][1].number = 37; 
     lines[1][2].name = "ضواحى الجيزة"; 
     lines[1][2].number = 38; 
     lines[1][3].name = "محطة الجيزة"; 
     lines[1][3].number = 39; 
     lines[1][4].name = "فيصل"; 
     lines[1][4].number = 40; 
     lines[1][5].name = "جامعة القاهرة"; 
     lines[1][5].number = 41; 

行3

  lines[2] = new station[35]; 
     lines[2][0].name = "مطار القاهرة"; 
     lines[2][0].number =54; 
     lines[2][1].name = "السلام"; 
     lines[2][1].number = 55; 
     lines[2][2].name = "عمر بن الخطاب"; 
     lines[2][2].number = 56; 
     lines[2][3].name = "قباء"; 
     lines[2][3].number = 57; 
     lines[2][4].name = "النزهة٢"; 
     lines[2][4].number = 58; 
     lines[2][5].name = "النزهة١"; 
     lines[2][5].number = 59; 
+0

你能後的實際代碼。還顯示錯誤在哪一行。 – deathismyfriend

+0

當你聲明一個參差不齊的數組時,你首先聲明所有的維度爲一個新的數組,但是你必須沿着第一維進行迭代,併爲該維度中的每個數組聲明一個長度爲X的新數組。請記住,鋸齒狀數組是一組數組。所以你「新」數組的數組,然後「新」每個數組。 – WDS

+0

但是,如果lines [0] [0] .name引用具有名爲.Name的屬性的Class或Struct,那麼您還必須初始化該Class或Struct。這通常由構造函數完成,所以。 。 。是的,請張貼更多的代碼,以便我們看到發生了什麼。 – WDS

回答

0

您初始化數組很好,但你沒有初始化它們內部的任何對象。

務必:

lines[0][0] = new Station() { name = "...", number = 1 }; 

PS:我想你的意思是不是Stationstation

+0

station是我製作的一個類,它包含一個名爲name的字符串和一個名爲number的值,它的名字全部用小寫字母表示。 :) – GamerGamesGames

+0

@GamerGamesGames不知道C#允許類名以小寫字母開頭。無論如何,你的課程應該以大寫字母開頭。 –

+0

謝謝,我會記住:D – GamerGamesGames

0

我覺得這是也許有點混亂。我試圖捕捉我認爲你的意思是什麼。你需要一個數組,每行包含一個數組。每個車站都有一個名字和號碼。基於這樣的理解,下面是一些可以工作的代碼,並給出了其餘部分應該如何構造的一個想法。

internal class Station 
    { 
     internal string name; 
     internal int stationNumber; 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Station[][] line = new Station[3][]; // Assuming 3 lines total 

     line[0] = new Station[34]; // This is saying line #0 (stored in element major 0) needs 34 elements, each of which will contain a Station instance 
     line [0][0] = new Station { "حلوان", 1 }; 
     line [0][1] = new Station {"عين حلوان", 2}; 
     line[0][2] = new Station {"جامعة حلوان",3}; 
     line[0][3] = new Station {"وادى حوف",4}; 
     line[0][4] = new Station { "حدائق حلوان", 5 }; 
    } 
+0

我希望這是有道理的。在沒有看到整個程序的情況下知道你需要什麼是非常困難的,但是我希望這能讓你明白你需要做什麼來防止你得到的錯誤。 – WDS

+0

究竟:),爲什麼我使用鋸齒陣是因爲每一行有不同數量的站點,雖然有兩個站點是相同的,但是在兩個不同的線路上,對於那些我只是做EX:'line [1] [6] = helwan //(已經聲明的對象)' – GamerGamesGames

+0

嗯,那很奇怪。我在VS2013中編寫了代碼,但是當我在這裏複製/粘貼時,它移動了引號。可能軟件遇到從右到左的字母表有問題。無論如何,我相信你知道我想發佈的內容,引述圍繞字符串,而不是數字,因爲它在這裏。 – WDS