C# Method Issue

Hi,

When I click a button, I need to take the user entered wholesale price and custom percentage and pass them to a method as arguments and then return the value to a label.

Where did I mess up?

      private double CalculateRetail(double wholesale, double percentage)
        {
            return wholesale * percentage;
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            double rp, retailPrice, wholesale, percentage;
            if (double.TryParse(wholesaleTextBox.Text, out wholesale))
            {
                if (double.TryParse(percentageTextBox.Text, out percentage))
                {
                    retailPrice = CalculateRetail(wholesale);

                    resultLabel.Text = rp.ToString("n1");
                }
                else
                {
                    MessageBox.Show("Enter a Valid Price");
                }
            }
        }


    }
}

    

Open in new window

LVL 3
Computer GuyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Try this (quick and dirty):

Form1.cs -
using System;
using System.Windows.Forms;

namespace EE_Q28645837
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void OnClick(object sender, EventArgs e)
		{
			double wholesale, percentage;
			if (double.TryParse(tbPrice.Text, out wholesale))
			{
				if (double.TryParse(tbPercent.Text, out percentage))
					lblResults.Text = CalculateRetail(wholesale, percentage).ToString();
				else
					MessageBox.Show("Enter a valid percentage!");
			}
			else
				MessageBox.Show("Enter a valid price!");
		}

		private double CalculateRetail(double wholesale, double percentage)
		{
			return wholesale * percentage;
		}
	}
}

Open in new window

Form1.Designer.cs -
namespace EE_Q28645837
{
	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.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.tbPrice = new System.Windows.Forms.TextBox();
			this.tbPercent = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.lblResults = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(197, 65);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 23);
			this.button1.TabIndex = 0;
			this.button1.Text = "Calculate";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.OnClick);
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(12, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(87, 13);
			this.label1.TabIndex = 1;
			this.label1.Text = "Wholesale Price:";
			// 
			// tbPrice
			// 
			this.tbPrice.Location = new System.Drawing.Point(105, 13);
			this.tbPrice.Name = "tbPrice";
			this.tbPrice.Size = new System.Drawing.Size(167, 20);
			this.tbPrice.TabIndex = 2;
			// 
			// tbPercent
			// 
			this.tbPercent.Location = new System.Drawing.Point(105, 39);
			this.tbPercent.Name = "tbPercent";
			this.tbPercent.Size = new System.Drawing.Size(167, 20);
			this.tbPercent.TabIndex = 4;
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(34, 42);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(65, 13);
			this.label2.TabIndex = 3;
			this.label2.Text = "Percentage:";
			// 
			// lblResults
			// 
			this.lblResults.AutoSize = true;
			this.lblResults.Location = new System.Drawing.Point(236, 95);
			this.lblResults.Name = "lblResults";
			this.lblResults.Size = new System.Drawing.Size(0, 13);
			this.lblResults.TabIndex = 5;
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(155, 95);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(45, 13);
			this.label4.TabIndex = 6;
			this.label4.Text = "Results:";
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(284, 264);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.lblResults);
			this.Controls.Add(this.tbPercent);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.tbPrice);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox tbPrice;
		private System.Windows.Forms.TextBox tbPercent;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label lblResults;
		private System.Windows.Forms.Label label4;
	}
}

Open in new window

Produces the following results -Capture.JPG-saige-
0
käµfm³d 👽Commented:
In line 13 you're only passing one value, but your method expects 2 values.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

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.