Extend System.Decimal?

  • Thread starter Thread starter Fredrik Melin
  • Start date Start date
F

Fredrik Melin

Is there any way to extend or write a own "System.Decimal"?

In the doc its declared as
<Serializable>
Public Structure Decimal
Implements IFormattable, IComparable, IConvertibleBut how do you get the
functionallity that you can do Dim x as mydecimalx = 100Is that possible or
can only microsoft do stuff like that?RegardsFredrik
 
Frederik,

In my opinion can you create any class that holds the methods to do what you
want, however not create a new value type.

Cor
 
Fredrick,
Decimal, being a Structure is a value type, which work the same as
NotInheritable Classes. Which means you cannot extend it directly, you would
need to encapsulate it and delegate all the methods to the real structure.

You can define MyDecimalX as a Structure and it will have all the value type
semantics of the Decimal type. However you will need to wait for VS.NET 2005
(aka Whidbey, due out in 2005) to support overloaded operators. Which will
allow your MyDecimalX to support +, -, /, * and other operators that you
define.

For a quick intro to overloading operators in VB.NET see:

http://blogs.gotdotnet.com/cambecc/permalink.aspx/5de5a161-9150-4237-a751-127195cceeab

Overloading operator CType allows you to define custom conversions, for
example Decimal to & from MyDecimalX, allowing:

Dim d As Decimal
Dim md As MyDecimalX
d = md ' Widening Operator CType
md = CType(d, MyDecimalX) ' Narrowing Operator CType

FYI: VB.NET 2002 & 2003 knows about the Decimal type, so we are currently
able to use its overloaded operators directly. With VB.NET 2005 we will be
able to make use of the overloaded operators on any type in the Framework,
such as DateTime & TimeSpan.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top