overload operator +
-
Hi guys, Quick question: I got this class:
public class PresaHidraulica {
.....public System.Collections.ArrayList listaRevizii;
}
and i want to overload the operator + to add an object of another class(Revizie) in this ArrayList from this class. I tried :public static PresaHidraulica operator +(Revizie r1) { }
And i get the error : The parameter of a unary operator must be the containing type Any suggestions please? Regards, Alex“Be the change you want to see in the world.”
-
Hi guys, Quick question: I got this class:
public class PresaHidraulica {
.....public System.Collections.ArrayList listaRevizii;
}
and i want to overload the operator + to add an object of another class(Revizie) in this ArrayList from this class. I tried :public static PresaHidraulica operator +(Revizie r1) { }
And i get the error : The parameter of a unary operator must be the containing type Any suggestions please? Regards, Alex“Be the change you want to see in the world.”
That means that your parameter r1 is of wrong type: Revizie. When you overload operator + you must pass in parameter PresaHidraulica
public static PresaHidraulica operator +(PresaHidraulica ph)
{
}Maybe you should create a method called AddRevizie
public void AddRevizie(Revizie r1)
{
listaRevizii.Add(r1);
} -
Hi guys, Quick question: I got this class:
public class PresaHidraulica {
.....public System.Collections.ArrayList listaRevizii;
}
and i want to overload the operator + to add an object of another class(Revizie) in this ArrayList from this class. I tried :public static PresaHidraulica operator +(Revizie r1) { }
And i get the error : The parameter of a unary operator must be the containing type Any suggestions please? Regards, Alex“Be the change you want to see in the world.”
-
The first parameter must be the containing type. In this instance, you should change the operator method signature to
public static PresaHidraulica operator +(PresaHidraulica self, Revizie r1)
Thanks a lot. It worked just fine:D here it is:
public static PresaHidraulica operator +(PresaHidraulica p, Revizie r) { p.ListaRevizii.Add(r); return p; }
Regards, Alex“Be the change you want to see in the world.”
-
Thanks a lot. It worked just fine:D here it is:
public static PresaHidraulica operator +(PresaHidraulica p, Revizie r) { p.ListaRevizii.Add(r); return p; }
Regards, Alex“Be the change you want to see in the world.”
-
Thanks a lot. It worked just fine:D here it is:
public static PresaHidraulica operator +(PresaHidraulica p, Revizie r) { p.ListaRevizii.Add(r); return p; }
Regards, Alex“Be the change you want to see in the world.”
Hi, this is a bad idea. When you have integers a, b, c and execute
c = a + b;
do you expect a or b to change? That is what your code is doing! You should not change the value of the operands, Overloaded operators are not the right solution, create an Add() method instead. :) -
Hi, this is a bad idea. When you have integers a, b, c and execute
c = a + b;
do you expect a or b to change? That is what your code is doing! You should not change the value of the operands, Overloaded operators are not the right solution, create an Add() method instead. :)I totally agree. What you are trying to do is also adding apples and oranges:
foo = 3.1415 + "Hello";
Use an Add method, rather than trying to twist the compiler into effectively allowing implicit casts between unrelated objects. That didn't make as much sense when I read it back. Try this:
...
int a, c;
string b;
...
c = a + b;
...You would not expect that to compile, would you? So why would you expect this to compile?
class AClass
{
private static AClass operator + (BClass b)
...
}
class BClass
{
...
}
...
AClass a, c;
BClass b;
...
c = a + b;
... -
I totally agree. What you are trying to do is also adding apples and oranges:
foo = 3.1415 + "Hello";
Use an Add method, rather than trying to twist the compiler into effectively allowing implicit casts between unrelated objects. That didn't make as much sense when I read it back. Try this:
...
int a, c;
string b;
...
c = a + b;
...You would not expect that to compile, would you? So why would you expect this to compile?
class AClass
{
private static AClass operator + (BClass b)
...
}
class BClass
{
...
}
...
AClass a, c;
BClass b;
...
c = a + b;
...I agree to a point. There are situations where it could be acceptable (the OP's may be one). To continue your adding apples and oranges analogy, imagine a class
FruitBasket
that has a List of Apples. Adding a newApple
to the basket via an overloaded + binary operator could make sense. I prefer exposing the list of Apples as a property so it's Add method can be called directly or creating an AddApple method, but it's not 'bad' IMO.public class FruitBasket
{
private List<Apple> _Apples;
public FruitBasket()
{
_Apples = new List<Apple>();
}
public static FruitBasket operator +(FruitBasket basket, Apple apple)
{
basket._Apples.Add(apple);
return basket;
}
}
public class Apple
{
public Apple(string name)
{
Name = name;
}
public string Name
{
get;
set;
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I agree to a point. There are situations where it could be acceptable (the OP's may be one). To continue your adding apples and oranges analogy, imagine a class
FruitBasket
that has a List of Apples. Adding a newApple
to the basket via an overloaded + binary operator could make sense. I prefer exposing the list of Apples as a property so it's Add method can be called directly or creating an AddApple method, but it's not 'bad' IMO.public class FruitBasket
{
private List<Apple> _Apples;
public FruitBasket()
{
_Apples = new List<Apple>();
}
public static FruitBasket operator +(FruitBasket basket, Apple apple)
{
basket._Apples.Add(apple);
return basket;
}
}
public class Apple
{
public Apple(string name)
{
Name = name;
}
public string Name
{
get;
set;
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)I see what you are saying, that there may be instances where it could be acceptable. I am not sure though, that it adds anything usefull over using an Add method - at least you expect
fruitBasket.Add(apple);
to change fruitBasket without looking at the implementation. I wouldn't expect
foo = fruitbasket + apple;
to result in a change in foo and fruitBasket at first view. Surely, good practice would say "don't do it"? Just a thought, not an arguement...