I’m learning about accessor methods and enumeration. I wrote a public class ‘Car’ under the namespace ‘Vehicles’, and set private properties such as _manufacturer, _model, _year and _color. I’d like to write a single method to access properties and another to set/update them. This is my class:
using System;
namespace Vehicles
{
public class Car
{
private string _manufacturer;
private string _model;
private string _year;
private string _color;
public void honkHorn()
{
// Add argument for a file name?
// Code here to play a WAV file?
MessageBox.Show("Honk!");
}
public string getCarInfo(string whichProperty)
{
switch (whichProperty)
{
case ("manufacturer"):
return _manufacturer;
case ("model"):
return _model;
case ("year"):
return _year;
case ("color"):
return _color;
default:
return null;
}
}
public void setCarInfo(string whichProperty, string newValue)
{
switch (whichProperty)
{
case ("manufacturer"):
_manufacturer = newValue;
break;
case ("model"):
_model = newValue;
break;
case ("year"):
_year = newValue;
break;
case ("color"):
_color = newValue;
break;
}
}
}
}
And this is my form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Vehicles;
namespace CS_Enumeration
{
public partial class Form1 : Form
{
public Car myCar = new Car();
public Form1()
{
InitializeComponent();
myCar.setCarInfo("manufacturer", "Ford");
labelManfValue.Text = myCar.getCarInfo("manufacturer");
myCar.setCarInfo("model", "Ranger");
labelModelValue.Text = myCar.getCarInfo("model");
myCar.setCarInfo("year", "2012");
labelYearValue.Text = myCar.getCarInfo("year");
myCar.setCarInfo("color", "Blue");
labelColorValue.Text = myCar.getCarInfo("color");
}
private void button1_Click(object sender, EventArgs e)
{
myCar.honkHorn();
}
}
}
Is this really the best way to write a single method that can get/set? I first tried to cast a string value that matched the name of the object property and return the actual property, but that doesn’t work (unless someone knows how to cast a string to an object property?).
Thanks for the replies. This is all an exercise from a book I’m reading. It goes so far as to says that not everything should be public, but not everything should be private either. So how do I know when things should/should not be public/private? Sounds like the book is leading me in the wrong direction as to what’s good coding design. Anyone have any book suggestions for learning good coding design practices for Visual C#?
Don’t do this.
Use public properties instead and you gain type safety and a much more expressive usage of your class. In your current approach any typo in the property name string will cause a run-time exception instead of a compilation error.
Just use properties:
Now you can just access the properties directly:
Also you should define a constructor that fully initializes a Car object, otherwise you might have some properties set, and others not.