C#数组,字符串,错误

时间:2013-05-15 10:10:58

标签: c# visual-c++

        int i;
        int [,] Prices = new int [2, 7]{{1,2,3,4,5,6,7},{700,600,500,400,300,200,100}};
        string[,] City = new string [2,1]{{"A"},{"B"}}; 
        bool found = false;
        for (i = 0; i <= City.Length -1; i++)
          // for (y = 0; y <= City.Length - 1; y++)


        {
            if (LstDestinationCity.Text == City[i]) <<-- i get error here
          {

我计划做一个程序,如果我选择一个城市我得到第一排如果B市我得到2行

3 个答案:

答案 0 :(得分:3)

我认为这是因为City [i]“不包含任何内容”你应该检查City [i,0]

if (LstDestinationCity.Text == City[i,0])// this should access the first element which is the text you are looking for

答案 1 :(得分:0)

我宁愿这样做

if (LstDestinationCity.Text == City[i,i])
{
    // ...
}

答案 2 :(得分:0)

您的City变量不需要是二维数组。如果将其更改为一维数组,则可以使用一个索引而不是2来访问值。

string[] City = new string [2]{"A","B"};