Ello',
I'm taking a course in C# and in a bit more of a rush then ever due to starting a job as a Programmer tomorrow and would like to get to a section of the book dealing with SQL (as this is what I'll be creating an interface for). Unfortunately I've spent the last hour banging my head against the wall after having over a few dozen epiphanies that have failed. In the course I'm taking an exercise is given and I'm attempting to prove my understanding of classes and properties and passing values to private classes through properties by completing it. If you'll forgive my horrible naming conventions (as i said I'm rushing things a bit) perhaps you can help.
Essentially my problem is this:
I'm attempting to pass a value to a property to a class through a button - Having set a default construct for the property.
Unfortunately it always displays whatever is in the construct's value instead of whatever I'm trying to pass.
Here is my code:
The Interface Code: (Specifically the Button(s)
CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Classes_and_Objects
{
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 = "Sahid";
birthdayMessage.MyProperty2 = "false";
returnedMessage = birthdayMessage.MyProperty;
MessageBox.Show(returnedMessage);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
}
}
The Class/Property Code:
CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Classes_and_Objects
{
class HappyBirthday
{
private string getMessage(string givenName)
{
string theMessage;
theMessage = "Happy Birthday " + givenName + "\n";
theMessage += "Number of presents = " + numberOfPresents.ToString() + "\n";
if (party == "true")
{
theMessage += "Have a good one= " + party;
}
else if (party == "false")
{
theMessage += "Sucks to be you= " + party;
}
return theMessage;
}
private string birthdayMessage;
public string MyProperty
{
get { return birthdayMessage; }
set { birthdayMessage = getMessage(value); }
}
private int numberOfPresents;
private string party;
public HappyBirthday()
{
numberOfPresents = 0;
party = "true";
}
public int PresentCount
{
set { numberOfPresents = value; }
}
public string MyProperty2
{
set { party = value; }
}
}
}