I am new to C# and I am creating a Form that is supposed to give the user the opportunity to enter name and age. Then, by submitting this information, there should be a summary(a new form) that shows what the user their input. I finally got it working with the name, as it's a string and it's not a big deal, but I am stuck with the age. I've tried casting, however, it does not work. I also looked at the documentation, but I do not find anything useful. Well, probably because I don't know where to look. Anyway, I would strongly appreciate if someone give me an example for this. Thanks in advance.
FormEnterDetails.cs
PersonStatic.LName = this.textBoxLastName.Text;
PersonStatic.Age = this.textBoxAge.Text;
DetailsHolder.cs
private string lName;
public string LName
{
get { return lName; }
set { lName = value; }
}
string age;
public String Age
{
get { return age; }
set { age = value; }
}
FormSummary.cs
private void FormSummary_Load(object sender, EventArgs e)
{
//we need to do this work on form load and not on creation
this.labelFirstNameSummary.Text = dh.FName;
this.labelLastNameSummary.Text = dh.LName;
this.labelAge.Text = Int32.Parse(dh.Age);
}
PersonStatic.cs
static string lName;
public static string LName
{
get { return PersonStatic.lName; }
set { PersonStatic.lName = value; }
}
static string age;
public static string Age
{
get { return PersonStatic.age;}
set { PersonStatic.age = value; }
}
.ToString()
on the end of whatever you want to cast to astring
, so in this caseint myInt = 0; string theString = myInt.ToString();
. Without seeing any code at all it's hard to tell what you're doing wrong as representing the age as an integer shouldn't cause any issues and casting it to a string seems redundant.ToString()
to get it's string representation. For getting an int value from a string, useint.Parse()
.