Let's say I've got a Car
class and that class contains a Radio
object. Which looks like
class Radio
{
public string Model { get; set; }
private List<string> myChannels = new List<string>();
public List<string> PresetChannels
{
get { return myChannels; }
set { myChannels = value; }
}
private bool radioState;
public void ToggleRadio()
{
if (!radioState)
radioState = true;
else
radioState = false;
}
private string selectedChannel = string.Empty;
//
public void SetStation(int radioButton, string channelName)
{
while (!ValidateRadioButtonNumber(radioButton))
{
Console.Write("Index out of range, choose another value: ");
radioButton = Convert.ToInt32(Console.ReadLine());
}
PresetChannels[radioButton] = channelName;
Console.WriteLine("The {0} radio button was set to {1}",radioButton,channelName);
}
private bool ValidateRadioButtonNumber(int radioButton)
{
if (radioButton < 0 || radioButton > 5)
return false;
else
return true;
}
//
public void SelectChannel(int radioButton)
{
while (!ValidateRadioButtonNumber(radioButton))
{
Console.Write("Index out of range, choose another value: ");
radioButton = Convert.ToInt32(Console.ReadLine());
}
selectedChannel = PresetChannels[radioButton];
Console.WriteLine(PresetChannels[radioButton]);
}
public Radio()
{
PresetChannels = new List<string>();
PresetChannels.Capacity = 5;
//initialize every element in the list at runtime
//so the user can set any station they wish
for (int i = 0; i < PresetChannels.Capacity; i++)
{
PresetChannels.Add(string.Empty);
}
}
}
with the Car
class like
public class Car
{
public int Year { get; set; }
public string Model { get; set; }
private Radio radio;
public Radio MyRadio { get; set; }
//initialize the radio of the car
public Car()
{
radio = new Radio();
MyRadio = new Radio();
}
//containment
public void SelectStation(int radioButton)
{
radio.SelectChannel(radioButton);
}
public void SetStation(int radioButton, string channelName)
{
radio.SetStation(radioButton, channelName);
}
public void ToggleRadio()
{
radio.ToggleRadio();
}
}
If I make design the class with MyRadio
as a property, then what's the point of containment? If a property of Radio
had a private setter and you tried to set that value in the Main
method it wouldn't compile, right?
Car c = new Car();
c.SetStation(0, "99.2");
c.SetStation(10, "100"); //works
c.SelectStation(120);
Car c2 = new Car();
c2.MyRadio.SetStation(0, "99.0");//works
Console.ReadLine();
What are some general guidelines as to when one should keep a custom type a field vs. making it a property?