2
Answers

comboBox select form

Photo of mileswoolgar

mileswoolgar

19y
1.9k
1

Hi

As a newbie I'm trying to learn to use methods... and I'm a bit stuck .  This is what I'm trying to achieve [I'm expanding an exercise from the internet]:

1. select one of 3 countries from a comboBox [cboCountry] - show selected country in cboCountry

2. enter amount of cash to do calculation on [txtAmount]

3. depending on which country is selected will depend on the FxRate - which will show in the textBox txtFxRate

4. txtSubTotal (taxableAmount) should show figure entered in txtAmount * figure entered in txtFxRate [as passed by the cboCountry]

5. txtCashierFee should show 2% on the subTotal [cashierFee of 2% on the taxableAmount]

6. txtGrandTotal then shows subTotal + cashierFee
 

These are the two errors I get:

C:\BEGIN2\code\Lesson4x\ForEx\Form1.cs(265): 'displayResult' denotes a 'variable' where a 'method' was expected

C:\BEGIN2\code\Lesson4x\ForEx\Form1.cs(236): The name 'fxRate' does not exist in the class or namespace 'ForEx.Form1'

Many thanks for your help in advance

My code:

 

private void btnDollar_Click(object sender, System.EventArgs e)

{

#region select

switch(cboCountry.Text)

{

case "USA":

{

fxRate = 1.74743;

break;

}

case "PHIL":

{

fxRate = 89.4507;

break;

}

case "SAU":

{

fxRate = 6.55381;

break;

}

}

#endregion

double fxRate;

double taxableAmount; //subtotal

double cashierFee; // 2%

double grandTotal;

double displayResult;

txtFxRate.Text = cboCountry.Text.ToString();

txtTaxableAmount.Text = taxableAmount.ToString();

taxableAmount = calculateTaxableAmount(taxableAmount, fxRate);

cashierFee = calculateCashierFee(taxableAmount);

grandTotal = calculateGrandTotal(taxableAmount, cashierFee);

displayResult = displayResult(grandTotal);

}

#region private methods

private double calculateTaxableAmount(double taxableAmount, double fxRate)

{

return taxableAmount * fxRate;

}

private double calculateCashierFee(double taxableAmount)

{

return taxableAmount * 0.02;

}

private double calculateGrandTotal( double taxableAmount, double cashierFee)

{

return taxableAmount + cashierFee;

}

private void displayResult(double grandTotal)

{

MessageBox.Show("grand total = " + String.Format("0.C)", grandTotal));

}

#endregion

Answers (2)

0
mileswoolgar
NA 3 0 19y
Many thanks Ahmad, but I now get new errors - sorry please be patient.

I changed this: displayResult = displayResult(grandTotal); to this: showResult = displayResult(grandTotal);

I now get a conversion error on this line of code - Cannot implicitly convert type 'void' to 'double'
 
To solve this I tried - showResult = displayResult(grandTotal.ToString());

...But then I got an overload error -
The best overloaded method match for 'ForEx.Form1.displayResult(double)' has some invalid arguments

Any suggestions... anybody?

Many Thanks.

My code now looks like this>>

private void btnDollar_Click(object sender, System.EventArgs e)

 

 

{

#region select

double fxRate;

switch(cboCountry.Text)

{

case "USA":

{

fxRate = 1.74743;

break;

}

case "PHIL":

{

fxRate = 89.4507;

break;

}

case "SAU":

{

fxRate = 6.55381;

break;

}

}

#endregion

 

 

 

double taxableAmount;
double cashierFee;

double grandTotal;

double showResult;

 

 

txtFxRate.Text = cboCountry.Text.ToString();

 

txtTaxableAmount.Text = taxableAmount.ToString();

 

taxableAmount = calculateTaxableAmount(taxableAmount, fxRate);

cashierFee = calculateCashierFee(taxableAmount);

grandTotal = calculateGrandTotal(taxableAmount, cashierFee);

 

// showResult = displayResult(grandTotal.ToString());

showResult = displayResult(grandTotal);

}

#region private methods

private double calculateTaxableAmount(double taxableAmount, double fxRate)

{

return taxableAmount * fxRate;

}

private double calculateCashierFee(double taxableAmount)

{

return taxableAmount * 0.02;

}

private double calculateGrandTotal( double taxableAmount, double cashierFee)

{

return taxableAmount + cashierFee;

}

 

 

private void displayResult(double grandTotal)

{

MessageBox.Show("grand total = " + String.Format("0.C)", grandTotal));

 

}

#endregion

0
Ahmad Shaban
NA 6 0 19y

Hi

your problem seem to be

C:\BEGIN2\code\Lesson4x\ForEx\Form1.cs(236): The name 'fxRate' does not exist in the class or namespace 'ForEx.Form1'

why ?? cause you must declare the variable before using it !!

------------------------------------------

switch(cboCountry.Text)

{

case "USA":

{

fxRate = 1.74743;

break;

}

case "PHIL":

{

fxRate = 89.4507;

break;

}

case "SAU":

{

fxRate = 6.55381;

break;

}

}

#endregion

double fxRate; //<------ must go before the switch

you should move

double fxRate;

before the switch

C:\BEGIN2\code\Lesson4x\ForEx\Form1.cs(265): 'displayResult' denotes a 'variable' where a 'method' was expected

---------------------------------

double displayResult;

txtFxRate.Text = cboCountry.Text.ToString();

txtTaxableAmount.Text = taxableAmount.ToString();

taxableAmount = calculateTaxableAmount(taxableAmount, fxRate);

cashierFee = calculateCashierFee(taxableAmount);

grandTotal = calculateGrandTotal(taxableAmount, cashierFee);

displayResult = displayResult(grandTotal);

------------

private void displayResult(double grandTotal)

{

MessageBox.Show("grand total = " + String.Format("0.C)", grandTotal));

}

YOU CAN'T HAVE SAME NAME IN ONE SCOPE

HERE YOU HAVE

double displayResult; // THIS IS VARIABLE

and a method has the same name which is not accepted change the name of the var or the method