8 Replies - 430 Views - Last Post: 10 December 2013 - 07:47 AM Rate Topic: -----

#1 Pikey85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 15-November 13

If statement not working unsure what I've done wrong

Posted 09 December 2013 - 05:54 PM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks; // added 
using System.Windows.Forms;

namespace MCC
{
    public partial class Form1 : Form
    {

        int TotalValue;
        int CostPerCredit;
        int NumberOfCredit;
        int CoinOnePence;
        int CoinTwoPence;
        int CoinFivePence;
        int CoinTenPence;
        int CoinTwentyPence;
        int CoinFiftyPence;
        int CoinOnePound;
        int CoinTwoPound;
        double DblTotalValue;
        string StrTotalValue;



        public Form1()
        {
            InitializeComponent();
        }


      
     

        private void btn_1p_Click(object sender, EventArgs e)
        {
            if (CostPerCredit > 0)
            {
                CoinOnePence = CoinOnePence +1;
                label_1p.Text = CoinOnePence.ToString();
                TotalValue = TotalValue + 1;
                textBox_pence.Text = TotalValue.ToString();
                NumberOfCredit = TotalValue / CostPerCredit;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                StrTotalValue = String.Format("{0:n2}",(DblTotalValue));
                textBox_poundpence.Text = StrTotalValue.ToString();
            }
            else
            {
                MessageBox.Show("You have not set the cost per credit!");
            }


        }


When I enter 0 or any other number into the textBox.costpercredit I am still getting the messagebox showing... Is it because I haven't linked the textbox with the variable?

I have tried this with no luck..

textBox_CostPerCredit.Text = CostPerCredit.ToString();

Is This A Good Question/Topic? 0
  • +

Replies To: If statement not working unsure what I've done wrong

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 181
  • View blog
  • Posts: 658
  • Joined: 03-February 10

Re: If statement not working unsure what I've done wrong

Posted 09 December 2013 - 06:51 PM

Your variable CostPerCredit does not change from zero. You are not changing the value of CostPerCredit before you check it.

So you would need to get the value from the text box, check the value to make sure it is valid and then you would need to assign it to your variable.

     var enteredCostPerCredit = textBox_CostPerCredit.Text;
     
     if(!int.TryParse(enteredCostPerCredit, out CostPerCredit) || CostPerCredit <= 0)
     {
         //if the value in the text box could not be parsed to an int
         MessageBox.Show("You have not set the cost per credit!");

     }
     else
     {
         //your code
     }

     


This post has been edited by demausdauth: 09 December 2013 - 06:53 PM

Was This Post Helpful? 1
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 764
  • View blog
  • Posts: 3,334
  • Joined: 03-December 12

Re: If statement not working unsure what I've done wrong

Posted 09 December 2013 - 06:54 PM

If you are assigning value to a variable,

Quote

textBox_CostPerCredit.Text = CostPerCredit.ToString();

It works better in reverse.
Was This Post Helpful? 1
  • +
  • -

#4 Pikey85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 15-November 13

Re: If statement not working unsure what I've done wrong

Posted 10 December 2013 - 03:17 AM

Hi thanks for replying.

I have had a go at it this morning but still having troubles. Here is what I have done.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks; // added 
using System.Windows.Forms;

namespace MCC
{
    public partial class Form1 : Form
    {

        int TotalValue;
        int CostPerCredit;
        int NumberOfCredit;
        int CoinOnePence;
        int CoinTwoPence;
        int CoinFivePence;
        int CoinTenPence;
        int CoinTwentyPence;
        int CoinFiftyPence;
        int CoinOnePound;
        int CoinTwoPound;
        double DblTotalValue;
        string StrTotalValue;


        public Form1()
        {
            InitializeComponent();
        }



        

        private void btn_1p_Click(object sender, EventArgs e)
        {
            var EnteredCostPerCredit = textBox_CostPerCredit.Text;

            if (!int.TryParse(EnteredCostPerCredit, out CostPerCredit) || CostPerCredit <= 0)
            {
                CoinOnePence = CoinOnePence +1;
                label_1p.Text = CoinOnePence.ToString();
                TotalValue = TotalValue + 1;
                textBox_pence.Text = TotalValue.ToString();
                NumberOfCredit = TotalValue / CostPerCredit;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                StrTotalValue = String.Format("{0:n2}",(DblTotalValue));
                textBox_poundpence.Text = StrTotalValue.ToString();
                
            }
            else
            {
                MessageBox.Show("You have not set the cost per credit!");
            }


        }

