✅ Methods
Hi, i need help building a method to see if a textbox contains digits or letters. The assignment is to put in the year you are born and then give back how old you are. The tricky part for me is to build a method that will catch if you put letters in your textbox and not break down. Im not allowed to use any built in methods like tryParse och try and catch. ```cs
private void Button_Click(object sender, RoutedEventArgs e)
{
string yearBorn = TxtYearBorn.Text;
bool istrueornot = TryifStringisNumber(yearBorn);
if (istrueornot == false) { MessageBox.Show("Du får bara skriva in siffror"); } else if (istrueornot == true) { int integerYearBorn = int.Parse(yearBorn); int yearBornResult = Age(integerYearBorn); MessageBox.Show($"You are {yearBornResult} years old"); } } public bool TryifStringisNumber(string yearborn) { for (int i = 0; i < yearborn.Length; i++) { if (i <= 0 ) { return false; } else {
return true; } } return false; } private int Age(int yearOfBirth) { if (yearOfBirth >= 0) { return -1; } else { int currentyear = DateTime.Now.Year; return currentyear - yearOfBirth; } }
} }
if (istrueornot == false) { MessageBox.Show("Du får bara skriva in siffror"); } else if (istrueornot == true) { int integerYearBorn = int.Parse(yearBorn); int yearBornResult = Age(integerYearBorn); MessageBox.Show($"You are {yearBornResult} years old"); } } public bool TryifStringisNumber(string yearborn) { for (int i = 0; i < yearborn.Length; i++) { if (i <= 0 ) { return false; } else {
return true; } } return false; } private int Age(int yearOfBirth) { if (yearOfBirth >= 0) { return -1; } else { int currentyear = DateTime.Now.Year; return currentyear - yearOfBirth; } }
} }
25 Replies
Loop over all of the characters and return
false
if you encounter a non-number
char.IsNumber()
will be useful
Or check for ASCII value rangeslike how? isnt char.IsNumber a built in method?
If you can't even use that, then ASCII ranges it is
Angius
REPL Result: Success
Result: <>f__AnonymousType0#1<int, int>
Compile: 320.462ms | Execution: 85.448ms | React with ❌ to remove this embed.
Check if the char's ASCII value is between 48 and 57
Or just if it's between
'0'
and '9'
Angius
REPL Result: Success
Result: bool
Compile: 336.701ms | Execution: 30.589ms | React with ❌ to remove this embed.
Angius
REPL Result: Success
Result: bool
Compile: 326.685ms | Execution: 47.967ms | React with ❌ to remove this embed.
ch >= '0' && ch <= '9'
if you can't use pattern matching
And ecven if you can, it should ve >=
and <=
, not >
and <
My mistake thereim really confused here since i have only programmed i C# for a month...
Every
string
is a collection of char
sAngius
REPL Result: Success
Console Output
Compile: 579.490ms | Execution: 57.705ms | React with ❌ to remove this embed.
And every
char
has an int
associated with it, according to the ASCII tableAngius
REPL Result: Success
Result: int
Compile: 279.950ms | Execution: 21.356ms | React with ❌ to remove this embed.
Combining those two, every char in a string has an associated number
Angius
REPL Result: Success
Console Output
Compile: 578.712ms | Execution: 46.851ms | React with ❌ to remove this embed.
Char
'0'
has the value of 48
Char '9'
has the value of 57yes im with you so far
If a given char has a value that's higher or equal to 48 and lower or equal to 57, it means that char represents a number
true, but how do i change the whole string to ASCCI letters?
The string already is a list of characters
And each character already is it's ASCII value
See here
I just looped over the string
"hello"
and got both letters and their values out of it, inside the loopoh okey, i think the console write through me of because we only work in WPF and not console!
Oof, that means you're kinda working ass-backwards. WPF is complex and hard, it should absolutely not be how you teach C#
yeah the course I'm reading is ridiculous, so many people have actually dropped out because of it! We even doing OOP when this is for many the first programming course.
That last part isn't too weird tbh, but starting with WPF is.
Anyways, did ZZZ's advice get you unstuck?
yes it did! just hoping the teacher excepts it!
oh okey, because when i read python we didnt do OOP so was just a chock for me personally maybe