Solved

Object reference not set to an instance of an object

Posted on 2008-09-30
24
1,270 Views
Last Modified: 2012-05-05
For a property of type string, I use the following condition in an if statement....

value.Length > maxLength

When I go to run the program in visual studios 2008 express debugger, I receive the following error message "Object reference not set to an instance of an object".  Why am I getting that error message?  The property is of type string and the value keyword should reference the instance of that string, right?  I tried also using the field corresponding to the property instead of value.Length, but that didn't work either. It just popped up with the same warning.  How do I go about fixing this?

I've attached the relevant code below.
public string CakeWriting
{
    get
    {
        return this.cakeWriting;
    }
    set
    {
        int maxLength;
 
        if (CakeSize == 8)
            maxLength = 16;
        else
            maxLength = 40;
 
        if (value.Length > maxLength)
        {
            MessageBox.Show("Too many letters for a " + CakeSize + " inch cake");
            if (maxLength > this.cakeWriting.Length)
                maxLength = this.cakeWriting.Length;
            this.cakeWriting = cakeWriting.Substring(0, maxLength);
        }
        else
            this.cakeWriting = value;
    }
}

Open in new window

0
Comment
Question by:jabrthel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 5
  • +3
24 Comments
 
LVL 14

Expert Comment

by:Jai S
ID: 22611794
in which line you are getting the error...
0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 22611802
try declaring a string and assigning the value to it.

and then use that as the condition

string sTmpString = value;
if (sTmpString.Length > maxLength) ....

seems kinda lame, but it should work.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22611817
try with the following:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
 public string CakeWriting
{
    get
    {
        return this.cakeWriting;
    }
    set
    {
        int maxLength;
 
        if (CakeSize == 8)
            maxLength = 16;
        else
            maxLength = 40;
 
        if (value == null)
            this.cakeWriting = "";
        else if (value.Length > maxLength)
        {
            MessageBox.Show("Too many letters for a " + CakeSize + " inch cake");
            if (maxLength > this.cakeWriting.Length)
                maxLength = this.cakeWriting.Length;
            this.cakeWriting = cakeWriting.Substring(0, maxLength);
        }
        else
            this.cakeWriting = value;
    }
} 

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 150 total points
ID: 22611853
Sorry, appears to be there is a problem with this page, here is the code:

public string CakeWriting
{
    get
    {
        return this.cakeWriting;
    }
    set
    {
        int maxLength;
 
        if (CakeSize == 8)
            maxLength = 16;
        else
            maxLength = 40;

        if (value==null)
              this.cakeWriting = "";
        else if (value.Length > maxLength)
        {
            MessageBox.Show("Too many letters for a " + CakeSize + " inch cake");
            if (maxLength > this.cakeWriting.Length)
                maxLength = this.cakeWriting.Length;
            this.cakeWriting = cakeWriting.Substring(0, maxLength);
        }
        else
            this.cakeWriting = value;
    }
}
0
 

Author Comment

by:jabrthel
ID: 22611987
xDJR1875:
I had tried that before, but it didn't work.

jaime_olivares:
Your solution worked, but why is value equal to null?  There's a textbox control that is set to have some initial text "Happy Birthday"... when I create an instance of the class this CakeWriting property is in, I pass textbox.Text into the classes constructor... the constructor then sets the CakeWriting property equal to the parameter. So... value should never be null according to my understanding, but it appears to be null... any ideas on why this is?
0
 
LVL 14

Expert Comment

by:Jai S
ID: 22612001
can you try passing a a static text like "happy birthday" instead of the textbox.Text and see whether its taking the value...if it does...the the Textbox.text is not set
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22612015
can you please share with us what are the values of CakeSize, the string that is being assigned to the property when you get the exception because then only we can replicate the exception otherwise the code in the propety looks ok

but you can try the code snipped suggested by jaime_olivares to be on the safer side
0
 
LVL 14

Expert Comment

by:Jai S
ID: 22612016
setting the value like this gives error

            CakeWriting = SourceShipNameCombo.Text;
            InitializeComponent(); //this is the class where all the controls are initialized by the designer

and setting it like this does not

            InitializeComponent();
            CakeWriting = SourceShipNameCombo.Text;
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22612037
try to put a trap into your constructor, like:

public class yourClass
{
     yourClass(string initValue)
     {
           if (initValue == null)
                MessageBox.Show("Trying to initialize with a null value");
           /// all your stuff here
     }
}

