Home Ask Login Register

Developers Planet

Your answer is one click away!

Student214 February 2016

Property or indexer cannot be assigned to - it is read only

Hey i just started by C# class 2 weeks ago so i am beginner programmer and i am having trouble with my code. i have 2 classes, one of them is the test case that runs the program and the other has private variables. my variables color, NumOfWheels, StartingPoint, CurrentSpeed, and Mileage says property or indexer cannot be assigned to - it is read only when i try to build it. how do i fix this?

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;
               

Answers


David February 2016

You're trying to set a property:

Color = "";

(among other places) But that property doesn't have a setter, only a getter:

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

In order to set the value of a property, it needs a setter:

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

(repeat for your other properties as well)


It looks like you're trying to create Java-like setter methods:

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

This works, and you can call those instead of trying to set the properties:

setColor("");

But it's not the expected convention in C#. The properties can manage the backing variables themselves. In fact, you can remove the backing variables entirely and use auto-implemented properties for simple values:

public virtual string Color { get; set; }

If you just need to hold a value, a simple property does that just fine. Methods are more for operations in code, not for getting/setting simple values. (Additionally, you wouldn't want to get into the habit of calling a lot of methods from a constructor. A constructor should really just build the state of the object and nothing else.)

Post Status

Asked in February 2016
Viewed 2,715 times
Voted 10
Answered 1 times

Search




Leave an answer


Quote of the day: live life