C# WindowsForms-如何在C中将整数转换为字符串#

C# WindowsForms-如何在C中将整数转换为字符串#,c#,winforms,C#,Winforms,我是C#的新手,我正在创建一个表单,该表单应该让用户有机会输入姓名和年龄。然后,通过提交这些信息,应该有一个摘要(一个新的表单),显示用户输入的内容。 我终于找到了这个名字,因为它是一个字符串,没有什么大不了的,但我被年龄所困扰。 我试过铸造,但是,它不起作用。我还查看了文档,但没有发现任何有用的内容。嗯,可能是因为我不知道去哪里找。 无论如何,如果有人能给我举个例子,我将不胜感激。 提前谢谢 FormEnterDetails.cs PersonStatic.LName = this.textB

我是C#的新手,我正在创建一个表单,该表单应该让用户有机会输入姓名和年龄。然后,通过提交这些信息,应该有一个摘要(一个新的表单),显示用户输入的内容。 我终于找到了这个名字,因为它是一个字符串,没有什么大不了的,但我被年龄所困扰。 我试过铸造,但是,它不起作用。我还查看了文档,但没有发现任何有用的内容。嗯,可能是因为我不知道去哪里找。 无论如何,如果有人能给我举个例子,我将不胜感激。 提前谢谢

FormEnterDetails.cs

PersonStatic.LName = this.textBoxLastName.Text;
PersonStatic.Age = this.textBoxAge.Text;
private string lName;
public string LName
{
    get { return lName; }
    set { lName = value; }
}

string age;
public String Age
{
    get { return age; }
    set { age = value; }
}
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);
}
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; }
}
DetailsHolder.cs

PersonStatic.LName = this.textBoxLastName.Text;
PersonStatic.Age = this.textBoxAge.Text;
private string lName;
public string LName
{
    get { return lName; }
    set { lName = value; }
}

string age;
public String Age
{
    get { return age; }
    set { age = value; }
}
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);
}
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; }
}
FormSummary.cs

PersonStatic.LName = this.textBoxLastName.Text;
PersonStatic.Age = this.textBoxAge.Text;
private string lName;
public string LName
{
    get { return lName; }
    set { lName = value; }
}

string age;
public String Age
{
    get { return age; }
    set { age = value; }
}
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);
}
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; }
}
PersonStatic.cs

PersonStatic.LName = this.textBoxLastName.Text;
PersonStatic.Age = this.textBoxAge.Text;
private string lName;
public string LName
{
    get { return lName; }
    set { lName = value; }
}

string age;
public String Age
{
    get { return age; }
    set { age = value; }
}
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);
}
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; }
}

我希望你想要这样的东西

string ageString = ageInt.ToString();
使用
ToString()
内置函数将任何内容转换为字符串:

Int x=5;
String y;
y=x.ToString();

从您发布的代码来看,所有与年龄相关的地方似乎都已经在使用
string
,而不是
int
(请注意,这些是缩短实际类型名称的关键字,
System.string
System.Int32
)。因此,您应该在这一行中得到一个错误:

this.labelAge.Text = Int32.Parse(dh.Age);
字符串
转换为
int
。(如果您想以另一种方式转换,那么正如其他答案所提到的,您可以在
int
上调用
ToString()
实例方法)

但是在本例中,
dh.Age
,假设
dh
DetailsHolder
的一个实例,已经是一个
字符串。假设
labelAge
System.Windows.Forms.Label
的一个实例,那么它也是一个
字符串。因此,您无需进行任何转换:

this.labelAge.Text = dh.Age;

通常,您只需将
.ToString()
放在要转换为
字符串的任何字符串的末尾,因此在本例中
int myInt=0;字符串theString=myInt.ToString()。如果根本看不到任何代码,就很难判断您做错了什么,因为将年龄表示为整数不应该引起任何问题,并且将其转换为字符串似乎是多余的。不要将int转换为字符串,请使用
ToString()
获取其字符串表示形式。要从字符串中获取int值,请使用
int.Parse()
。顺便说一句,您可以查找框架提供的类型,如
Label