if the messagebox does not appear, then the vaue is changing in another point different than constructor
Also, maybe you have a default constructor? if so, the initial value won't be set.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22612062
@jaiganeshsrinivasan,
Please do not log with 2 users. It is extremely confusing
0
 
LVL 14

Expert Comment

by:Jai S
ID: 22612069
@jaiganeshsrinivasan,
Please do not log with 2 users. It is extremely confusing

---- i donot understand this...i ahve only one id...its jaiganeshsrinivasan...the other comments are by other users...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22612108
oops sorry, my mistake.
Some corporate users enter to the same question with 2 users, I thought it was the case.
0
 
LVL 14

Expert Comment

by:Jai S
ID: 22612170
no problemo...iam no corporate user...let stick to the question now :-)))
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22612659
Wouldn't looking at the call stack when the exception happens tell you what is setting your value to null?
0
 

Author Comment

by:jabrthel
ID: 22620890
jaiganeshsrinivasan:
I tried passing a static text "Happy Birthday" as the parameter instead of textbox.Text, but it gave me the same error

jaiganeshsrinivasan:
I have my code set up as such...
            InitializeComponent();
            CakeWriting = SourceShipNameCombo.Text;
                  
jaime_olivares:
I tried putting the trap:
public class yourClass
{
     yourClass(string initValue)
     {
           if (initValue == null)
                MessageBox.Show("Trying to initialize with a null value");
           /// all your stuff here
     }
}
and when I ran the code, it popped up with the MessageBox

Tiggerito:
When I look at the call stack, it appears that it is passing the value "Happy Birthday" as the parameter, but the value is = null in the property according to the call stack.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22620971
well, then your problem is that somewhere in your code you are passing a null string to the constructor, I suggest to solve with this:

     yourClass(string initValue)
     {
           if (initValue == null)
                initValue = "";
           /// all your stuff here
     }
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22634929
Something is setting CakeWriting to null, so at some point the "Happy Birthday" is getting lost.

Maybe placing a break point where you set  "Happy Birthday" and stepping into things will give you a clue.



0
 

Author Comment

