Reputation: 1726
I need to count all checked checkboxes and I was meaning to use operator overloading as this example :
public static int operator +(bool b1, bool b2)
{
int i1 = 0;
int i2 = 0;
if (b1) i1 = 1;
if (b2) i2 = 1;
return i1 + i2;
}
And then the total would be simply retrieved by
int countCbx = cbx1.Checked + cbx2.Checked + ...
But it don't work and I don't see why. The compilation error is "One of the parameters of a binary operator must be the containing type". The logic seems good, but it's the first time I use operator overload outside example.
Thanks for your help.
Upvotes: 0
Views: 329
Reputation: 1502066
But it don't work and I don't see why.
Because the language specification says so. The compiler is basically giving you the reason.
The logic seems good, but it's the first time I use operator overload outside example.
The logic of the body of the member is fine - but you can't overload operators for arbitrary types. At least one of the operands (or the return type for conversions) has to be the type that you're declaring the operator in.
The C# 5 specification contains this in section 10.10:
Like other members, operators declared in a base class are inherited by derived classes. Because operator declarations always require the class or struct in which the operator is declared to participate in the signature of the operator, it is not possible for an operator declared in a derived class to hide an operator declared in a base class. Thus, the new modifier is never required, and therefore never permitted, in an operator declaration.
That rule is then enforced in 10.10.1:
The following rules apply to unary operator declarations, where T denotes the instance type of the class or struct that contains the operator declaration:
- A unary +, -, !, or ~ operator must take a single parameter of type T or T? and can return any type.
- ...
.... 10.10.2:
The following rules apply to binary operator declarations, where T denotes the instance type of the class or struct that contains the operator declaration:
- A binary non-shift operator must take two parameters, at least one of which must have type T or T?, and can return any type.
- A binary
<<
or>>
operator must take two parameters, the first of which must have type T or T? and the second of which must have type int or int?, and can return any type.
... and 10.10.3:
For a given source type S and target type T, if S or T are nullable types, let S0 and T0 refer to their underlying types, otherwise S0 and T0 are equal to S and T respectively. A class or struct is permitted to declare a conversion from a source type S to a target type T only if all of the following are true:
- S0 and T0 are different types.
- Either S0 or T0 is the class or struct type in which the operator declaration takes place.
- ...
Upvotes: 6
Reputation: 437544
You cannot do that simply because the language does not allow it. The addition operator cannot be overloaded to work with bool
values, and since bool
is not implicitly convertible to any arithmetic type (as it is for example in C++) you are out of options.
If you want to count the number of checked boxes, you can for example use
var count = (new[] { cbx1, cbx2, ... }).Count(cb => cb.Checked);
Upvotes: 4
Reputation: 50144
The compilation error tells you exactly what's wrong.
"One of the parameters of a binary operator must be the containing type"
You can't overload an operator except inside the definition of the type (or one of the types) you're overloading it for.
May I suggest
int countCbx = new[] { cbx1, cbx2, ... }.Count(c => c.Checked);
Upvotes: 2