LewisDavie
LewisDavie

Reputation: 75

The best overloaded method match has some invalid arguments for a class constructor (C#)

I'm getting this error and not entirely sure why. In one class I create an object with the following line of code:

StoreSale sale = new StoreSale(1, 13.99);

The constructor inside the StoreSale class looks like this:

public StoreSale(int quantity, float value)
{
   this.quantity = quantity;
   this.value = value;
}

...and I'm getting the error 'The best overloaded method match for 'applicationname'.StoreSale.StoreSale(int, float) has some invalid arguments.'

Could anybody advise me as to what I'm doing wrong?

Upvotes: 1

Views: 2575

Answers (3)

Jakub Lortz
Jakub Lortz

Reputation: 14894

You get this error because the literal 13.99 is a double, and there is no implicit conversion from double to float. Use 13.99F instead.

StoreSale sale = new StoreSale(1, 13.99F);

Upvotes: 3

Joey
Joey

Reputation: 354714

13.99 is a double literal. Append an f to make it a float: 13.99f.

In any case, you probably don't want to use binary floating-point for monetary values anyway. decimal is a far saner choice.

Upvotes: 12

clcto
clcto

Reputation: 9648

13.99 is a double. To make it a float literal add an f: 13.99f.

Upvotes: 4

Related Questions