7 Replies - 2098 Views - Last Post: 17 October 2010 - 01:39 PM Rate Topic: -----

#1 8100 Power  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 10-February 09

C# If Statement Help

Posted 17 October 2010 - 01:00 PM

I've been practicing with C# as I have a class with it. I'm making this program to help me price my side hobby(business)..

I'm trying to figure out why my "if" statements are being skipped over? Basically I set my pricing by a user selection in a combo box and it sets the price to calculate materials on.

I set up a few textboxs to output my varibles and my "userVinylCost" is being skipped over.

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // Form Varibles
        Form2 f2 = new Form2();
       

        // Price Varibles
        double markupRate = 0;
        double materialCost = 0; // Total Cost of App & Vinyl Material (not including markup)
        double vinylCost = 0; 
        double tapeCost = 0;
        double salePrice = 0;

        //User Input Varibles  
        double userVinylCost;
        double userAppCost = 0.0006;
        string vinylType = "TR";
        
        public Form1()
        {
            InitializeComponent();
        }

        public void btnCalculate_Click(object sender, EventArgs e)
        {
            
            
            VinylMaterialPricer();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //txtSpecifyBox.Visible = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            txtLength.Clear();
            txtWidth.Clear();
        }

        public void VinylMaterialPricer()
        {
            
            if (cmbMatSelect.SelectedIndex ==  1)
                userVinylCost = 0.0030;
                lblAppSetOutput.Text = "Oracal 651 Selected @ $25.00 \nfor 24 in X 10 yrd";
                vinylType = "651";
            
            if (cmbMatSelect.SelectedText == "751")
                userVinylCost = 0.0060;
                lblAppSetOutput.Text = "Oracal 751 Selected @ $50.00 \nfor 24 in X 10 yrd";
                vinylType = "751";
            
            if (cmbMatSelect.SelectedText == "951")
                userVinylCost = 0.0090;
                lblAppSetOutput.Text = "Oracal 951 Selected @ $??.00 \nfor 24 in X 10 yrd";
                vinylType = "951";
            
            if (cmbMatSelect.SelectedText == "OTHER")
                vinylType = "Custom";
                //txtSpecifyBox.Visible = true;

            
            


            double widthValue = Convert.ToDouble(txtWidth.Text);
            double lengthValue = Convert.ToDouble(txtLength.Text);
            double sqinch = widthValue * lengthValue; // Total Sq Inches


            

            if (rbRtaVinyl.Checked == true)
                markupRate = 19.0;
            if (rbMagnets.Checked == true)
                markupRate = 39.0;
            if (rbBanners.Checked == true)
                markupRate = 59.0;

            vinylCost = sqinch * userVinylCost;  // Calucate Vinyl Per Sq In based on $25.00 Roll
            tapeCost = sqinch * userAppCost; // Calculate App Tape Per Sq in base on $24.00/12" x 100yd roll
            materialCost = vinylCost + tapeCost;
            salePrice = (materialCost * markupRate) + materialCost;
            textBox1.Text = Convert.ToString(vinylCost);
            

            listBox1.Items.Add(String.Format("{0,-5} {1,8}  {2,8} {3,8} ","Type", "Sq In ", "Material Cost ", "Sale Price"));
            listBox1.Items.Add(String.Format("{0,-5} {1,8}  {2,14:c2} {3,11:c2} ", vinylType, sqinch, materialCost, salePrice));
        }

        

       

        private void userSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            f2.Show();
           
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
                     
    }
}



Thanks in advance for your help.

This post has been edited by 8100 Power: 17 October 2010 - 01:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C# If Statement Help

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: C# If Statement Help

Posted 17 October 2010 - 01:05 PM

Your not putting you if-statement's code in { }s.

