无法将类型字符串隐式转换为int ISSUE

时间:2014-03-14 14:12:16

标签: c#

这是我在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;
        }
        }

    }
}

4 个答案:

答案 0 :(得分:1)

您的媒体资源属于int,但您尝试返回string

答案 1 :(得分:0)

只需更改自

public int BookDispay 

到此

public string BookDispay

答案 2 :(得分:0)

问题:您没有私人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 ;
        }            
    }

答案 3 :(得分:0)

您在签名属性方面错了

您将其声明为Int并返回String

Int替换为String