O'Reilly Forums: Dinner Party Constructor Question - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dinner Party Constructor Question

#1 User is offline   Raymii 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 12
  • Joined: 11-October 11

Posted 08 April 2012 - 11:41 AM

There are two things about the constructor of the DinnerParty instance (pg. 214) I just don't get.

The constructor is:

       public DinnerParty(int numberOfPeople, bool healthyOption, bool fancyDecorations) 
        { 
            NumberOfPeople = numberOfPeople;
            this.fancyDecorations = fancyDecorations;
            SetHealthyOption(healthyOption); 
            CalculateCostOfDecorations(fancyDecorations); 


That's my understanding of what these lines of code do: First, NumberOfPeople gets set trough the "set" Methode.

        private int numberOfPeople;
        public int NumberOfPeople
        {
            get
            {
                return numberOfPeople;
            }
            set
            {
                numberOfPeople = value;
                CalculateCostOfDecorations(fancyDecorations);
            }   


When NumberOfPeople gets set, the CalculateCostOfDecorations Methode is called - with a parameter that hasn't been set yet. Shouldn't this result in an error? What am I missing here?

The additional question is about the line " this.fancyDecorations = fancyDecorations;" in the constructor. Is this really necessary? Because fancyDecorations gets set anyway by the CalculateCostOfDecorations-Method that is called right afterwards. Again, am I missing something? :-)

Many thanks in advance for any input you might have.
0

#2 User is offline   Sikta_26785 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 68
  • Joined: 18-May 11

Posted 09 April 2012 - 06:57 PM

This actually surprised me but I stepped through the code and found that even if you don't initialized the bool it is false after being declared. When you have questions like this, the best bet is to put some breakpoints in and see what values the variables hold.
0

#3 User is offline   Raymii 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 12
  • Joined: 11-October 11

Posted 11 April 2012 - 08:41 AM

Ah, that makes sense. Thanks a lot, appreciate it.
0

#4 User is offline   Rythameen 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 24-July 09

Posted 05 June 2012 - 01:44 PM

A little late to the party, but what the heck. C# assigns default values to all its data types.......like so

bool: False
byte: 0
char:
decimal: 0
double: 0
float: 0
int: 0
long: 0
object:
short: 0
signed byte: 0
string: null
unsigned int: 0
unsigned long: 0
unsigned short: 0
int?: null

And of course all reference types default to null.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users