【问题标题】:CS0029 Compile Error - implicit conversion when outputting a integer to a text boxCS0029 编译错误 - 将整数输出到文本框时的隐式转换
【发布时间】:2019-09-15 01:34:36
【问题描述】:

我正在尝试为一个项目创建库存系统。我得到了一些类的基本代码,因此得到了列表和对象。

当我尝试使用 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;
            }
        }
    }
}

【问题讨论】:

  • 问题是Text 属性是string 类型,而您尝试存储在Text 中的一些Bike 属性不是string 类型。您需要研究转换和转换数据类型。微软有一篇关于这个here的文章。
  • 您的foreach 循环作为一个整体看起来不正确...您真的想在每次向该列表添加新的Bike 时循环整个Bikes 列表吗?
  • @Jaxix - 它可能看起来有效,但如果你追踪它(如果有帮助的话,可能在纸上),它看起来好像做了很多不必要的循环。仅仅因为它编译并不意味着它是正确的。它可能是正确的,但它肯定看起来不正确。
  • @BrootsWaymb 我承认这可能不是最有利的方式,但我对这门语言还是很陌生,我只是接受了我提供的代码并使用它,但我肯定会调查它!

标签: c# winforms


【解决方案1】:

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

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

将数字转换为string

【讨论】:

  • 这是一个 C# 问题。
【解决方案2】:

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

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;
}

【讨论】:

  • 这个答案没有解决richTextBox1.Text 将只包含bk.Forks 的值这一事实。
  • 没错。我只是解决了他的编译错误,根本没有解决任何逻辑缺陷,因为他的代码总体上没有太大意义。
  • @LewsTherin 击败了我,我是否可以使用 AppendText 来解决这个问题?
  • @Jaxix 这真的取决于你想显示什么。附加文本只会连接单个richTextBox1中的字符串,它们之间没有任何空格或换行符。
  • 要实现这一点,您应该做一些研究并设置一个列表框。下面是一些开始:c-sharpcorner.com/UploadFile/mahesh/listbox-in-C-Sharpstackoverflow.com/questions/1732054/…
猜你喜欢
  • 1970-01-01
  • 2021-02-14
  • 2014-05-28
  • 1970-01-01
  • 2020-10-04
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多