Hello,
I've been recently trying to teach myself the fundamentals of the C# language. I've previously done some OO programming using C++ (mostly academic stuff), but I'm trying to transition myself to a newer language. I'm also trying to complete my grad project and recognize the need for an OO approach to my development efforts. So, please bear with me if my questions seem odd (I'm a bit rusty). :)
So then, let's say this is my scenario. I have a Player object that can possess between 1 and 3 dice. However, at run time, I don't always know what kind of Player I will have (it could be variable dependent on a number of things). The only way I could think of handling this would be to implement something like this:
public class Dice() {
//...code...
}
public class Player() {
Dice die1 = new Dice(); Dice die2 = new Dice(); Dice die3 = new Dice(); public Player(int val1) { die1.setval(val1); // pretend there's some method here to do this } public Player(int val1, int val2) { die1.setval(val1); die2.setval(val2); } public Player(int val1, int val2, int val3) { die1.setval(val1); die2.setval(val2); die3.setval(val3); }
Dice die1 = new Dice();
Dice die2 = new Dice();
Dice die3 = new Dice();
public Player(int val1) {
die1.setval(val1); // pretend there's some method here to do this
public Player(int val1, int val2) {
die1.setval(val1); die2.setval(val2);
die1.setval(val1);
die2.setval(val2);
public Player(int val1, int val2, int val3) {
die1.setval(val1); die2.setval(val2); die3.setval(val3);
die3.setval(val3);
Now, I know this is probably not the best example, but here's my concern. When I instantiate my Player class, I don't always want to create 3 Dice objects, especially since that Player might only have 1 or 2 (creating the others seems wasteful). Could you perhaps suggest something I should look at (like a C# language construct of some sort) that would handle this situation more elegantly?
Is there some other book or reference I should read up on that could help me brush up on such matters?
Thanks for your help in advance!
Mitch