Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. overload operator +

overload operator +

Scheduled Pinned Locked Moved C#
questionhelp
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    al3xutzu00
    wrote on last edited by
    #1

    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.”

    D 0 2 Replies Last reply
    0
    • A al3xutzu00

      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.”

      D Offline
      D Offline
      Dino Mulahusic
      wrote on last edited by
      #2

      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);
      }

      1 Reply Last reply
      0
      • A al3xutzu00

        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.”

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #3

        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)

        A 1 Reply Last reply
        0
        • 0 0x3c0

          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)

          A Offline
          A Offline
          al3xutzu00
          wrote on last edited by
          #4

          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.”

          0 L 2 Replies Last reply
          0
          • A al3xutzu00

            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.”

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #5

            You're very welcome. And thank you for working the code out - too many people here expect to be spoon-fed

            1 Reply Last reply
            0
            • A al3xutzu00

              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.”

              L Offline
              L Offline
              Luc 648011
              wrote on last edited by
              #6

              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. :)

              OriginalGriffO 1 Reply Last reply
              0
              • L Luc 648011

                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. :)

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                D 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  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;
                  ...

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  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 new Apple 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)

                  OriginalGriffO 1 Reply Last reply
                  0
                  • D DaveyM69

                    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 new Apple 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)

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    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...

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups