
Untitled
By: a guest on
Aug 5th, 2012 | syntax:
None | size: 1.80 KB | hits: 12 | expires: Never
One Constructor for a Class and its Nested Classes
class Room
{
public string Name;
public int Width;
public int Height;
// and so on...
public class Suspect
{
public string Name;
public bool PlaysCroquet;
}
public class Weapon
{
public string Name;
public bool IsShiny;
}
}
public Room(string Name, int Width, int Height)
{
this.Name = Name;
this.Width = Width;
this.Height = Height;
}
Room[i] = new Room("Conservatory", 7, 3);
Room[i] = new Room("Library", 8, 5, "Professor Plum", false, "Candlestick", true);
public class Room
{
public Room(Suspect suspect, Weapon weapon)
{
SuspectInRoom = suspect;
WeaponInRoom = weapon;
}
public Suspect SuspectInRoom { get; set; }
public Weapon WeaponInRoom { get; set; }
}
// Example usage:
Suspect coronelCustard = new Suspect("Coronel Custard");
Weapon musket = new Weapon("Musket");
Room someRoom = new Room(coronelCustard, musket);
// Then your room can be used to access all sorts of data.
Console.WriteLine(someRoom.SuspectInRoom.Nickname); // "The Big Kahuna"
Console.WriteLine(someRoom.WeaponInRoom.AttackDamage); // "20"
Room someRoom = new Room(new Suspect("Colonel Custard"), new Weapon("Musket"));
var room = new Room { Name = "Library",
Width = 7,
Height = 3,
Suspect = new Suspect { Name = "Professor Plum",
PlaysCroquet = false },
Weapon = new Weapon { Name = "Candlestick",
IsShiny = true }
};
Room[i] = new Room("Library", 8, 5, new Suspect("Professor Plum", false), new Weapon("Candlestick", true));