by:jabrthel
ID: 22646036
I've tried looking for where I could be setting it to null... but to no avail.  Maybe I just need another set of eyes.  Could someone take a look at this via the attached file?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Chapter_5_Project_1
{
    class Party
    {
        const int CostOfFoodPerPerson = 25;
        private bool fancyDecorations;
        public decimal CostOfDecorations = 0;
 
        public Party(int numberOfPeople, bool fancyDecorations)
        {
            this.fancyDecorations = fancyDecorations;
            this.NumberOfPeople = numberOfPeople;
        }
 
        private int numberOfPeople;
        public virtual int NumberOfPeople
        {
            get{ return numberOfPeople; }
            set
            {
                numberOfPeople = value;
                CalculateCostOfDecorations(fancyDecorations);
            }
        }
 
        public void CalculateCostOfDecorations(bool fancy)
        {
            fancyDecorations = fancy;
            if (fancy)
            {
                CostOfDecorations = (NumberOfPeople * 15.00M) + 50M;
            }
            else
            {
                CostOfDecorations = (NumberOfPeople + 7.50M) + 30M;
            }
        }
 
        public virtual decimal CalculateCost()
        {
            //Each person cost $25 for food plus cost of beverages
            decimal TotalCost = CostOfDecorations + (CostOfFoodPerPerson * NumberOfPeople);
            if (NumberOfPeople > 12)
            {
                TotalCost += 100M;
            }
            return TotalCost;
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Chapter_5_Project_1
{
    class DinnerParty : Party
    {
        public decimal CostOfBeveragesPerPerson;
 
        public DinnerParty(int numberOfPeople, bool healthyOption, bool fancyDecorations) : base(numberOfPeople, fancyDecorations)
        {
            SetHealthyOption(healthyOption);
            CalculateCostOfDecorations(fancyDecorations);
        }
 
        
        public void SetHealthyOption(bool healthy)
        {
            if (healthy)
            {
                CostOfBeveragesPerPerson = 5M;
            }
            else
            {
                CostOfBeveragesPerPerson = 20M;
            }
        }
 
        public decimal CalculateCost(bool healthy)
        {
            decimal totalCost;
 
            totalCost = base.CalculateCost();
            totalCost += (CostOfBeveragesPerPerson * base.NumberOfPeople);
 
            if (healthy)
            {
                return totalCost * 0.95M;
            }
            else
            {
                return totalCost;
            }
        }
    }
}
 
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Chapter_5_Project_1
{
    class BirthdayParty : Party
    {
        public int CakeSize;
 
        public BirthdayParty(int numberOfPeople, bool fancyDecorations, string cakeWriting) : base(numberOfPeople, fancyDecorations)
        {
            CalculateCakeSize();
            CalculateCostOfDecorations(fancyDecorations);
            this.CakeWriting = cakeWriting;
        }
 
        public override int NumberOfPeople
        {
            get
            {
                return base.NumberOfPeople;
            }
            set
            {
                base.NumberOfPeople = value;
                CalculateCakeSize();
                this.CakeWriting = cakeWriting;
            }
        }
 
        private string cakeWriting;
        public string CakeWriting
        {
            get
            {
                return this.cakeWriting;
            }
            set
            {
                int maxLength;
 
                if (CakeSize == 8)
                    maxLength = 16;
                else
                    maxLength = 40;
                
                if (value.Length > maxLength)
                {
                    MessageBox.Show("Too many letters for a " + CakeSize + " inch cake");
                    if (maxLength > this.cakeWriting.Length)
                        maxLength = this.cakeWriting.Length;
                    this.cakeWriting = cakeWriting.Substring(0, maxLength);
                }
                else
                    this.cakeWriting = value;
            }
        }
 
        private void CalculateCakeSize()
        {
            if (base.NumberOfPeople > 4)
            {
                CakeSize = 16;
            }
            else
            {
                CakeSize = 8;
            }
        }
 
        public override decimal CalculateCost()
        {
            decimal totalCost;
 
            totalCost = base.CalculateCost();
 
            if (CakeSize > 8)
                totalCost += 75M;
            else
                totalCost += 40M;
 
            totalCost += cakeWriting.Length * 0.25M;
 
            return totalCost;
        }
    }
}
 
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 Chapter_5_Project_1
{
    public partial class Form1 : Form
    {
        DinnerParty dinnerParty;
        BirthdayParty birthdayParty;
 
        public Form1()
        {
            InitializeComponent();
            dinnerParty = new DinnerParty((int)numberOfPeople.Value, healthyOption.Checked, fancyDecorations.Checked) ;
            birthdayParty = new BirthdayParty((int)numberBirthday.Value, fancyBirthday.Checked, cakeWriting.Text); 
            DisplayDinnerPartyCost();
        }
 
        private void DisplayDinnerPartyCost()
        {
            decimal Cost = dinnerParty.CalculateCost(healthyOption.Checked);
            costLabel.Text = Cost.ToString("c");
        }
 
        public void DisplayBirthdayPartyCost()
        {
            decimal Cost = birthdayParty.CalculateCost();
            birthdayCost.Text = Cost.ToString("c");
        }
 
        private void numberOfPeople_ValueChanged(object sender, EventArgs e)
        {
            dinnerParty.NumberOfPeople = (int)numberOfPeople.Value;
            DisplayDinnerPartyCost();
        }
 
        private void fancyDecorations_CheckedChanged(object sender, EventArgs e)
        {
            dinnerParty.CalculateCostOfDecorations(fancyDecorations.Checked);
            DisplayDinnerPartyCost();
        }
 
        private void healthyOption_CheckedChanged(object sender, EventArgs e)
        {
            dinnerParty.SetHealthyOption(healthyOption.Checked);
            DisplayDinnerPartyCost();
        }
 
        private void numberBirthday_ValueChanged(object sender, EventArgs e)
        {
            birthdayParty.NumberOfPeople = (int)numberBirthday.Value;
            DisplayBirthdayPartyCost();
        }
 
        private void fancyBirthday_CheckedChanged(object sender, EventArgs e)
        {
            birthdayParty.CalculateCostOfDecorations(fancyBirthday.Checked);
            DisplayBirthdayPartyCost();
        }
 
        private void cakeWriting_TextChanged(object sender, EventArgs e)
        {
            birthdayParty.CakeWriting = cakeWriting.Text;
            DisplayBirthdayPartyCost();
        }
    }
}
 
namespace Chapter_5_Project_1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.numberOfPeople = new System.Windows.Forms.NumericUpDown();
            this.fancyDecorations = new System.Windows.Forms.CheckBox();
            this.healthyOption = new System.Windows.Forms.CheckBox();
            this.label2 = new System.Windows.Forms.Label();
            this.costLabel = new System.Windows.Forms.Label();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.label3 = new System.Windows.Forms.Label();
            this.numberBirthday = new System.Windows.Forms.NumericUpDown();
            this.fancyBirthday = new System.Windows.Forms.CheckBox();
            this.label4 = new System.Windows.Forms.Label();
            this.cakeWriting = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.birthdayCost = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.numberOfPeople)).BeginInit();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numberBirthday)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(6, 7);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(92, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Number of People";
            // 
            // numberOfPeople
            // 
            this.numberOfPeople.Location = new System.Drawing.Point(6, 24);
            this.numberOfPeople.Name = "numberOfPeople";
            this.numberOfPeople.Size = new System.Drawing.Size(92, 20);
            this.numberOfPeople.TabIndex = 1;
            this.numberOfPeople.ValueChanged += new System.EventHandler(this.numberOfPeople_ValueChanged);
            // 
            // fancyDecorations
            // 
            this.fancyDecorations.AutoSize = true;
            this.fancyDecorations.Location = new System.Drawing.Point(6, 51);
            this.fancyDecorations.Name = "fancyDecorations";
            this.fancyDecorations.Size = new System.Drawing.Size(115, 17);
            this.fancyDecorations.TabIndex = 2;
            this.fancyDecorations.Text = "Fancy Decorations";
            this.fancyDecorations.UseVisualStyleBackColor = true;
            this.fancyDecorations.CheckedChanged += new System.EventHandler(this.fancyDecorations_CheckedChanged);
            // 
            // healthyOption
            // 
            this.healthyOption.AutoSize = true;
            this.healthyOption.Location = new System.Drawing.Point(6, 75);
            this.healthyOption.Name = "healthyOption";
            this.healthyOption.Size = new System.Drawing.Size(96, 17);
            this.healthyOption.TabIndex = 3;
            this.healthyOption.Text = "Healthy Option";
            this.healthyOption.UseVisualStyleBackColor = true;
            this.healthyOption.CheckedChanged += new System.EventHandler(this.healthyOption_CheckedChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(6, 99);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(31, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Cost:";
            // 
            // costLabel
            // 
            this.costLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.costLabel.Location = new System.Drawing.Point(44, 99);
            this.costLabel.Name = "costLabel";
            this.costLabel.Size = new System.Drawing.Size(54, 13);
            this.costLabel.TabIndex = 5;
            this.costLabel.Text = "$";
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(12, 12);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(163, 158);
            this.tabControl1.TabIndex = 6;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.fancyDecorations);
            this.tabPage1.Controls.Add(this.costLabel);
            this.tabPage1.Controls.Add(this.label1);
            this.tabPage1.Controls.Add(this.label2);
            this.tabPage1.Controls.Add(this.numberOfPeople);
            this.tabPage1.Controls.Add(this.healthyOption);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(155, 132);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Dinner Party";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.birthdayCost);
            this.tabPage2.Controls.Add(this.label5);
            this.tabPage2.Controls.Add(this.cakeWriting);
            this.tabPage2.Controls.Add(this.label4);
            this.tabPage2.Controls.Add(this.fancyBirthday);
            this.tabPage2.Controls.Add(this.numberBirthday);
            this.tabPage2.Controls.Add(this.label3);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(155, 132);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Birthday Party";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(6, 5);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(94, 13);
            this.label3.TabIndex = 0;
            this.label3.Text = "Number Of People";
            // 
            // numberBirthday
            // 
            this.numberBirthday.Location = new System.Drawing.Point(7, 21);
            this.numberBirthday.Name = "numberBirthday";
            this.numberBirthday.Size = new System.Drawing.Size(120, 20);
            this.numberBirthday.TabIndex = 1;
            this.numberBirthday.ValueChanged += new System.EventHandler(this.numberBirthday_ValueChanged);
            // 
            // fancyBirthday
            // 
            this.fancyBirthday.AutoSize = true;
            this.fancyBirthday.Location = new System.Drawing.Point(7, 48);
            this.fancyBirthday.Name = "fancyBirthday";
            this.fancyBirthday.Size = new System.Drawing.Size(115, 17);
            this.fancyBirthday.TabIndex = 2;
            this.fancyBirthday.Text = "Fancy Decorations";
            this.fancyBirthday.UseVisualStyleBackColor = true;
            this.fancyBirthday.CheckedChanged += new System.EventHandler(this.fancyBirthday_CheckedChanged);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(6, 68);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(68, 13);
            this.label4.TabIndex = 3;
            this.label4.Text = "Cake Writing";
            // 
            // cakeWriting
            // 
            this.cakeWriting.Location = new System.Drawing.Point(7, 89);
            this.cakeWriting.Name = "cakeWriting";
            this.cakeWriting.Size = new System.Drawing.Size(142, 20);
            this.cakeWriting.TabIndex = 4;
            this.cakeWriting.Text = "Happy Birthday";
            this.cakeWriting.TextChanged += new System.EventHandler(this.cakeWriting_TextChanged);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(7, 116);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(31, 13);
            this.label5.TabIndex = 5;
            this.label5.Text = "Cost:";
            // 
            // birthdayCost
            // 
            this.birthdayCost.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.birthdayCost.Location = new System.Drawing.Point(46, 112);
            this.birthdayCost.Name = "birthdayCost";
            this.birthdayCost.Size = new System.Drawing.Size(81, 16);
            this.birthdayCost.TabIndex = 6;
            this.birthdayCost.Text = "$";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(186, 181);
            this.Controls.Add(this.tabControl1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.numberOfPeople)).EndInit();
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            this.tabPage2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numberBirthday)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.NumericUpDown numberOfPeople;
        private System.Windows.Forms.CheckBox fancyDecorations;
        private System.Windows.Forms.CheckBox healthyOption;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label costLabel;
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.Label birthdayCost;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox cakeWriting;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.CheckBox fancyBirthday;
        private System.Windows.Forms.NumericUpDown numberBirthday;
        private System.Windows.Forms.Label label3;
    }
}

