user2405778
user2405778

Reputation: 497

Passing a true value to a boolean

I am trying to learn C# and I am up to an example that uses a boolean. For the life of me I cant figure out why the program isnt noticing that I am trying to pass a value of true to the boolean. Here is the code in the Form.cs:

namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        HappyBirthday birthdayMessage = new HappyBirthday();
        string returnedMessage;

        birthdayMessage.PresentCount = 5;
        birthdayMessage.MyProperty = "Adam";
        birthdayMessage.hasParty = true;
        returnedMessage = birthdayMessage.MyProperty;

        MessageBox.Show(returnedMessage);

    }
}
}

Here is the Class that I created:

class HappyBirthday
{

//====================
//  CLASS VARIABLES
//====================
private int numberOfPresents;
private string birthdayMessage;
private bool birthdayParty;

//===========================
//  DEFAULT CONSTRUCTOR
//===========================
public HappyBirthday()
{
    numberOfPresents = 0;
    //birthdayParty = false;
}

//===========================
//      METHOD
//===========================
private string getMessage(string givenName)
{

    string theMessage;

    theMessage = "Happy Birthday " + givenName + "\n";
    theMessage += "Number of presents = ";
    theMessage += numberOfPresents.ToString() + "\n";

    if (birthdayParty == true)
    {
        theMessage += "Hope you enjoy the party!";
    }
    else
    {
        theMessage += "No party = sorry!";
    }

    return theMessage;
}

//================================
//      READ AND WRITE PROPERTY
//================================
public string MyProperty
{
    get { return birthdayMessage; }

    set { birthdayMessage = getMessage(value); }
}

//================================
//     WRITE-ONLY PROPERTY
//================================
public int PresentCount
{
    set { numberOfPresents = value; }
}

public bool hasParty
{
    set { birthdayParty = value; }
}

}

Now I set the initial value to false (even though if my understanding is correct that should be the default value), but when I try to set it = true, the program does not recognize it. Am I supposed to pass a boolean differently then I would a string or int?

Upvotes: 4

Views: 4589

Answers (2)

Tim S.
Tim S.

Reputation: 56536

The way MyProperty works is confusing, because the set and get deal with different values (you set the name, and then get the whole message, which is confusing). I'd replace it with a GivenName property and then make the GetMessage() (or expose it as a read-only property Message) public.

Also, you can make your code much simpler by using auto-properties (you can use private gets to keep the write-only behavior, though in the real world write-only properties are very rare, and you should probably just make them public like the sets). And since the default int value is 0, you don't need to specify your default constructor. Here's how the code looks now:

class HappyBirthday
{
    public string Message
    {
        get
        {
            string theMessage;

            theMessage = "Happy Birthday " + GivenName + "\n";
            theMessage += "Number of presents = ";
            theMessage += PresentCount.ToString() + "\n";

            if (HasParty)
            {
                theMessage += "Hope you enjoy the party!";
            }
            else
            {
                theMessage += "No party = sorry!";
            }

            return theMessage;
        }
    }

    public string GivenName { private get; set; }

    public int PresentCount { private get; set; }

    public bool HasParty { private get; set; }
}

Upvotes: 0

user565869
user565869

Reputation:

You're setting MyProperty before you're setting hasParty. getMessage() is not being called every time MyProperty is polled.

Upvotes: 7

Related Questions