Try this:
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // Form Varibles
        Form2 f2 = new Form2();
       

        // Price Varibles
        double markupRate = 0;
        double materialCost = 0; // Total Cost of App & Vinyl Material (not including markup)
        double vinylCost = 0; 
        double tapeCost = 0;
        double salePrice = 0;

        //User Input Varibles  
        double userVinylCost;
        double userAppCost = 0.0006;
        string vinylType = "TR";
        
        public Form1()
        {
            InitializeComponent();
        }

        public void btnCalculate_Click(object sender, EventArgs e)
        {
            
            
            VinylMaterialPricer();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //txtSpecifyBox.Visible = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            txtLength.Clear();
            txtWidth.Clear();
        }

        public void VinylMaterialPricer()
        {
            
            if (cmbMatSelect.SelectedIndex ==  1)
             {
                userVinylCost = 0.0030;
                lblAppSetOutput.Text = "Oracal 651 Selected @ $25.00 \nfor 24 in X 10 yrd";
                vinylType = "651";
             }
            if (cmbMatSelect.SelectedText == "751")
             {
                userVinylCost = 0.0060;
                lblAppSetOutput.Text = "Oracal 751 Selected @ $50.00 \nfor 24 in X 10 yrd";
                vinylType = "751";
             }
            if (cmbMatSelect.SelectedText == "951")
             {
                userVinylCost = 0.0090;
                lblAppSetOutput.Text = "Oracal 951 Selected @ $??.00 \nfor 24 in X 10 yrd";
                vinylType = "951";
             }
            if (cmbMatSelect.SelectedText == "OTHER")
             {
                vinylType = "Custom";
                //txtSpecifyBox.Visible = true;
             }
            
            


            double widthValue = Convert.ToDouble(txtWidth.Text);
            double lengthValue = Convert.ToDouble(txtLength.Text);
            double sqinch = widthValue * lengthValue; // Total Sq Inches


            

            if (rbRtaVinyl.Checked == true)
                markupRate = 19.0;
            if (rbMagnets.Checked == true)
                markupRate = 39.0;
            if (rbBanners.Checked == true)
                markupRate = 59.0;

            vinylCost = sqinch * userVinylCost;  // Calucate Vinyl Per Sq In based on $25.00 Roll
            tapeCost = sqinch * userAppCost; // Calculate App Tape Per Sq in base on $24.00/12" x 100yd roll
            materialCost = vinylCost + tapeCost;
            salePrice = (materialCost * markupRate) + materialCost;
            textBox1.Text = Convert.ToString(vinylCost);
            

            listBox1.Items.Add(String.Format("{0,-5} {1,8}  {2,8} {3,8} ","Type", "Sq In ", "Material Cost ", "Sale Price"));
            listBox1.Items.Add(String.Format("{0,-5} {1,8}  {2,14:c2} {3,11:c2} ", vinylType, sqinch, materialCost, salePrice));
        }

        

       

        private void userSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            f2.Show();
           
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
                     
    }
}


A if statement without braces is only valid if only one line is being executed like this:
if (rbBanners.Checked == true)
                markupRate = 59.0;


This post has been edited by Amrykid: 17 October 2010 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: C# If Statement Help

Posted 17 October 2010 - 01:05 PM

They're not being skipped. YOu just didn't form them right. You forgot the braces around the block of code that should be executed if the condition is met.


if (cmbMatSelect.SelectedIndex ==  1)
{
                userVinylCost = 0.0030;
                lblAppSetOutput.Text = "Oracal 651 Selected @ $25.00 \nfor 24 in X 10 yrd";
                vinylType = "651";
}

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: C# If Statement Help

Posted 17 October 2010 - 01:15 PM

Hard coded all your values like that is going to be rough to maintain.
I would suggest you make a class for your vinyltype


public class VinylType
{
   public string FriendlyName
    {
       get; set;
    }
   
   public decimal PricePerYard
    {
       get; set;
    }

   public string override ToString()
   {
     return FriendlyName;
     // This will then show up in  your combobox
   ]

}


When you fill your combobox you actually send VinylType objects to the .Items collection. Not just strings, but the actual object.

Then that ugliness starting at line 63 looks more like this with no if statement at all.


vinylType = cmbMatSelect.SelectedItem;
lblAppSetOutput.Text = vinylType.ToString();
UserVinylCost = vinylType.PricePerYard;


To learn more about using classes and inheritance etc. you might like this tutorial.
Bulding an application - Part 1
Was This Post Helpful? 1
  • +
  • -

#5 8100 Power  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 10-February 09

Re: C# If Statement Help

Posted 17 October 2010 - 01:16 PM

I tried teh braces also...
It's still not executing correctly. When I select "751" in the combo box for example, it's resorting to the vinylCost that is initialized at the top as "OUT". It's not picking them up inside the if blocks.