Open in new window

0
 

Author Comment

by:jabrthel
ID: 22646703
I'm upping the points on this just because I'm being a pain...
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22646923
In your NumberOfPeople setter you have this...

this.CakeWriting = cakeWriting;

It is setting the public property to its own priviate variable, which seems wrong.

You will be setting NumberOfPeople in your constructor which will trigger that line and set your CakeWriting to its current value of null...bang!

Note: I would change your naming convension so private variables have different names to parameters.



        public override int NumberOfPeople
        {
            get
            {
                return base.NumberOfPeople;
            }
            set
            {
                base.NumberOfPeople = value;
                CalculateCakeSize();
                this.CakeWriting = cakeWriting; // HERE!
            }
        }

Open in new window

0
 

Author Comment

by:jabrthel
ID: 22647047
it is setting the Public to the private... but it's meant to since the cakeSize depends on the number of people... so if it gets a new value for NumberOfPeople it needs to recalculcuate the cakeSize and then adjust the cakewriting length so that it fits on the cake.  The way it does that is by making the property to reset the field.  So that shouldn't be the problem, right?

If I declare private cakeWriting = "" in the BirthdayParty class... I can run the program.  So I guess the problem I'm having now is not that there is a null value, but why is the initial value "Happy Birthday" not being passed through.   My program will work after a modify the cake textbox, but it doesn't work with the initial value I've put in the cake textbox.
0
 
LVL 23

Accepted Solution

by:
Tony McCreath earned 350 total points
ID: 22647149
It seems you are only setting your birthdayParty objects CakeWriting property when the cakeWriting TextBox's text has changed.

As you are initialising the TextBox to "Happy Birthday" I don't think the TextChanged event is being called.

You will need to set the Propeties of you birthdayParty object to the values in your controls once they have been initialised. Where you create it in Form1() would work.



        private void cakeWriting_TextChanged(object sender, EventArgs e)
        {
            birthdayParty.CakeWriting = cakeWriting.Text;
            DisplayBirthdayPartyCost();
        }

Open in new window

0
 

Author Comment

by:jabrthel
ID: 22647182
That was it... there was great confusion on my end since my dinnerParty class manipulates the form directly but my BirthdayParty class does not.
0
 

Author Closing Comment

by:jabrthel
ID: 31501857
Thanks, you both helped me in different ways.  I'm splitting the points among Tiggerito and jaime_olivares.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question