Martin Rosenquist
Martin Rosenquist

Reputation: 29

C# calculator exponet function. How?

I am currently working on a calculator for a school project. I need to have an exponent function, but can't figure out how. I'm getting this error:

Argument 1: cannot convert from 'double?' to 'double'

some of my code.

namespace Lommeregner
{
    public partial class Lommeregner : Form
    {
        double? firstpressednumber;
        double sum;
        double? secondpressednumber = null;
        string operationclicked;

        onscreenShower _onscreenShower = new onscreenShower();

My exponent:

private void btn_exponent_Click(object sender, EventArgs e)
        {
            screen_text.Text = _onscreenShower.DisplayOnScreen(Math.Exp(secondpressednumber).ToString(), screen_text.Text.ToString());
        }

My equal code.

private void btn_equal_Click(object sender, EventArgs e)
        {
            if(screen_text.ToString().Length>0)
            {
                switch(operationclicked)
                {
                    case "+":
                        secondpressednumber = double.Parse(screen_text.Text.ToString());
                        sum = double.Parse((firstpressednumber + secondpressednumber).ToString());
                        firstpressednumber = double.Parse(screen_text.Text.ToString());

                        screen_text.Text = sum.ToString();
                        break;

                    case "-":
                        secondpressednumber = double.Parse(screen_text.Text.ToString());
                        sum = double.Parse((firstpressednumber - secondpressednumber).ToString());
                        firstpressednumber = double.Parse(screen_text.Text.ToString());

                        screen_text.Text = sum.ToString();
                        break;

                    case "*":
                        secondpressednumber = double.Parse(screen_text.Text.ToString());
                        sum = double.Parse((firstpressednumber * secondpressednumber).ToString());
                        firstpressednumber = double.Parse(screen_text.Text.ToString());

                        screen_text.Text = sum.ToString();
                        break;

                    case "/":
                        secondpressednumber = double.Parse(screen_text.Text.ToString());
                        sum = double.Parse((firstpressednumber / secondpressednumber).ToString());
                        firstpressednumber = double.Parse(screen_text.Text.ToString());

                        screen_text.Text = sum.ToString();
                        break;
                }
            }
        }

Hope you guys can help.

Upvotes: 0

Views: 137

Answers (2)

Master Azazel
Master Azazel

Reputation: 600

Your double is nullable

If you want to access its value, try doubleVarName.Value

You can check if it has a value using doubleVarName.HasValue then pass the value to your method if true

Upvotes: 2

Jamiec
Jamiec

Reputation: 136174

Math.Exp expects a double as the parameter - you're (trying to) pass it a nullable double (double?).

At minimum you want the .Value, but you almost certainly should be checking a value exists first:

if(!secondpressednumber.HasValue)
   throw new InvalidOperationException("No value");
screen_text.Text = _onscreenShower.DisplayOnScreen(Math.Exp(secondpressednumber.Value).ToString(), screen_text.Text.ToString());

Upvotes: 2

Related Questions