Re: Extending System.Drawing.Rectangle...

Dave <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <000201c782f5$c803f750$6400a8c0@omniscient1>
Good!! Cool!!

Thanks!!  Seems cleaner in C#, indeed

Dave.

P.S.  In my understanding, "implicit" and "explicit" would be equivalent to
"narrowing" and "widening"...  Didn't check it in Reflector...  Would that
be the case?

-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps and
controls [mailto:[email protected]] On Behalf Of Chris
Anderson
Sent: April 19, 2007 8:18 PM
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Extending System.Drawing.Rectangle...

Good point, I forgot about conversion operators.

FWIW in C# it would be something like :

        public static implicit operator Rectangle (RectangleEx r) {...}
        public static implicit operator RectangleEx (Rectangle r) {...}
or
        public static explicit operator Rectangle (RectangleEx r) {...}
        public static explicit operator RectangleEx (Rectangle r) {...}

You could use implicit in this case as no member vars are being added
(and so all Rectangles are compatible with all RectangleExs), allowing
code like this to be used:

        RectangleEx rex = new RectangleEx(1,2,3,4);
        Rectangle r = rex; //no casting required!

In the case of a class I wouldn't like it (because it looks like you
have two references to the same instance, whereas you actually have
created a new instance - they are called conversion operators for a
reason), but with a struct you *know* that rex and r are not the same
"instance", so it's not hiding anything.

Nice idea, Dave!

(BTW I don't know about VB support, but IIRC this ability was in C#
since 1.0 :) )

Chris

> -----Original Message-----
> From: Discussion forum for developers using Windows Forms to build
apps
> and controls [mailto:[email protected]] On Behalf Of
> Dave
> Sent: 20 April 2007 00:46
> To: [email protected]
> Subject: Re: [DOTNET-WINFORMS] Extending System.Drawing.Rectangle...
>
> Uh?
>
> What about just casting the object from one to the other, with a cast
> operator?  Don't need 3.0 to do that...  Although you need 2.0 to make
> it
> clean, it's pretty easy with 1.0-1.1 with functions like ToRectangle()
> and
> ctor(Rectangle)
>
> Try that:
> (And if you're not comfortable with VB, think of Reflector...)
>
> Public Structure RectangleEx
<snip>
>
>     Public Shared Narrowing Operator CType(ByVal item As RectangleEx)
> As
> Rectangle
>         Return New Rectangle(item.X, item.Y, item.Width, item.Height)
>     End Operator
>
>     Public Shared Widening Operator CType(ByVal item As Rectangle) As
> RectangleEx
>         Return New RectangleEx(item.X, item.Y, item.Width,
item.Height)
>     End Operator
> End Structure
>
> Have fun!
>
> Dave.
> www.omniscienttrader.com
> www.omniscient.ca
>
>
> -----Original Message-----
> From: Discussion forum for developers using Windows Forms to build
apps
> and
> controls [mailto:[email protected]] On Behalf Of
> Chris
> Anderson
> Sent: April 18, 2007 2:28 AM
> To: [email protected]
> Subject: Re: [DOTNET-WINFORMS] Extending System.Drawing.Rectangle...
>
> You'd probably need to wrap the Rectangle inside a new class and
> internally
> use the Rectangle to perform standard Rectangle functionality, tgen
add
> what
> you want.
> Of course you wouldn't be able to use the new class *as* a Rectangle
> where a
> Rectangle was expected.
>
> In .net 3.0 you *might* be able to use extension methods. ( not 100%
> sure if
> you can use them on structs, but worth checking)
>
> What extra functionality are you looking to add?
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.