        private void btn_2p_Click(object sender, EventArgs e)
        {

            if (CostPerCredit > 0)
            {
                CoinTwoPence = CoinTwoPence + 1;
                label_2p.Text = CoinTwoPence.ToString();
                TotalValue = TotalValue + 2;
                textBox_pence.Text = TotalValue.ToString();
                NumberOfCredit = TotalValue / CostPerCredit;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                StrTotalValue = String.Format("{0:n2}", (DblTotalValue));
                textBox_poundpence.Text = StrTotalValue.ToString();
            }
            else
            {
                MessageBox.Show("You have not set the cost per credit!!");

            }
        }
        private void btn_5p_Click(object sender, EventArgs e)
        {

            if (CostPerCredit > 0)
            {
                CoinFivePence = CoinFivePence + 1;
                label_5p.Text = CoinFivePence.ToString();
                TotalValue = TotalValue + 5;
                textBox_pence.Text = TotalValue.ToString();
                NumberOfCredit = TotalValue / CostPerCredit;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                StrTotalValue = String.Format("{0:n2}", (DblTotalValue));
                textBox_poundpence.Text = StrTotalValue.ToString();
            }
            else
            {
                MessageBox.Show("You have not set the cost per credit!!");

            }
        }

        private void btn_10p_Click(object sender, EventArgs e)
        {

            if (CostPerCredit > 0)
            {
                CoinTenPence = CoinTenPence + 1;
                label_10p.Text = CoinTenPence.ToString();
                TotalValue = TotalValue + 10;
                textBox_pence.Text = TotalValue.ToString();
                NumberOfCredit = TotalValue / CostPerCredit;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                StrTotalValue = String.Format("{0:n2}", (DblTotalValue));
                textBox_poundpence.Text = StrTotalValue.ToString();
            }
            else
            {
                MessageBox.Show("You have not set the cost per credit!!");

            }
        }
        private void btn_20p_Click(object sender, EventArgs e)
        {

            if (CostPerCredit > 0)
            {
                CoinTwentyPence = CoinTwentyPence + 1;
                label_20p.Text = CoinTwentyPence.ToString();
                TotalValue = TotalValue + 20;
                textBox_pence.Text = TotalValue.ToString();
                NumberOfCredit = TotalValue / CostPerCredit;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                StrTotalValue = String.Format("{0:n2}", (DblTotalValue));
                textBox_poundpence.Text = StrTotalValue.ToString();
            }
            else
            {
                MessageBox.Show("You have not set the cost per credit!!");

            }
        }

        private void btn_50p_Click(object sender, EventArgs e)
        {

            if (CostPerCredit > 0)
            {
                CoinFiftyPence = CoinFiftyPence + 1;
                label_50p.Text = CoinTenPence.ToString();
                TotalValue = TotalValue + 50;
                textBox_pence.Text = TotalValue.ToString();
                NumberOfCredit = TotalValue / CostPerCredit;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                StrTotalValue = String.Format("{0:n2}", (DblTotalValue));
                textBox_poundpence.Text = StrTotalValue.ToString();
            }
            else
            {
                MessageBox.Show("You have not set the cost per credit!!");

            }
        }

        private void btn_1pound_Click(object sender, EventArgs e)
        {

            if (CostPerCredit > 0)
            {
                CoinOnePound = CoinOnePound + 1;
                label_1pound.Text = CoinOnePound.ToString();
                TotalValue = TotalValue + 100;
                textBox_pence.Text = TotalValue.ToString();
                NumberOfCredit = TotalValue / CostPerCredit;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                StrTotalValue = String.Format("{0:n2}", (DblTotalValue));
                textBox_poundpence.Text = StrTotalValue.ToString();
            }
            else
            {
                MessageBox.Show("You have not set the cost per credit!!");

            }
        }
        private void btn_2pound_Click(object sender, EventArgs e)
        {

            if (CostPerCredit > 0)
            {
                CoinTwoPound = CoinTwoPound + 1;
                label_2pound.Text = CoinTwoPound.ToString();
                TotalValue = TotalValue + 200;
                textBox_pence.Text = TotalValue.ToString();
                NumberOfCredit = TotalValue / CostPerCredit;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                StrTotalValue = String.Format("{0:n2}", (DblTotalValue));
                textBox_poundpence.Text = StrTotalValue.ToString();
            }
            else
            {
                MessageBox.Show("You have not set the cost per credit!!");

            }
        }

