is there a NZ function in C#

Hello,
I want to be able to add textboxes - even if one is empty- is there any type of function in C# like NZ?
is there a way to add to code example below?
thank you
private void button1_Click(object sender, EventArgs e)
        {
            double firstDay, secondDay,thirdDay,fourthDay,fifthDay,Total;

            firstDay = double.Parse(textBox1.Text);
            secondDay = double.Parse(textBox2.Text);
            thirdDay = double.Parse(textBox3.Text);
            fourthDay = double.Parse(textBox4.Text);
            fifthDay = double.Parse(textBox5.Text);


            Total = firstDay + secondDay + thirdDay + fourthDay + fifthDay;

            textBox6.Text = "the total is" + Total;
        }

Open in new window

davetoughAsked:
Who is Participating?
 
Miguel OzSoftware EngineerCommented:
See the attached code implementing my previous post.
TryParse will return false if the conversion fails and in this case the default is 0.

Note : All previous code from nandithaa will throw an exception if your textbox contains a value that can not be converted to double.  (double.Parse requires exception handling, thats why.net offers TRyParse method)

        private void button1_Click(object sender, EventArgs e)
        {
            double firstDay, secondDay, thirdDay, fourthDay, fifthDay, Total;

            if (!double.TryParse(textBox1.Text, out firstDay))
            {
                firstDay = 0;
            }
            if (!double.TryParse(textBox2.Text, out secondDay))
            {
                secondDay= 0;
            }
            if (!double.TryParse(textBox3.Text, out thirdDay))
            {
                thirdDay= 0;
            }
            if (!double.TryParse(textBox4.Text, out fourthDay))
            {
                fourthDay= 0;
            }
            if (!double.TryParse(textBox5.Text, out fifthDay))
            {
                fifthDay= 0;
            } 

            Total = firstDay + secondDay + thirdDay + fourthDay + fifthDay;
            textBox6.Text = "the total is" + Total; 
}

Open in new window

0
 
Miguel OzSoftware EngineerCommented:
Not really, you can see an implentation here:
http://www.developmentnow.com/g/38_2005_9_0_0_600617/Emulate-Nz-Function.htm

At the end you are trying to test whether the value is OK or not I will suggest using TryParse method:
http://msdn.microsoft.com/en-us/library/994c0zb1.aspx
http://dotnetperls.com/double-parse

I attached the sample code from the last link, you just need to replace the sample in your code.

string[] unusuals = new string[]
        {
            "NaN",              // <-- This can be parsed.
            "MaxValue",         // <-- This fails.
            "NegativeInfinity",
            "Programmer",
            "0.01-0.02",
            "    0"             // <-- This succeeds and is 0.
        };

        foreach (string unusual in unusuals)
        {
            double value;
            if (double.TryParse(unusual, out value)) // Returns bool
            {
                Console.WriteLine("Valid: {0}", value);
            }
        }

Open in new window

0
 
nandithaaCommented:
U can try like this:
private void button1_Click(object sender, EventArgs e)
{
	double firstDay, secondDay,thirdDay,fourthDay,fifthDay,Total;
	if(textBox1.Text!="")
	{

		firstDay = double.Parse(textBox1.Text);
	}
	else
	{
		firstDay =0;
	}
	if(textBox2.Text!="")
	{
		secondDay = double.Parse(textBox2.Text);
	}
	else
	{
		secondDay =0;
	}
	if(textBox3.Text!="")
	{
		thirdDay = double.Parse(textBox3.Text);
	}
	else
	{
		thirdDay =0;
	}
	if(textBox4.Text!="")
	{        
		fourthDay = double.Parse(textBox4.Text);
	}
	else
	{
		fourthDay =0;
	}
	if(textBox5.Text!="")
	{ 
		fifthDay = double.Parse(textBox5.Text);
	}
	else
	{
		fifthDay =0;
	}
	Total = firstDay + secondDay + thirdDay + fourthDay + fifthDay;
	textBox6.Text = "the total is" + Total;
}

Open in new window

0
 
nandithaaCommented:
Or u can try like this:
private void button1_Click(object sender, EventArgs e)
{
	double firstDay, secondDay,thirdDay,fourthDay,fifthDay,Total;
	if (String.IsNullOrEmpty(textBox1.Text))   
	{
		firstDay =0;		
	}
	else
	{
		firstDay = double.Parse(textBox1.Text);
	}
	if (String.IsNullOrEmpty(textBox2.Text))
	{
		secondDay =0;
	}
	else
	{
		secondDay = double.Parse(textBox2.Text);
		
	}
	if (String.IsNullOrEmpty(textBox3.Text))
	{
		thirdDay =0;	
	}
	else
	{
		thirdDay = double.Parse(textBox3.Text);
	}
	if (String.IsNullOrEmpty(textBox4.Text))
	{        
		fourthDay =0;
	}
	else
	{
		fourthDay = double.Parse(textBox4.Text);
	}
	if (String.IsNullOrEmpty(textBox5.Text))
	{ 
		fifthDay =0;
	}
	else
	{
		fifthDay = double.Parse(textBox5.Text);
	}
	Total = firstDay + secondDay + thirdDay + fourthDay + fifthDay;
	textBox6.Text = "the total is" + Total;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.