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. How to override the = operator?

How to override the = operator?

Scheduled Pinned Locked Moved C#
tutorialquestion
15 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 atoi_powered

    I made a class for Complex Numbers. I don't know what to write in the body of my override function.

    public ComplexNumber(int real, int imagine)
    {
    //Real and Imagine are attributes.
    Real = real;
    Imagine = imagine;
    }

    public static ComplexNumber operator =(ComplexNumber a, ComplexNumber b)
    {
    return new /* ?????????? */;
    }

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #6

    Are you trying to assign a to b here?

    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

    A 1 Reply Last reply
    0
    • P Pete OHanlon

      Are you trying to assign a to b here?

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      A Offline
      A Offline
      atoi_powered
      wrote on last edited by
      #7

      Yes, I want to do that but I've got that there is not such a possibility for the "=" operator, according to what Dave was trying to tell me.

      P 1 Reply Last reply
      0
      • A atoi_powered

        Yes, I want to do that but I've got that there is not such a possibility for the "=" operator, according to what Dave was trying to tell me.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #8

        I can't see much sense in trying to assign a ComplexNumber from two others like this. Do you really mean it to be the summation of the two complex numbers to form the new one? If so, you would override the + instead. Have a look here[^] for details.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        A 1 Reply Last reply
        0
        • P Pete OHanlon

          I can't see much sense in trying to assign a ComplexNumber from two others like this. Do you really mean it to be the summation of the two complex numbers to form the new one? If so, you would override the + instead. Have a look here[^] for details.

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          A Offline
          A Offline
          atoi_powered
          wrote on last edited by
          #9

          No as I said before, I somehow misunderstood the whole thing. By the way thanks for your attention and guidance :)

          1 Reply Last reply
          0
          • A atoi_powered

            I made a class for Complex Numbers. I don't know what to write in the body of my override function.

            public ComplexNumber(int real, int imagine)
            {
            //Real and Imagine are attributes.
            Real = real;
            Imagine = imagine;
            }

            public static ComplexNumber operator =(ComplexNumber a, ComplexNumber b)
            {
            return new /* ?????????? */;
            }

            P Offline
            P Offline
            Philippe Mori
            wrote on last edited by
            #10

            You should typically implement a function Clone that will create a copy of your class. See MSDN documentation for IClonable.

            public ComplexNumber Clone()
            {
            return new ComplexNumber { Real = this.Real, Imagine = this.Imagine };
            }

            By the way if you want to implement IClonable, it would be preferable to use explicit interface implementation so that your regular Clone function could returns a ComplexNumber (not sure if it still necessary with recent C# compiler).

            Philippe Mori

            1 Reply Last reply
            0
            • A atoi_powered

              I made a class for Complex Numbers. I don't know what to write in the body of my override function.

              public ComplexNumber(int real, int imagine)
              {
              //Real and Imagine are attributes.
              Real = real;
              Imagine = imagine;
              }

              public static ComplexNumber operator =(ComplexNumber a, ComplexNumber b)
              {
              return new /* ?????????? */;
              }

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #11

              I guess you are a C++ programmer (your user name further reinforces that impression). In C#, you cannot override assignment; a = b for a reference type will always assign a to exactly the same reference as b, and it's not possible to change this behaviour. (I think this is good because it means you know what a line is doing, whereas hiding copying in = requires you to know things about the class you're using.) If you want to create copies of an object, you should implement ICloneable (but read this[^]), a copy method (e.g. public ComplexNumber Copy() { ... }) and/or provide a copy constructor (i.e. a constructor that takes a ComplexNumber).

              A 1 Reply Last reply
              0
              • A atoi_powered

                I made a class for Complex Numbers. I don't know what to write in the body of my override function.

                public ComplexNumber(int real, int imagine)
                {
                //Real and Imagine are attributes.
                Real = real;
                Imagine = imagine;
                }

                public static ComplexNumber operator =(ComplexNumber a, ComplexNumber b)
                {
                return new /* ?????????? */;
                }

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #12

                atoi_powered wrote:

                I made a class for Complex Numbers

                IMO you got a wrong starting point, special number systems (such as complex) is something I would implement as a value type (i.e. a struct), not a class. That would make your "instances" immutable, and it would remove the problem of assignment. :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                A 1 Reply Last reply
                0
                • L Luc Pattyn

                  atoi_powered wrote:

                  I made a class for Complex Numbers

                  IMO you got a wrong starting point, special number systems (such as complex) is something I would implement as a value type (i.e. a struct), not a class. That would make your "instances" immutable, and it would remove the problem of assignment. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  A Offline
                  A Offline
                  atoi_powered
                  wrote on last edited by
                  #13

                  That's right! Thanks :)

                  L 1 Reply Last reply
                  0
                  • B BobJanova

                    I guess you are a C++ programmer (your user name further reinforces that impression). In C#, you cannot override assignment; a = b for a reference type will always assign a to exactly the same reference as b, and it's not possible to change this behaviour. (I think this is good because it means you know what a line is doing, whereas hiding copying in = requires you to know things about the class you're using.) If you want to create copies of an object, you should implement ICloneable (but read this[^]), a copy method (e.g. public ComplexNumber Copy() { ... }) and/or provide a copy constructor (i.e. a constructor that takes a ComplexNumber).

                    A Offline
                    A Offline
                    atoi_powered
                    wrote on last edited by
                    #14

                    You're completely right. I should try that instead. And by the way, I know there is a function in C++ called atoi but It has another meaning to me. It actually represents my slogan in a short form which is the same with that function's name! :-D Thanks for your help :)

                    1 Reply Last reply
                    0
                    • A atoi_powered

                      That's right! Thanks :)

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #15

                      you're welcome. :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      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