【问题标题】:Property or indexer cannot be assigned to - it is read only无法分配属性或索引器 - 它是只读的
【发布时间】:2016-02-06 20:01:19
【问题描述】:

嘿,我两周前刚开始上 C# 课,所以我是初学者 我的代码有问题。我有 2 个类,其中一个是运行程序的测试用例,另一个是私有变量。我的变量颜色、NumOfWheels、StartingPoint、CurrentSpeed 和 Mileage 表示无法分配属性或索引器 - 只有在我尝试构建它时才会读取它。我该如何解决?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1 
{
    class Car
    {
        private string color;
        private int numOfWheels;
        private int startingPoint;
        private int mileage;
        private int currentSpeed;

        public Car()
        {
            Color = "";
            NumOfWheels = 4;
            StartingPoint = 100000;
            CurrentSpeed = 0;
            Mileage = 0;
        }

        public Car(string color, int numOfWheels, int startingPoint, int currentSpeed, int mileage)
        {
            Color = color;
            NumOfWheels = numOfWheels;
            StartingPoint = startingPoint;
            CurrentSpeed = currentSpeed;
            Mileage = mileage;
        }

        public virtual void setcolor(string color)
        {
            this.color = color;
        }

        public virtual void setnumOfWheels(int numOfWheels)
        {
            this.numOfWheels = numOfWheels;
        }


        public virtual string Color
        {
            get
            {
                return color;
            }
        }

        public virtual double NumOfWheels
        {
            get
            {
                return numOfWheels;
            }
        }

        public virtual int StartingPoint
        {
            get
            {
                return startingPoint;
            }
        }

        public virtual int CurrentSpeed
        {
            get
            {
                return currentSpeed;
            }
        }

        public virtual int Mileage
        {
            get
            {
                return mileage;
            }
        }


        public override string ToString()
        {
            return (" color " + color + " numOfWheels" + numOfWheels + "startingPoint " + startingPoint + "mileage" + mileage + "current speed" + currentSpeed);
        }
     }
}

********************************************************************************
///  this is the test case that runs the program 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    class CarTest
    {
       static void Main(string[] args)
        {


            Car myCar = new Car();


                Console.WriteLine("*****************************"); 
                Console.WriteLine("*                           *"); 
                Console.WriteLine("* WELCOME TO CAR MANAGER    *");
                Console.WriteLine("*  By <<my Name>>   *"); 
                Console.WriteLine("*                           *");
                Console.WriteLine("*****************************");



            Console.WriteLine("\nEnter the number of wheels of a car");
            int numOfWheels = Console.Read();
                myCar.setWheels(numOfWheels);



            Console.WriteLine("Enter the color of the car");
            String color = Console.ReadLine();

            Console.WriteLine("Current mileage will be set to zero");

            Console.WriteLine("The current starting point will be set to 100000");

            Console.Write("The current status of your car \n{0:D} Wheels, \n{1}, \n{2:D} Miles and \nCAR POINT = {3:D}", myCar.getNumOfWheels, 
            myCar.getColor, myCar.getMileage, myCar.getStartingPoint);

            Console.WriteLine("\nEnter the owner's name");
            String name = Console.ReadLine();

            Console.WriteLine("Enter the miles the car ran in this week");
            int milesThisWeek = Console.ReadLine;
            myCar.setMileage(Mileage);

            Console.WriteLine("This car is owned by n{1}", name); 


                Console.WriteLine("===>The current status of your car:");
            Console.WriteLine("Wheels: " + myCar.getWheels());
            Console.WriteLine("Color: " + myCar.getColor());
                Console.WriteLine("Current Mileage: " + myCar.getMileage());
            Console.WriteLine("Starting Point: " + myCar.getStartingPoint());
            Console.WriteLine("************ Thank you for using CAR MANAGER *************");
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("Press ENTER to close console…….");    
        }
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    您正在尝试设置属性:

    Color = "";
    

    (在其他地方)但是该属性没有 setter,只有 getter:

    public virtual string Color
    {
        get
        {
            return color;
        }
    }
    

    为了设置一个属性的值,它需要一个setter:

    public virtual string Color
    {
        get
        {
            return color;
        }
        set
        {
            color = value;
        }
    }
    

    (也对您的其他属性重复)


    看起来您正在尝试创建类似 Java 的 setter 方法:

    public virtual void setcolor(string color)
    {
        this.color = color;
    }
    

    有效,您可以调用它们而不是尝试设置属性:

    setColor("");
    

    但是这不是 C# 中的预期约定。这些属性可以自己管理支持变量。事实上,您可以完全删除支持变量,并为简单值使用自动实现的属性:

    public virtual string Color { get; set; }
    

    如果你只需要保存一个值,一个简单的属性就可以了。方法更多地用于代码中的操作,而不是用于获取/设置简单值。 (此外,您不希望养成从构造函数调用大量方法的习惯。构造函数实际上应该只构建对象的状态,而不是其他任何东西。)

    【讨论】:

    • 并删除所有那些'java-like' setX,setY 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    相关资源
    最近更新 更多