473,796 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.ValueTyp e

Okay, I can't inherit from System.ValueTyp e. 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.WriteLi ne(m) ; // prints 3.19

alternatively,

money m = 1.29 ;
object o = m ;
decimal d = (decimal) o ;
Console.WriteLi ne(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 3061
A struct is a type that derives from System.ValueTyp e - 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.ValueTyp e. 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******@NOSPA Mdevelop.com> wrote in
message news:OE******** ******@TK2MSFTN GP12.phx.gbl...
A struct is a type that derives from System.ValueTyp e - 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.ValueTyp e. Why this restriction??

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

Nov 16 '05 #3

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

"Sahil Malik" <co************ *****@nospam.co m> wrote in message
news:ub******** ********@TK2MSF TNGP15.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***@discussi ons.microsoft.c om> wrote in message
news:6A******** *************** ***********@mic rosoft.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
1425
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
10101
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 since DBNull cannot be converted to int. I wrote the following method that looks up the column's data type and if it is a ValueType, returns the default value for the ValueType.
5
4032
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 is almost impossible to figure out what causes this. I've tried adding: AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler (SystemErrorHandler); to my main method, and a handler function:
1
281
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 structures have to be treated when interfacing with unmanaged libraries. Is this an actual member created in the structure? If it is, a pointer that is incrementing in an unmanaged library and performing a task on all the members of a structure...
10
1923
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
2034
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 true. --Musthafa
2
1812
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 (float, double, and decimal), and the code is highly redundant, but I can't seem to come up with a clean way to use Generics in the code. For example, the following doesn't work
1
1534
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 is derived from Object. What is the reson for overriding these two methods ? One mode question.
2
2322
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 and regards, Veeranna Ronad.
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.