Re: Extending System.Drawing.Rectangle...

Chris Anderson <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
Well, the other way around (widening = implicit), but yeah ..looks that way (I just knocked up a little test app to check)
So in this case you would just write two narrowing/implicit conversion operators (because Rectangle and the new "extended" RectangleEx differ only in the methods they expose), and being implicit will automatically convert between the types if you pass a RectangleEx to a System.Drawing function, etc (Of course passing to ByRef will result in two additional types being created as it converts to then from, etc - so performance may be a consideration)
 
 
Public Shared Widening Operator CType(ByVal item As RectangleEx) As Rectangle
  Return New Rectangle(item.X, item.Y, item.Width, item.Height)
End Sub
Public Shared Widening Operator CType(ByVal item As Rectangle) As RectangleEx
  Return New RectangleEx(item.X, item.Y, item.Width, item.Height)
End Sub
 
or in C#
 
public static implicit operator Rectangle(RectangleEx item)
{
  return new Rectangle(item.X, item.Y, item.Width, item.Height);
}
 
public static implicit operator RectangleEx(Rectangle item)
{
  return new RectangleEx(item.X, item.Y, item.Width, item.Height);
}
 
Sweet!
 

________________________________

From: Discussion forum for developers using Windows Forms to build apps and controls on behalf of Dave
Sent: Fri 20/04/2007 03:46
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Extending System.Drawing.Rectangle...



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
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.