就是通过打开一个csv文件,在lisbox里显示数据(已完成)、在picturebox里作图(已完成)。但是问题是现在题目要求:create a method called GetSeatColor which is passed the type of person("Adult" or "child") and returns back the correct seat color.实际上我现在的代码已经完整地把这道题做了出来,但是并没有采用并达到上面这个要求。请各位帮忙看看,如何完成上述要求。
private void fileToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Graphics paper = pictureBoxCinema.CreateGraphics();
            DrawCinema(paper);
            openFileDialog1.Filter = "CSV Files|*.CSV|ALL Files|*.*";
            openFileDialog1.ShowDialog();
            string[] csvArray;
            string line;
            //use try/catch stucture
            try
            {
                StreamReader Readfile = File.OpenText(openFileDialog1.FileName);
                while (Readfile.Peek() != -1)
                {
                    int seat, row;
                    string name;
                    string type;
                    line = Readfile.ReadLine();
                    csvArray = line.Split(',');
                    seat = Convert.ToInt32(csvArray[0]);
                    row = Convert.ToInt32(csvArray[1]);
                    name = Convert.ToString(csvArray[2]);
                    type = Convert.ToString(csvArray[3]);
                    
                    //show the content in the listBox
                    listBoxBookings.Items.Add(seat + "   " + row + "   " + name.PadRight(15) + "   " + type);
                    //show the color of adults' and children's seats
                    if (type == "Adult")
                    {
                        DrawASeat(paper, (row - 1) * SeatSize, (seat - 1) * SeatSize, Color.Gold);                    }
                    if (type == "Child")
                    {
                        DrawASeat(paper, (row - 1) * SeatSize, (seat - 1) * SeatSize, Color.Purple);                    }
   
                }                Readfile.Close();
                }
            
            
            catch (Exception)
            {
            }
        }

解决方案 »

  1.   

    // ------------ GetSeatColor方法: private Color GetSeatColor(string typeOfPerson)
    {
    if (typeOfPerson == "Adult")
    return Color.Gold;
    else if (typeOfPerson == "Child")
    return Color.Purple;
    else
    throw new Exception("invalid type of person");
    }// ------------ 把你的程序中的下面这段代码改掉:
      //show the color of adults' and children's seats
      if (type == "Adult")
      {
      DrawASeat(paper, (row - 1) * SeatSize, (seat - 1) * SeatSize, Color.Gold);
      }
      if (type == "Child")
      {
      DrawASeat(paper, (row - 1) * SeatSize, (seat - 1) * SeatSize, Color.Purple);
      }// ------- 改成:  //show the color of adults' and children's seats
      DrawASeat(paper, (row - 1) * SeatSize, (seat - 1) * SeatSize, GetSeatColor(type));
      

  2.   

    楼上,你回复得很好。非常感谢你,最后还有个问题,为什么在我修改原code时,括号中的GetSeatColor下出现红线,并提示“the name does not exist in the current context”?请问是否要设置全局变量或是其它问题?