469,616 Members | 1,806 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,616 developers. It's quick & easy.

Can I call this variable of foreach in C#?

meomap0z1
I have this code
Expand|Select|Wrap|Line Numbers
  1. class QuanLySinhVien
  2. {
  3. public static List<SinhVien> DanhSachSinhVien = new List<SinhVien>(50);
  4.         public List<SinhVien> GetDanhSachSinhVien
  5.         {
  6.             get => DanhSachSinhVien;
  7.         }
  8. public void FindStudent()
  9.         {
  10.             Console.Write("Enter ID's student that you need to find: ");
  11.             string msSV = Console.ReadLine().ToUpper();
  12.             SinhVien sv = CheckExist(msSV);
  13.             if (sv == null)
  14.             {
  15.                 Console.Write("Can not find this student with {0}'s ID\n", msSV);
  16.             }
  17.             else
  18.             {
  19.                 foreach (SinhVien s in QuanLySinhVien.DanhSachSinhVien)
  20.                 {
  21.                     Console.WriteLine("ID: " + sv.MMaSV);
  22.                     Console.WriteLine("Name: " + sv.HoTen1);
  23.                     Console.WriteLine("Birthdate: ");
  24.                     sv.NgaySinh1.ShowBirthdate();
  25.                     Console.WriteLine("Address: " + sv.DiaChi1);
  26.                     Console.WriteLine("Number: " + sv.DienThoai1);
  27.                     Console.WriteLine("------*****------");
  28.                     break;
  29.                 }
  30.  
  31.             }
  32.         } 
  33. }   
  34.  
My question that I wanna use this " SinhVien sv " in forEach
Expand|Select|Wrap|Line Numbers
  1.  foreach (SinhVien s in QuanLySinhVien.DanhSachSinhVien) }
  2.  
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!!
Jan 30 '20 #1
4 4885
dbrewerton
115 100+
If you use sv instead of s, exactly what happens? Does it error out?
Feb 5 '20 #2
Actually I do not have any problem for using sv instead of s
Feb 6 '20 #3
Frinavale
9,735 Expert Mod 8TB
Do you need the foreach at all if you just want to print the details for a single, known/found object?
Nov 3 '20 #4
vikramjk
2 2Bits
try using this

Expand|Select|Wrap|Line Numbers
  1.     foreach (SinhVien s in QuanLySinhVien.DanhSachSinhVien)
  2.                 {
  3.                     Console.WriteLine("ID: " + s.MMaSV);
  4.                     Console.WriteLine("Name: " + s.HoTen1);
  5.                     Console.WriteLine("Birthdate: ");
  6.                     s.NgaySinh1.ShowBirthdate();
  7.                     Console.WriteLine("Address: " + s.DiaChi1);
  8.                     Console.WriteLine("Number: " + s.DienThoai1);
  9.                     Console.WriteLine("------*****------");
  10.                     break;
  11.                 }
  12.  
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.
Expand|Select|Wrap|Line Numbers
  1.  SinhVien sv = CheckExist(msSV);
Dec 22 '20 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Chmouel Boudjnah | last post: by
134 posts views Thread by James A. Donald | last post: by
32 posts views Thread by Joe Rattz | last post: by
27 posts views Thread by Tripper | last post: by
7 posts views Thread by Robert Bravery | last post: by
35 posts views Thread by erik gartz | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.