user770022
user770022

Reputation: 2959

Error 1 Operator '*' cannot be applied to operands of type 'method group' and 'double'

I believe what i am trying to do is very simple but I get the error. Operator '*' cannot be applied to operands of type 'method group' and 'double'

I want to multiply the number of days that have been stored in dayrental by the amount 19.95 if that checkbox is checked. I get the error that says I cant because dayrental is a method. How can I get the value from dayrental so that I can multiple by 19.95?

private void button1_Click(object sender, EventArgs e)
    {
        double rental;
        dayrental();
        if (checkBox1.Checked == true)
            rental = dayrental * 19.95;
        label4.Text = Convert.ToString(rental);




    }

    private void label4_Click(object sender, EventArgs e)
    {

    }
    public void dayrental()
    {
        var timeSpan = dateTimePicker2.Value - dateTimePicker1.Value;
        var rentalDays = timeSpan.Days;                      
        //label4.Text = Convert.ToString(rentalDays);   
    }

Upvotes: 1

Views: 2703

Answers (5)

Grizzly
Grizzly

Reputation: 20191

In c# a methodcall is always denoted by a pair of braces, while the method itself is addressed by it's name. So dayrental() is the returnvalue of dayrental, while dayrental is the method dayrental. Therefore you are multiplying the method dayrental with the with 19.95 which obviously fails. What you are trying to do is:

rental = dayrental() * 19.95;

Also dayrental returns void so youm ight want to change it to

 public double dayrental()

and return some value.

Upvotes: 0

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102408

Do this:

private void button1_Click(object sender, EventArgs e)
{
    double rental;

    var dayRental = dayrental();

    if(checkBox1.Checked == true)
       rental = dayrental * 19.95;

    label4.Text = Convert.ToString(rental);  
}

private void label4_Click(object sender, EventArgs e)
{

}

public int dayrental()
{
     var timeSpan = dateTimePicker2.Value - dateTimePicker1.Value;

     var rentalDays = timeSpan.Days;                      

     return rentalDays;
}

Upvotes: 0

Steve Whitfield
Steve Whitfield

Reputation: 2029

You are missing the paranthesis from your function call to dayrental, which is causing the compiler to think you are refering to the method itself rather than the result of a call to that method.

rental = dayrental * 19.95;

should be

rental = dayrental() * 19.95;

Upvotes: 0

Itay Karo
Itay Karo

Reputation: 18286

dayrental is a function that return void.

it has no value and you cannot multiply it by 19

Upvotes: 2

Mark Synowiec
Mark Synowiec

Reputation: 5445

your syntax is just a little off (missing parentheses after function call and no return type specified for your function).

if (checkBox1.Checked == true)
    rental = dayrental() * 19.95;

public double dayrental()
{
    var timeSpan = dateTimePicker2.Value - dateTimePicker1.Value;
    return (double) timeSpan.Days;
}

Upvotes: 4

Related Questions