Re: A question on the RECT struct
- From: Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Mon, 20 Aug 2007 19:52:53 -0700
Dom wrote:
I hope I'm not a pest, but I guess the question I have is better put
this way: Since there are already 3 overloads for the Rectangle
constructor, why isn't there just a fourth that takes the LTRB args?
I'm just confused about the two calls, one to a constructor, one to a
method.
Here's the declaration for the constructor that takes the top-left coordinates and the size:
public Rectangle(int x, int y, int width, int height);
Here's the declaration for the constructor that takes the top-left and bottom-right coordinates:
public Rectangle(int xLeft, int yTop, int xRight, int yBottom);
Here's some code that uses these constructors:
// initialize with top-left and size:
Rectangle rectA = new Rectangle(10, 20, 30, 30);
// initialize with top-left and bottom-right:
Rectangle rectB = new Rectangle(10, 20, 40, 50);
Now, imagine you're the compiler. In the above code example, how do you know which constructor goes with which assignment?
That should illustrate the issue, but just in case:
The reason that the constructor exists for one and not the other is that both are method signatures that takes four integers and return a Rectangle. You can't have two constructors with identical signatures, so at least one of the methods cannot be a constructor. It has to be a plain static method that returns a Rectangle.
Now, a valid question might be: why not forego the constructor altogether and make things consistent by making all of the initialization methods be plain static methods.
IMHO, the answer to that is that a constructor really is a preferable way to initialize an object (it's a lot more discoverable, especially via Intellisense, for one), and so it's worth making as many of the initialization methods be constructors.
That's one possible answer. I don't know if that's the actual reason for the current design, but it seems plausible to me.
Pete
.
- Follow-Ups:
- Re: A question on the RECT struct
- From: Dom
- Re: A question on the RECT struct
- References:
- A question on the RECT struct
- From: Dom
- Re: A question on the RECT struct
- From: Mattias Sjögren
- Re: A question on the RECT struct
- From: Dom
- Re: A question on the RECT struct
- From: Peter Duniho
- Re: A question on the RECT struct
- From: Dom
- A question on the RECT struct
- Prev by Date: Multithreaded GUI issues
- Next by Date: Re: C++ v C#
- Previous by thread: Re: A question on the RECT struct
- Next by thread: Re: A question on the RECT struct
- Index(es):
Relevant Pages
|