© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
36 replies
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;
}
}




}

}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ methods
C#CC# / help
8mo ago
✅ Methods
C#CC# / help
16mo ago
overriding methods
C#CC# / help
15mo ago