コード例 #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            HappyBirthday birthdayMessage = new HappyBirthday();
            string        returnedMessage;

            birthdayMessage.PresentCount   = 10;
            birthdayMessage.MyProperty     = ("Shane");
            birthdayMessage.PartyStatus    = true; //Why is this not working properly?...Isn't this new value supposed to be passed over to the "PartyStatus" property variable in the HappyBirthday class?....Same goes for the code beneath.....WTF?
            birthdayMessage.PartyAttendees = 27;   //Why is this not working properly?...It seems to be set up the same as the value assignment for birthdayMessage.PresentCount
            returnedMessage = birthdayMessage.MyProperty;

            MessageBox.Show(returnedMessage);
        }
コード例 #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            HappyBirthday birthdayMessage = new HappyBirthday();
            string        returnedMessage;

            /*******************************************************************************
             * ORIGINAL CODE
             *
             * birthdayMessage.PresentCount = 10;
             * birthdayMessage.MyProperty = ("Shane");
             * birthdayMessage.PartyStatus = true; //Why is this not working properly?...Isn't this new value supposed to be passed over to the "PartyStatus" property variable in the HappyBirthday class?....Same goes for the code beneath.....WTF?
             * birthdayMessage.PartyAttendees = 27; //Why is this not working properly?...It seems to be set up the same as the value assignment for birthdayMessage.PresentCount
             * returnedMessage = birthdayMessage.MyProperty;
             *
             * The whole problem you're having is with the order that things happen.
             *
             * The message that gets built in getMessage() is using the original values
             * in the partyYayNay and partyComers fields.
             *
             * You are setting PartyStatus and PartyAttendees properties AFTER
             * you set MyProperty.
             *
             * The message that gets built in getMessage doesn't automatically update
             * when the values that were used change. You need to move the line of code
             * that sets MyProperty so that it's building the message after you've set your
             * other values to what you want them to be.
             *
             * Stepping through the code that builds the message using the debugger
             * reveals that the values have not yet been changed at the time the message
             * is being built.
             *
             ******************************************************************************/

            // Fixed code:

            birthdayMessage.PresentCount   = 10;
            birthdayMessage.PartyStatus    = true;
            birthdayMessage.PartyAttendees = 27;
            birthdayMessage.MyProperty     = ("Shane"); // moved this AFTER PartyStatus & PartyAttendees are set

            returnedMessage = birthdayMessage.MyProperty;

            MessageBox.Show(returnedMessage);
        }