Here is my latest 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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // Form Varibles
        Form2 f2 = new Form2();
       

        // Price Varibles
        double markupRate = 0;
        double materialCost = 0; // Total Cost of App & Vinyl Material (not including markup)
        double vinylCost = 0; 
        double tapeCost = 0;
        double salePrice = 0;

        //User Input Varibles  
        double userVinylCost;
        double userAppCost = 0.0006;
        string vinylType = "OUT";
        
        public Form1()
        {
            InitializeComponent();
        }

        public void btnCalculate_Click(object sender, EventArgs e)
        {
            
            
            VinylMaterialPricer();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //txtSpecifyBox.Visible = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            txtLength.Clear();
            txtWidth.Clear();
        }

        public void VinylMaterialPricer()
        {

            if (cmbMatSelect.SelectedText == "651")
            {
                userVinylCost = 0.0030;
                lblAppSetOutput.Text = "Oracal 651 Selected @ $25.00 \nfor 24 in X 10 yrd";
                vinylType = "651";
            }
            if (cmbMatSelect.SelectedText == "751")
            {
                userVinylCost = 0.0060;
                lblAppSetOutput.Text = "Oracal 751 Selected @ $50.00 \nfor 24 in X 10 yrd";
                vinylType = "751";
            }
            if (cmbMatSelect.SelectedText == "951")
            {
                userVinylCost = 0.0090;
                lblAppSetOutput.Text = "Oracal 951 Selected @ $??.00 \nfor 24 in X 10 yrd";
                vinylType = "951";
            }
            if (cmbMatSelect.SelectedText == "OTHER")
            {
                vinylType = "Custom";
                //txtSpecifyBox.Visible = true;
            }
            
            


            double widthValue = Convert.ToDouble(txtWidth.Text);
            double lengthValue = Convert.ToDouble(txtLength.Text);
            double sqinch = widthValue * lengthValue; // Total Sq Inches


            

            if (rbRtaVinyl.Checked == true)
                markupRate = 19.0;
            if (rbMagnets.Checked == true)
                markupRate = 39.0;
            if (rbBanners.Checked == true)
                markupRate = 59.0;

            vinylCost = sqinch * userVinylCost;  // Calucate Vinyl Per Sq In based on $25.00 Roll
            tapeCost = sqinch * userAppCost; // Calculate App Tape Per Sq in base on $24.00/12" x 100yd roll
            materialCost = vinylCost + tapeCost;
            salePrice = (materialCost * markupRate) + materialCost;
            textBox1.Text = Convert.ToString(vinylCost);
            

            listBox1.Items.Add(String.Format("{0,-5} {1,8}  {2,8} {3,8} ","Type", "Sq In ", "Material Cost ", "Sale Price"));
            listBox1.Items.Add(String.Format("{0,-5} {1,8}  {2,14:c2} {3,11:c2} ", vinylType, sqinch, materialCost, salePrice));
        }

        

       

        private void userSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            f2.Show();
           
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
                     
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: C# If Statement Help

Posted 17 October 2010 - 01:22 PM

Hello there,

I think your problem may be your use of the SelectedText property. Try checking the SelectedItem property instead. This property returns an object, so you will have to convert it to a string before checking it in the if statement. An example would be this:

 if ((cmbMatSelect.SelectedItem.ToString()) == "751")
            {
                userVinylCost = 0.0060;
                lblAppSetOutput.Text = "Oracal 751 Selected @ $50.00 \nfor 24 in X 10 yrd";
                vinylType = "751";
            }

This post has been edited by CodingSup3rnatur@l-360: 17 October 2010 - 01:24 PM

Was This Post Helpful? 1
  • +
  • -

#7 8100 Power  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 10-February 09

Re: C# If Statement Help

Posted 17 October 2010 - 01:32 PM

Coding Supernatural. That worked! Thanks

tlhIn'toq - Thanks for you input. I will definitely go through your tutorial and see if I can improve my coding skills. I just started C# in August, just trying to learn. Thanks!
Was This Post Helpful? 0
  • +
  • -

#8 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: C# If Statement Help

Posted 17 October 2010 - 01:39 PM

View Post8100 Power, on 17 October 2010 - 12:32 PM, said:

Coding Supernatural. That worked! Thanks

tlhIn'toq - Thanks for you input. I will definitely go through your tutorial and see if I can improve my coding skills. I just started C# in August, just trying to learn. Thanks!


No problem :)!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1