473,383 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

System.ValueType

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

Nov 16 '05 #1
6 3014
A struct is a type that derives from System.ValueType - that is how you're supposed to create your own value types

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

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

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

Nov 16 '05 #2
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.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:OE**************@TK2MSFTNGP12.phx.gbl...
A struct is a type that derives from System.ValueType - that is how you're
supposed to create your own value types

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

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

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

Nov 16 '05 #3

"Sahil Malik" <co*****************@nospam.com> wrote in message
news:ub****************@TK2MSFTNGP15.phx.gbl...
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.
Nov 16 '05 #4
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]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eC****************@TK2MSFTNGP11.phx.gbl...

"Sahil Malik" <co*****************@nospam.com> wrote in message
news:ub****************@TK2MSFTNGP15.phx.gbl...
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.

Nov 16 '05 #5
> "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.
Nov 16 '05 #6

"MarkT" <Ma***@discussions.microsoft.com> wrote in message
news:6A**********************************@microsof t.com...
"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, ;).


Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jean Stax | last post by:
Hi ! Jeffrey Richter gives the following example in his book: struct Point { Int32 x, y; } class AAA{ .... public virtual void Add(Object value);
4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
5
by: Vitling | last post by:
For no apparent reason, a NullReference exception is thrown in system.dll (System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback). Since I only get a disassembly from Visual Studio, it...
1
by: kat | last post by:
Hi, I am pretty new to C#. I was debugging a project that I developed, and I noticed in the locals window that one of the members in my structure is System.ValueType. This could affect the way...
10
by: Qwert | last post by:
Hello, is it correct that if a type (System.Type) is a value (.IsValueType=True) and not primitive (.IsPrimitive=False), that type is a structure? Thanks.
6
by: Manikkoth | last post by:
Hello, Just curious to see why ValueType (which the base for all value types) is a class. I thought "class" would make a type a reference type. However, IsValueType for ValueType is returning...
2
by: Ravi Shekhar | last post by:
Hello, So I'm doing some mathematical modeling and it turns out I need distinct imaginary and complex types to carry out complex contour integration. I want to have it in all three precisions...
1
by: Tony Johansson | last post by:
Hello! Everything not derived from System.ValueType is a reference type. I just wonder this ValueType type is derived from Object and it overrides the two methods Equals and GetHashCode which...
2
by: Veeranna Ronad | last post by:
Hello, Our application gets datetime from an interface function. This function returns "ValueType". How to copy datetime content from this "ValueType" to DateTime variable? Thanks in advance...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.