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 6 3062
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".
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".
"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.
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.
> "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" <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, ;). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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);
|
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.
|
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:
|
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...
|
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.
| |
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
|
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
|
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.
|
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.
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
| |
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |