Reputation: 11
I was wanted to ask a question cause I can't really find what I'm looking for online. I want to see/check if a student IdNum
already exist for example.
I don't know the right term that I'm looking for to google it, and the book I have isn't really that useful as to what to do when needing to do this sort of check.
Here is the code what i have been tried so far :
static void Main(string[] args)
{
Class1[] c1 = new Class1[10]
for (int i = 0; i < c1.Length; i++)
{
Console.WriteLine("enter Student ID");
string text = Console.ReadLine();
int value;
while (!Int32.TryParse(text, out value))
{
Console.WriteLine("ID Was not a Valid ID Number. Try Again");
text = Console.ReadLine();
}
// maybe here say if IdNum exist or not
{
// Try a different number
}
}
}
Class Class1
{
public int IdNum { get; set; }
public int SomethingElse { get; set; }
// and so on
}
Thanks
Upvotes: 0
Views: 76
Reputation: 2108
IEnumerable<Class1> c1 = GetStudents();
string text = Console.ReadLine();
int value;
while (!Int32.TryParse(text, out value))
{
Console.WriteLine("ID Was not a Valid ID Number. Try Again");
text = Console.ReadLine();
}
bool exist = c1.Any(s = > s.IdNum == value);
If you don't want to use linq, you can just rewrite the last line with:
bool exist = false;
foreach (var s in c1)
{
if (s.IdNum == value)
{
exist = true;
break;
}
}
Upvotes: 1