System.ValueType

  • Thread starter Thread starter Sahil Malik
  • Start date Start date
S

Sahil Malik

Okay, I can't inherit from System.ValueType. Why this restriction??

What I am trying to acheive is, create my own ValueType called "Money".

So if I have a decimal that has value 1.93991, when cast'ed to Money, it
gives me back "1.93", and I can easily write implicit and explicit casts to
do a to-and-fro conversion.

But I can't do such a conversion to-and-from from object, BECAUSE, my object
inherits from System.Object, and I have no other choice but to inherit from
System.Object #(@((# !)@()#_.

Is there an alternate way of acheiving this goal??

If you wanna see a code view, here is what I wanna do.

object o = 3.1991 ;
money m = (money)o ;
Console.WriteLine(m) ; // prints 3.19

alternatively,

money m = 1.29 ;
object o = m ;
decimal d = (decimal) o ;
Console.WriteLine(d) ; // prints 1.2900000 (well it won't print that but you
get the idea).

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Sahil Malik said:
Richard,

Yes :-), but read further below. I'd like my ValueType to implement
implicit/explicit conversions from Object. Structs don't let me do that.

Unfortunatly, you aren't going to be able to manage that. The only explict
conversion from object-to-structure is unboxing, if memory serves. This
feature isn't available from any valoue type to my knowledge.
 
Hmm .. I was hoping I was wrong, but now it makes two of us.
Thanks for your answer. :)

See the issue is, a lot of internal .NET code uses "Object" to do certain
things. That so because I guess most of it was written pre .NET 1.0, and we
didn't have generics or any other type agnostic mechanism except cast
everything to object where you didn't quite know what you might have to deal
with.

And for those kinds of situations, I'm S.O.L I guess.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
"Daniel O'Connell [C# MVP]"
Unfortunatly, you aren't going to be able to manage that. The only explict
conversion from object-to-structure is unboxing, if memory serves. This
feature isn't available from any valoue type to my knowledge.

The reason doesn't actually have anything to do with value types
specifically, it is just that the 1.2 language spec does not allow the source
or target type of a user-defined type conversion to be the object class. This
is true for both value types and reference types. The wording changed
slightly in the 2.0 spec but this would still not be allowed.

They do not want to let us write conversion operators that replace the
predefined conversions.

There is already a conversion from object to a struct (unboxing as you note)
so we cannot write our own conversion that would change this behavior.
 
MarkT said:
"Daniel O'Connell [C# MVP]"
Unfortunatly, you aren't going to be able to manage that. The only
explict
conversion from object-to-structure is unboxing, if memory serves. This
feature isn't available from any valoue type to my knowledge.

The reason doesn't actually have anything to do with value types
specifically, it is just that the 1.2 language spec does not allow the
source
or target type of a user-defined type conversion to be the object class.
This
is true for both value types and reference types. The wording changed
slightly in the 2.0 spec but this would still not be allowed.
I figured as much, but I didn't really want to dig that deeply into the spec
last night, ;).
 
Back
Top