473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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 6235
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

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

Similar topics

3
by: Chmouel Boudjnah | last post by:
Hey, It's look simple for most of the people here but i can't figure out to that. If i have : def test(): print "foo" and:
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
5
by: Matt Clepper | last post by:
Any way to do this? I need to call functions based on a variable. Do I actually have to make a case statement and call each funciton explicitly, or is there any way to call a function where the...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
7
by: Robert Bravery | last post by:
HI all, If I have a foreach loop, is there a way to tell what number of itteration I am on during each loop Thanks Robert
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
3
AHayes
by: AHayes | last post by:
_Background I'm attempting to build a C# Windows application for work where I need to dynamically create and remove menu items (buttons). I've figured out how to dynamically create and (I think)...
35
by: erik gartz | last post by:
Hi. I'd like to be able to write a loop such as: for i in range(10): pass but without the i variable. The reason for this is I'm using pylint and it complains about the unused variable i. I can...
8
by: Bill Butler | last post by:
"raylopez99" <raylopez99@yahoo.comwrote in message news:bd59f62a-5b54-49e8-9872-ed9aef676049@t54g2000hsg.googlegroups.com... <snip> I don't think "right" is the correct word. There are many...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.