Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,312 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,496 people online right now. Registration is fast and FREE... Join Now!




Property Value Passing

 
Reply to this topicStart new topic

Property Value Passing, Unable to pass Value to a Property

CruorAvis
19 Aug, 2008 - 04:07 PM
Post #1

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 17

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; }
        }
    }
}




User is offlineProfile CardPM
+Quote Post

Jayman
RE: Property Value Passing
19 Aug, 2008 - 05:32 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,926



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
The problem is that because you are calling the method getMessage when you set the value of MyProperty. This happens before you ever set the value of MyProperty2. Because the message has already been constructed before you set MyProperty2, it has the default value.

Just set all you properties before setting the MyProperty property and this will solve the problem.
CODE

            HappyBirthday birthdayMessage = new HappyBirthday();
            string returnedMessage;

            birthdayMessage.PresentCount = 5;
            birthdayMessage.MyProperty2 = "false";
            birthdayMessage.MyProperty = "Sahid";            
            returnedMessage = birthdayMessage.MyProperty;


            MessageBox.Show(returnedMessage);

User is offlineProfile CardPM
+Quote Post

CruorAvis
RE: Property Value Passing
19 Aug, 2008 - 06:31 PM
Post #3

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 17

Wow in all the epiphanies of Oh! Its setting it before this or that and overriding this when i was designing the classes... I never gave a second thought to the button code, let alone that myProperty was calling getMessage before myProperty2 was set... Ugh!

Thank You, Very Much!

This post has been edited by CruorAvis: 19 Aug, 2008 - 06:57 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 06:49AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month