469,282 Members | 1,967 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

inheritance in value types of = operator overloading in reference types

apm
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?
Nov 17 '05 #1
6 3105
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]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:gOd3f.12663$U%5.799@lakeread05...
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?

Nov 17 '05 #2
apm

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Og*************@TK2MSFTNGP12.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]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:gOd3f.12663$U%5.799@lakeread05...
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?


Nov 17 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:T7e3f.12668$U%5.2537@lakeread05...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Og*************@TK2MSFTNGP12.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]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:gOd3f.12663$U%5.799@lakeread05...
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?



Nov 17 '05 #4
apm

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
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.
Thank you. Forgot I could do that. I'll assume that is the same as
overloding Clone().


Of course, you should do this on a reference type. Doing it on a
structure is a bit redundant.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:T7e3f.12668$U%5.2537@lakeread05...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Og*************@TK2MSFTNGP12.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]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:gOd3f.12663$U%5.799@lakeread05...
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?



Nov 17 '05 #5
apm,

You can't overload Clone, since object doesn't expose it by default.
You have to implement it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:Jle3f.12669$U%5.10956@lakeread05...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP09.phx.gbl...
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.


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


Of course, you should do this on a reference type. Doing it on a
structure is a bit redundant.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:T7e3f.12668$U%5.2537@lakeread05...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Og*************@TK2MSFTNGP12.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]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:gOd3f.12663$U%5.799@lakeread05...
> 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?
>



Nov 17 '05 #6
If you are going to implement Clone you should probably inherit
IClonable.

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Fuzzyman | last post: by
6 posts views Thread by woosu | last post: by
5 posts views Thread by Javier Campos | last post: by
6 posts views Thread by Massimo Soricetti | last post: by
40 posts views Thread by Zach | last post: by
reply views Thread by erik.erikson | last post: by
10 posts views Thread by MasterGaurav \(www.edujini-labs.com\) | last post: by
68 posts views Thread by Jim Langston | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.