learning C#
having trouble with a simple code asking for name and age and the app should output it but doesnt seem to be working any help is greatly appreciated
namespace collectNameAgeApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
int age;
if (int.TryParse(textBox2.Text, out age))
{
label3.Text = $"Hello {name}, you are {age} years old.";
}
else
{
label3.Text = "Please enter a valid age.";
}
}
}
}
16 Replies
every time i try to submit the information put in the response i get is always "Please enter a valid age."
What do you write in textBox2?
only numbers
any examples?
25
2
Alright.
Make sure there are no invisible characters
still doesnt work
time to learn how to $debug
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
you can try to use
.Text.Trim()
My best guess is that there's invisible characters
such as CRLFin the code or where im inputing values
what is CRLF?
textBox2.Text.Trim()
Carriage Return Line Feed
Basically line break
you might also be able to disable accepting return keywhere do i put that line of code
What code?
actually figured it out i need to work on my design
i had my text boxes reversed under the labels
you can rename them to give them better names