CS0029编译错误-将整数输出到文本框时的隐式转换

时间:2019-04-25 15:01:11

标签: c# winforms

我正在尝试为项目创建库存系统。已经为类提供了一些基本代码,因此为列表和对象提供了一些基本代码。

当我尝试使用WinForms将所有这些值从文件返回到RichTextBox时,出现编译错误(特别是对于隐式转换)。

我只是想知道,除了普通的textbox.txt = "some numbers"之外,是否还有其他脚本可以在WinForms中显示整数。

private void button5_Click(object sender, EventArgs e)
{
    List<Bike> Bikes = new List<Bike>();
    OpenFileDialog ofd = new OpenFileDialog();

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        string[] lines = File.ReadAllLines(ofd.FileName);
        int invCounter = 0;
        int invLength = lines.Length / 8;
        for (int i = 0; i < invLength; i++) //for each bike
        {
            string make = lines[i + invCounter++]; //store the make
            string type = lines[i + invCounter++]; //store the type
            string model = lines[i + invCounter++]; //store the model
            int year = int.Parse(lines[i + invCounter++]); //store the year
            string wheelSize = lines[i + invCounter++]; //store the wheel size
            string frameType = lines[i + invCounter++]; //store the frame type

            //store the security code
            int securityCode = int.Parse(lines[i + invCounter++]); 

            //create a new bike object with the details stored above
            Bike bk = new Bike(make, type, model, year, wheelSize, frameType,
                securityCode); 

            Bikes.Add(bk); //add the bike to Bikes list
            foreach (Bike bike in Bikes)
            {
                richTextBox1.Text = bk.Type;
                richTextBox1.Text = bk.SecurityCode; // Error here
                richTextBox1.Text = bk.Make;
                richTextBox1.Text = bk.Model;
                richTextBox1.Text = bk.Year; // Error here
                richTextBox1.Text = bk.WheelSize;
                richTextBox1.Text = bk.Forks;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您正在尝试将int分配给string属性(Text)。没有从intstring的隐式转换,因此您必须显式进行转换。只需在您要分配的.ToString()上致电int

foreach (Bike bike in Bikes)
{
    richTextBox1.Text = bk.Type;
    richTextBox1.Text = bk.SecurityCode.ToString();
    richTextBox1.Text = bk.Make;
    richTextBox1.Text = bk.Model;
    richTextBox1.Text = bk.Year.ToString();
    richTextBox1.Text = bk.WheelSize;
    richTextBox1.Text = bk.Forks;
}

答案 1 :(得分:0)

如果我理解您的问题,可以尝试:

richTextBox1.Text = bk.SecurityCode.ToString();
richTextBox1.Text = bk.Year.ToString();

将数字转换为string