inheritance in value types of = operator overloading in reference types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • apm

    inheritance in value types of = operator overloading in reference types

    Recently I have had to use a value type for a complex structure because I
    don't know how to override the = operator. Can the operator ever be
    overloaded? Or can inheritance be used with value types?


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: inheritance in value types of = operator overloading in reference types

    apm,

    Inheritance and the = operator being overloaded are two different
    things. First, you can not inherit from value types.

    Second, you can not overload the = operator.

    Why did you think that using a value type would allow you to overload
    the = operator?

    You might be able to get away with what you are doing by overloading the
    casting (implicit and explicit).

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "apm" <Contributor@Ad sorptionProcess Modeling.com> wrote in message
    news:gOd3f.1266 3$U%5.799@laker ead05...[color=blue]
    > Recently I have had to use a value type for a complex structure because I
    > don't know how to override the = operator. Can the operator ever be
    > overloaded? Or can inheritance be used with value types?
    >[/color]


    Comment

    • apm

      #3
      Re: inheritance in value types of = operator overloading in reference types


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:OgJ8Yb2zFH A.596@TK2MSFTNG P12.phx.gbl...[color=blue]
      > apm,
      >
      > Inheritance and the = operator being overloaded are two different
      > things. First, you can not inherit from value types.
      >
      > Second, you can not overload the = operator.
      >
      > Why did you think that using a value type would allow you to overload
      > the = operator?[/color]

      The = does what I want it to do in a value type. I don't have to overload
      it. I just wanted to 'have my cake and eat it too'. Why won't inheritance
      work with value types?
      [color=blue]
      >
      > You might be able to get away with what you are doing by overloading
      > the casting (implicit and explicit).
      >
      > Hope this helps.
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "apm" <Contributor@Ad sorptionProcess Modeling.com> wrote in message
      > news:gOd3f.1266 3$U%5.799@laker ead05...[color=green]
      >> Recently I have had to use a value type for a complex structure because I
      >> don't know how to override the = operator. Can the operator ever be
      >> overloaded? Or can inheritance be used with value types?
      >>[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: inheritance in value types of = operator overloading in reference types

        apm,

        Structures (value types) can not be inherited from. That's just the way
        it is.

        So you want copy semantics on assignment. You should just implement the
        IClonable interface and have it return the results of MemberwiseClone , as
        this will do the same thing that assigning a structure to a variable of that
        structure's type does.

        Of course, you should do this on a reference type. Doing it on a
        structure is a bit redundant.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "apm" <Contributor@Ad sorptionProcess Modeling.com> wrote in message
        news:T7e3f.1266 8$U%5.2537@lake read05...[color=blue]
        >
        > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
        > in message news:OgJ8Yb2zFH A.596@TK2MSFTNG P12.phx.gbl...[color=green]
        >> apm,
        >>
        >> Inheritance and the = operator being overloaded are two different
        >> things. First, you can not inherit from value types.
        >>
        >> Second, you can not overload the = operator.
        >>
        >> Why did you think that using a value type would allow you to overload
        >> the = operator?[/color]
        >
        > The = does what I want it to do in a value type. I don't have to overload
        > it. I just wanted to 'have my cake and eat it too'. Why won't inheritance
        > work with value types?
        >[color=green]
        >>
        >> You might be able to get away with what you are doing by overloading
        >> the casting (implicit and explicit).
        >>
        >> Hope this helps.
        >>
        >> --
        >> - Nicholas Paldino [.NET/C# MVP]
        >> - mvp@spam.guard. caspershouse.co m
        >>
        >> "apm" <Contributor@Ad sorptionProcess Modeling.com> wrote in message
        >> news:gOd3f.1266 3$U%5.799@laker ead05...[color=darkred]
        >>> Recently I have had to use a value type for a complex structure because
        >>> I don't know how to override the = operator. Can the operator ever be
        >>> overloaded? Or can inheritance be used with value types?
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • apm

          #5
          Re: inheritance in value types of = operator overloading in reference types


          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          message news:%23vwvMo2z FHA.1256@TK2MSF TNGP09.phx.gbl. ..[color=blue]
          > apm,
          >
          > Structures (value types) can not be inherited from. That's just the
          > way it is.
          >
          > So you want copy semantics on assignment. You should just implement
          > the IClonable interface and have it return the results of MemberwiseClone ,
          > as this will do the same thing that assigning a structure to a variable of
          > that structure's type does.[/color]

          Thank you. Forgot I could do that. I'll assume that is the same as
          overloding Clone().

          [color=blue]
          >
          > Of course, you should do this on a reference type. Doing it on a
          > structure is a bit redundant.
          >
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "apm" <Contributor@Ad sorptionProcess Modeling.com> wrote in message
          > news:T7e3f.1266 8$U%5.2537@lake read05...[color=green]
          >>
          >> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
          >> in message news:OgJ8Yb2zFH A.596@TK2MSFTNG P12.phx.gbl...[color=darkred]
          >>> apm,
          >>>
          >>> Inheritance and the = operator being overloaded are two different
          >>> things. First, you can not inherit from value types.
          >>>
          >>> Second, you can not overload the = operator.
          >>>
          >>> Why did you think that using a value type would allow you to overload
          >>> the = operator?[/color]
          >>
          >> The = does what I want it to do in a value type. I don't have to overload
          >> it. I just wanted to 'have my cake and eat it too'. Why won't
          >> inheritance work with value types?
          >>[color=darkred]
          >>>
          >>> You might be able to get away with what you are doing by overloading
          >>> the casting (implicit and explicit).
          >>>
          >>> Hope this helps.
          >>>
          >>> --
          >>> - Nicholas Paldino [.NET/C# MVP]
          >>> - mvp@spam.guard. caspershouse.co m
          >>>
          >>> "apm" <Contributor@Ad sorptionProcess Modeling.com> wrote in message
          >>> news:gOd3f.1266 3$U%5.799@laker ead05...
          >>>> Recently I have had to use a value type for a complex structure because
          >>>> I don't know how to override the = operator. Can the operator ever be
          >>>> overloaded? Or can inheritance be used with value types?
          >>>>
          >>>
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: inheritance in value types of = operator overloading in reference types

            apm,

            You can't overload Clone, since object doesn't expose it by default.
            You have to implement it.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "apm" <Contributor@Ad sorptionProcess Modeling.com> wrote in message
            news:Jle3f.1266 9$U%5.10956@lak eread05...[color=blue]
            >
            > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
            > in message news:%23vwvMo2z FHA.1256@TK2MSF TNGP09.phx.gbl. ..[color=green]
            >> apm,
            >>
            >> Structures (value types) can not be inherited from. That's just the
            >> way it is.
            >>
            >> So you want copy semantics on assignment. You should just implement
            >> the IClonable interface and have it return the results of
            >> MemberwiseClone , as this will do the same thing that assigning a
            >> structure to a variable of that structure's type does.[/color]
            >
            > Thank you. Forgot I could do that. I'll assume that is the same as
            > overloding Clone().
            >
            >[color=green]
            >>
            >> Of course, you should do this on a reference type. Doing it on a
            >> structure is a bit redundant.
            >>
            >>
            >> --
            >> - Nicholas Paldino [.NET/C# MVP]
            >> - mvp@spam.guard. caspershouse.co m
            >>
            >> "apm" <Contributor@Ad sorptionProcess Modeling.com> wrote in message
            >> news:T7e3f.1266 8$U%5.2537@lake read05...[color=darkred]
            >>>
            >>> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
            >>> in message news:OgJ8Yb2zFH A.596@TK2MSFTNG P12.phx.gbl...
            >>>> apm,
            >>>>
            >>>> Inheritance and the = operator being overloaded are two different
            >>>> things. First, you can not inherit from value types.
            >>>>
            >>>> Second, you can not overload the = operator.
            >>>>
            >>>> Why did you think that using a value type would allow you to
            >>>> overload the = operator?
            >>>
            >>> The = does what I want it to do in a value type. I don't have to
            >>> overload it. I just wanted to 'have my cake and eat it too'. Why won't
            >>> inheritance work with value types?
            >>>
            >>>>
            >>>> You might be able to get away with what you are doing by overloading
            >>>> the casting (implicit and explicit).
            >>>>
            >>>> Hope this helps.
            >>>>
            >>>> --
            >>>> - Nicholas Paldino [.NET/C# MVP]
            >>>> - mvp@spam.guard. caspershouse.co m
            >>>>
            >>>> "apm" <Contributor@Ad sorptionProcess Modeling.com> wrote in message
            >>>> news:gOd3f.1266 3$U%5.799@laker ead05...
            >>>>> Recently I have had to use a value type for a complex structure
            >>>>> because I don't know how to override the = operator. Can the operator
            >>>>> ever be overloaded? Or can inheritance be used with value types?
            >>>>>
            >>>>
            >>>>
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • kevin  cline

              #7
              Re: inheritance in value types of = operator overloading in reference types

              If you are going to implement Clone you should probably inherit
              IClonable.

              Comment

              Working...