Can I call this variable of foreach in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meomap0z1
    New Member
    • Jan 2020
    • 9

    Can I call this variable of foreach in C#?

    I have this code
    Code:
    class QuanLySinhVien
    {
    public static List<SinhVien> DanhSachSinhVien = new List<SinhVien>(50);
            public List<SinhVien> GetDanhSachSinhVien
            {
                get => DanhSachSinhVien;
            }
    public void FindStudent()
            {
                Console.Write("Enter ID's student that you need to find: ");
                string msSV = Console.ReadLine().ToUpper();
                SinhVien sv = CheckExist(msSV);
                if (sv == null)
                {
                    Console.Write("Can not find this student with {0}'s ID\n", msSV);
                }
                else
                {
                    foreach (SinhVien s in QuanLySinhVien.DanhSachSinhVien)
                    {
                        Console.WriteLine("ID: " + sv.MMaSV);
                        Console.WriteLine("Name: " + sv.HoTen1);
                        Console.WriteLine("Birthdate: ");
                        sv.NgaySinh1.ShowBirthdate();
                        Console.WriteLine("Address: " + sv.DiaChi1);
                        Console.WriteLine("Number: " + sv.DienThoai1);
                        Console.WriteLine("------*****------");
                        break;
                    }
                    
                }
            } 
    }
    My question that I wanna use this " SinhVien sv " in forEach
    Code:
     foreach (SinhVien s in QuanLySinhVien.DanhSachSinhVien) }
    instead of using SinhVien s.
    I do not use "SinhVien s" for printing the student on the list that I need to find.
    Thanks for reading and helping me, have a nice day!!
  • dbrewerton
    New Member
    • Nov 2009
    • 115

    #2
    If you use sv instead of s, exactly what happens? Does it error out?

    Comment

    • meomap0z1
      New Member
      • Jan 2020
      • 9

      #3
      Actually I do not have any problem for using sv instead of s

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Do you need the foreach at all if you just want to print the details for a single, known/found object?

        Comment

        • vikramjk
          New Member
          • Jul 2020
          • 2

          #5
          try using this

          Code:
              foreach (SinhVien s in QuanLySinhVien.DanhSachSinhVien)
                          {
                              Console.WriteLine("ID: " + s.MMaSV);
                              Console.WriteLine("Name: " + s.HoTen1);
                              Console.WriteLine("Birthdate: ");
                              s.NgaySinh1.ShowBirthdate();
                              Console.WriteLine("Address: " + s.DiaChi1);
                              Console.WriteLine("Number: " + s.DienThoai1);
                              Console.WriteLine("------*****------");
                              break;
                          }
          This should not create any error, you are just changing variable name as I understand.

          if "SinhVien " has all the properties which you are looping inside foreach, then it should be fine.

          Check, Variable "sv" is coming from this line of code.
          Code:
           SinhVien sv = CheckExist(msSV);

          Comment

          Working...