c# - Property or indexer cannot be assigned to - it is read only -
hey started c# class 2 weeks ago beginner programmer , having trouble code. have 2 classes, 1 of them test case runs program , other has private variables. variables color, numofwheels, startingpoint, currentspeed, , mileage says property or indexer cannot assigned - read when try build it. how 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 { { return color; } } public virtual double numofwheels { { return numofwheels; } } public virtual int startingpoint { { return startingpoint; } } public virtual int currentspeed { { return currentspeed; } } public virtual int mileage { { return mileage; } } public override string tostring() { return (" color " + color + " numofwheels" + numofwheels + "startingpoint " + startingpoint + "mileage" + mileage + "current speed" + currentspeed); } } } ******************************************************************************** /// test case runs 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 car manager *"); console.writeline("* <<my name>> *"); console.writeline("* *"); console.writeline("*****************************"); console.writeline("\nenter number of wheels of car"); int numofwheels = console.read(); mycar.setwheels(numofwheels); console.writeline("enter color of car"); string color = console.readline(); console.writeline("current mileage set zero"); console.writeline("the current starting point set 100000"); console.write("the current status of car \n{0:d} wheels, \n{1}, \n{2:d} miles , \ncar point = {3:d}", mycar.getnumofwheels, mycar.getcolor, mycar.getmileage, mycar.getstartingpoint); console.writeline("\nenter owner's name"); string name = console.readline(); console.writeline("enter miles car ran in week"); int milesthisweek = console.readline; mycar.setmileage(mileage); console.writeline("this car owned n{1}", name); console.writeline("===>the current status of 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 using car manager *************"); console.writeline("----------------------------------------------------------"); console.writeline("----------------------------------------------------------"); console.writeline("press enter close console……."); } } }
you're trying set property:
color = ""; (among other places) property doesn't have setter, getter:
public virtual string color { { return color; } } in order set value of property, needs setter:
public virtual string color { { return color; } set { color = value; } } (repeat other properties well)
it looks like you're trying create java-like setter methods:
public virtual void setcolor(string color) { this.color = color; } this works, , can call instead of trying set properties:
setcolor(""); but it's not expected convention in c#. properties can manage backing variables themselves. in fact, can remove backing variables entirely , use auto-implemented properties simple values:
public virtual string color { get; set; } if need hold value, simple property fine. methods more operations in code, not getting/setting simple values. (additionally, wouldn't want habit of calling lot of methods constructor. constructor should build state of object , nothing else.)
Comments
Post a Comment