-4

Hi i'm trying to add information into an int but i don't know how to declare it as an Int

I am trying to input an age as an int but don't know how.

my code:

        Author authorAdd = new Author();
        authorAdd.Name = txtName.Text;
        authorAdd.Age = txtAge.Text;

I have tried .Int I know that it is not .Text that i need but i can't remember what I do need, thanks.

2 Answers2

1

Try like this

authorAdd.Age = int.Parse(txtAge.Text);

If age can be null

Then try like this

int age;

if(int.TryParse(txtAge.Text,out age)){
  authorAdd.Age = age;
}
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
1

Try the following:

authorAdd.Age = int.Parse(txtAge.Text);
Mr. B
  • 2,845
  • 1
  • 21
  • 31