By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,722 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,722 IT Pros & Developers. It's quick & easy.

inheritance in value types of = operator overloading in reference types

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.