        private void textBox_CostPerCredit_TextChanged(object sender, EventArgs e)
        {
            int No;
            if (Int32.TryParse(textBox_CostPerCredit.Text, out No))
            {
                NumberOfCredit = 0;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                CostPerCredit = 0;
                CostPerCredit = Convert.ToInt16(textBox_yourcredits.Text);

                if (CostPerCredit > 0)
                {
                    NumberOfCredit = TotalValue / CostPerCredit;
                    textBox_yourcredits.Text = NumberOfCredit.ToString();
                    DblTotalValue = Convert.ToDouble(TotalValue) / 100;
                    StrTotalValue = String.Format("{0:n2}", (DblTotalValue));
                    textBox_poundpence.Text = StrTotalValue.ToString();

                }
                else
                {
                    NumberOfCredit = 0;
                    textBox_yourcredits.Text = NumberOfCredit.ToString();
                }
            }
            else
            {
                NumberOfCredit = 0;
                textBox_yourcredits.Text = NumberOfCredit.ToString();
                CostPerCredit = 0;
            } 
        }



            private void btn_reset_Click(object sender, EventArgs e)
        {
            TotalValue = 0;
            CostPerCredit = 0;
            NumberOfCredit = 0;
            CoinOnePence= 0;
            CoinTwoPence =0;
            CoinFivePence = 0;
            CoinTenPence = 0;
            CoinTwentyPence = 0;
            CoinFiftyPence = 0;
            CoinOnePound= 0;
            CoinTwoPound = 0;
            textBox_pence.Text = TotalValue.ToString();
            textBox_CostPerCredit.Text = CostPerCredit.ToString();
            label_1p.Text = CoinOnePence.ToString();
            label_2p.Text = CoinTwoPence.ToString();
            label_5p.Text = CoinFivePence.ToString();
            label_10p.Text = CoinTenPence.ToString();
            label_20p.Text = CoinTwentyPence.ToString();
            label_50p.Text = CoinFiftyPence.ToString();
            label_1pound.Text = CoinOnePound.ToString();
            label_2pound.Text = CoinTwoPound.ToString();
            textBox_CostPerCredit.Text = CostPerCredit.ToString();
            textBox_pence.Text = NumberOfCredit.ToString();
            DblTotalValue = Convert.ToDouble(TotalValue) / 100;
            StrTotalValue = String.Format("{0:n2}", (DblTotalValue));
            textBox_pence.Text = StrTotalValue.ToString();
          
            

         


            
       

        }

    

      

    }
}


Getting this error message (Attempted to divide by zero.)program freezes and crashes when I push a button..
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: If statement not working unsure what I've done wrong

Posted 10 December 2013 - 06:45 AM

If you run your code in the debugger, which line does the it point to when the attempted division by zero is detected?

Double check your conditions on line 44.
Was This Post Helpful? 1
  • +
  • -

#6 Pikey85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 15-November 13

Re: If statement not working unsure what I've done wrong

Posted 10 December 2013 - 07:31 AM

It points to line 44 - NumberOfCredit = TotalValue / CostPerCredit;
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 764
  • View blog
  • Posts: 3,334
  • Joined: 03-December 12

Re: If statement not working unsure what I've done wrong

Posted 10 December 2013 - 07:42 AM

View PostSkydiver, on 10 December 2013 - 08:45 AM, said:

Double check your conditions on line 44.


044             if (!int.TryParse(EnteredCostPerCredit, out CostPerCredit) || CostPerCredit <= 0)  


If CostPerCredit less than or equal to 0.
Was This Post Helpful? 1
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: If statement not working unsure what I've done wrong

Posted 10 December 2013 - 07:44 AM

View PostPikey85, on 10 December 2013 - 09:31 AM, said:

It points to line 44 - NumberOfCredit = TotalValue / CostPerCredit;


That's line 50.

View Postastonecipher, on 10 December 2013 - 09:42 AM, said:

View PostSkydiver, on 10 December 2013 - 08:45 AM, said:

Double check your conditions on line 44.


044             if (!int.TryParse(EnteredCostPerCredit, out CostPerCredit) || CostPerCredit <= 0)  


If CostPerCredit less than or equal to 0.


Or if the parse failed.
Was This Post Helpful? 0
  • +
  • -

#9 Pikey85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 15-November 13

Re: If statement not working unsure what I've done wrong

Posted 10 December 2013 - 07:47 AM

Thanks got it!! :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1