不能隐式地将类型string转换为int ISSUE

本文关键字:转换 int ISSUE string 类型 不能 | 更新日期: 2023-09-27 17:50:20

这是我的c#代码问题在get属性中。当我尝试返回时,它告诉我它不能将类型字符串转换为int !我试着找出问题的答案,但仍没有解决办法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DefiningProperties
{
    class BookPrices
    {
        private string Name;
        private int MarketPrice;
        private int Sale;
        private string Aouther;
        public int price
        { 
            get
            {
                return MarketPrice * Sale;
            }
            set
            {
                MarketPrice = value;
            }
            }
        public int BookDispay
        {
            get
            {
               strong text return Name + ", " + price + ", " + Aouther;
            }
        }
            public BookPrices(string bName, int bMarketPrice, int bSale, string bAouther)
        {
                Name = bName;
                MarketPrice = bMarketPrice;
                Sale = bSale;
                Aouther = bAouther;
        }
        }
    }
}

不能隐式地将类型string转换为int ISSUE

您的属性类型是int,但您试图返回string

From

public int BookDispay 

public string BookDispay

属性签名错误

您将其声明为Int并返回String

Int替换为String

问题:您没有私有string字段bookDisplay

解决方案:您需要创建一个私有字符串字段bookDisplay,然后写入公共属性来设置并获取私有bookDisplay字段的值。

试试这个:

class BookPrices
{
    private string bookDisplay; //add this private field.
    public string BookDispay    // now create a public property here
    {
        get
        {
          bookDisplay = Name + ", " + price + ", " + Aouther;
          return bookDisplay ;
        }            
    }