C# if textbox is empty, skip or add to list
I have been doing a lot of different research on this and from what I have
found out, tryparse and parse does not exist for strings. So I am looking
for an alternative for this.
I have 7 different textboxes and when the button is clicked I need it to
check the textboxes (the variable is a string) and if the textbox is
empty, I want it to completley skip it. If there is a string in the
textbox, I want it to add it to a list. It doesn't have to be a list, I
would prefer a list but I am not going to be picky! Pretty much the best
possible way. I have 7 other checkboxes that are float values, and this is
what I have with that:
List<float> ListGrade = new List<float>();
float LG;
if (float.TryParse(txtGrade1.Text, out LG))
ListGrade.Add(LG);
if (float.TryParse(txtGrade2.Text, out LG))
ListGrade.Add(LG);
if (float.TryParse(txtGrade3.Text, out LG))
ListGrade.Add(LG);
if (float.TryParse(txtGrade4.Text, out LG))
ListGrade.Add(LG);
if (float.TryParse(txtGrade5.Text, out LG))
ListGrade.Add(LG);
if (float.TryParse(txtGrade6.Text, out LG))
ListGrade.Add(LG);
if (float.TryParse(txtGrade7.Text, out LG))
ListGrade.Add(LG);
That works perfect, but I can not figure it out for a string. I thought
about using the nullorwhitespace, but I am not sure how to skip the
textbox completely. I have never used a list before and this is the first
time, and I am not completely understanding the out keyword. Every time I
do a search, nothing comes up besides saying if (textbox1.Text == "") but
that isn't showing how to skip the textbox.
Also, using the above code, and another list that looks identical to that,
except the textboxes are identical and the name is different, I get an
error saying I am using an undeclared variable when using this code at the
end of the above code:
if (ListGrades.Count > 0 && ListCredits.Count > 0)
AverageGrade = ListGrades.Sum() / ListCredits.Sum();
Do I have to add an else statement on to that? When I do it works, but I
am not sure what I could add so the end result would not get messed up.
If you guys need any more information, please let me know. I am trying to
learn and do research, but I am stumped!
Thanks guys!
No comments:
Post a Comment