C
C#7mo ago
olleeee

✅ 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; } }
} }
25 Replies
Angius
Angius7mo ago
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 ranges
olleeee
olleeee7mo ago
like how? isnt char.IsNumber a built in method?
Angius
Angius7mo ago
If you can't even use that, then ASCII ranges it is
MODiX
MODiX7mo ago
Angius
REPL Result: Success
new { zero = (int)'0', nine = (int)'9' }
new { zero = (int)'0', nine = (int)'9' }
Result: <>f__AnonymousType0#1<int, int>
{
"zero": 48,
"nine": 57
}
{
"zero": 48,
"nine": 57
}
Compile: 320.462ms | Execution: 85.448ms | React with ❌ to remove this embed.
Angius
Angius7mo ago
Check if the char's ASCII value is between 48 and 57 Or just if it's between '0' and '9'
MODiX
MODiX7mo ago
Angius
REPL Result: Success
'6' is > '0' and < '9'
'6' is > '0' and < '9'
Result: bool
True
True
Compile: 336.701ms | Execution: 30.589ms | React with ❌ to remove this embed.
MODiX
MODiX7mo ago
Angius
REPL Result: Success
'g' is > '0' and < '9'
'g' is > '0' and < '9'
Result: bool
False
False
Compile: 326.685ms | Execution: 47.967ms | React with ❌ to remove this embed.
Angius
Angius7mo ago
ch >= '0' && ch <= '9' if you can't use pattern matching And ecven if you can, it should ve >= and <=, not > and < My mistake there
olleeee
olleeee7mo ago
im really confused here since i have only programmed i C# for a month...
Angius
Angius7mo ago
Every string is a collection of chars
MODiX
MODiX7mo ago
Angius
REPL Result: Success
foreach (var letter in "hello")
{
Console.WriteLine($"Letter is: {letter}");
}
foreach (var letter in "hello")
{
Console.WriteLine($"Letter is: {letter}");
}
Console Output
Letter is: h
Letter is: e
Letter is: l
Letter is: l
Letter is: o
Letter is: h
Letter is: e
Letter is: l
Letter is: l
Letter is: o
Compile: 579.490ms | Execution: 57.705ms | React with ❌ to remove this embed.
Angius
Angius7mo ago
And every char has an int associated with it, according to the ASCII table
MODiX
MODiX7mo ago
Angius
REPL Result: Success
(int)'K'
(int)'K'
Result: int
75
75
Compile: 279.950ms | Execution: 21.356ms | React with ❌ to remove this embed.
Angius
Angius7mo ago
Combining those two, every char in a string has an associated number
MODiX
MODiX7mo ago
Angius
REPL Result: Success
foreach (var letter in "hello")
{
Console.WriteLine($"Letter {letter} has ASCII number {(int)letter}");
}
foreach (var letter in "hello")
{
Console.WriteLine($"Letter {letter} has ASCII number {(int)letter}");
}
Console Output
Letter h has ASCII number 104
Letter e has ASCII number 101
Letter l has ASCII number 108
Letter l has ASCII number 108
Letter o has ASCII number 111
Letter h has ASCII number 104
Letter e has ASCII number 101
Letter l has ASCII number 108
Letter l has ASCII number 108
Letter o has ASCII number 111
Compile: 578.712ms | Execution: 46.851ms | React with ❌ to remove this embed.
Angius
Angius7mo ago
Char '0' has the value of 48 Char '9' has the value of 57
olleeee
olleeee7mo ago
yes im with you so far
Angius
Angius7mo ago
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
olleeee
olleeee7mo ago
true, but how do i change the whole string to ASCCI letters?
Angius
Angius7mo ago
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 loop
olleeee
olleeee7mo ago
oh okey, i think the console write through me of because we only work in WPF and not console!
Angius
Angius7mo ago
Oof, that means you're kinda working ass-backwards. WPF is complex and hard, it should absolutely not be how you teach C#
olleeee
olleeee7mo ago
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.
Pobiega
Pobiega7mo ago
That last part isn't too weird tbh, but starting with WPF is. Anyways, did ZZZ's advice get you unstuck?
olleeee
olleeee7mo ago
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