473,748 Members | 4,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question on the RECT struct

Dom
is there a difference between the following:

Rectangle r = new Rectangle (10,20,30,40);
Rectangle r = Rectangle.FromL TRB(10,20,30,40 );

Also, I guess I'm not sure why Structs are created with a NEW keyword,
anyway. Aren't they located on the stack, like values, and isn't NEW
used to create space on the heap?

Dom

Aug 20 '07 #1
5 2212
>is there a difference between the following:

Rectangle r = new Rectangle (10,20,30,40);
Rectangle r = Rectangle.FromL TRB(10,20,30,40 );
Yes. In the first case 30 and 40 will be the width and height
respectively of the rectangle. In the FromLRTB case it will be the
Right and Bottom coordinates.

>Also, I guess I'm not sure why Structs are created with a NEW keyword,
anyway. Aren't they located on the stack, like values, and isn't NEW
used to create space on the heap?
No, using the new keyword doesn't imply heap allocation. And structs
aren't always located on the stack.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 20 '07 #2
Dom <do********@gma il.comwrote:

<snip>
Structs are not ALWAYS located on the heap? When are they, and when
are they not?
See http://pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 20 '07 #3
Dom
On Aug 20, 4:58 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
Dom <dolivas...@gma il.comwrote:

<snip>
Structs are not ALWAYS located on the heap? When are they, and when
are they not?

Seehttp://pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk...@pobox.co m>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Thanks for the article. Very helpful. The other articles at the site
(that I've read) were also quite good.

Aug 21 '07 #4
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
Aug 21 '07 #5
Dom
On Aug 20, 10:52 pm, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.com>
wrote:
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
Got it. Thanks.

Aug 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6441
by: Alex NSB | last post by:
Hi all. Consider the following page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML><HEAD><TITLE>Test</TITLE> <STYLE TYPE="text/css"> <!-- #mylayerDiv {position:absolute;left:50px;top:50px;clip:rect(0px, 50px, 50px,
0
1760
by: Marco | last post by:
How can I disable the rect that is display when I click on the button or press tab button? Thanks. Marco
2
1527
by: Kay L. | last post by:
Hi, when I draw a long text inside a rect, it will go outside, I want to know which charactor is the last one which is inside the rect. I know I can do like below, but it's not efficient, is there any better way to do this, thanks. CString str = "a long text"; // if it's too long, I want to draw "a long t..."; CRect rc(0,0,100,100); char c; for(int i=0; i<str.GetLength(); i++)
6
23055
by: Mythran | last post by:
I have a question regarding the RECT structure required for PInvoke calls. I was reviewing the Rectangle structure (System.Drawing.Rectangle) and it occurred to me that I may be able to use this structure in place of defining a RECT structure. I would create the RECT structure definition the same as the Rectangle structure is defined. Is there any problems with using Rectangle in place of RECT for the API functions? Thanks, Mythran
9
2033
by: bunty | last post by:
Hi all, I am working on 16 bit app on windows. My problem is I am using windows RECT structure. on 16 bit windows it 's all member are declared as short which allows max 32767 max value. and i require more valus to be stored for the Rect. So i created my own rect structure with long members. but when i do this call windows API GetWindowRect() fails. it does fill the rect structure with appropriate values. can any one help.
1
2183
by: osmarjunior | last post by:
I override the OnShown event of a form and call both methods Select() and Focus() of a RadioButton. The RadioButton is selected, but the focus rect is not visible. If I press Tab key to exit the RadioButton, and press Shift+Tab to back to the control, then the focus rect is visible... But I wanna make the focus rect visible as soon as the form is shown. How can I ensure this? Thanks.
2
1349
by: yinglcs | last post by:
Hi, I have a class called 'A', and it has a method called 'Rect(int x, int y, int w, int h)' And I have another class called 'Rect'. When I called 'Rect* in other method of A, I get this compiler error: void A::aMethod() { Rect* lastRect;
1
1427
by: globalrev | last post by:
http://www.pygame.org/docs/ref/rect.html#Rect.move well i need to put my rect ina specific place and i donw know from where i will move it so i need a function rect.moveTo(x,y). how do i do that?
29
4093
by: Chris Riesbeck | last post by:
I have an image with a class and the class defines a clip rectangle. In Firefox 2 and 3, and Opera 9, I can access the rectangle with document.defaultView.getComputedStyle(). But that doesn't seem to work in Safari for Windows 3, nor when I use image.currentStyle.clip in IE 7. Is there a way to do this in those browsers? Am I doing something stupid?
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
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...
0
8243
agi2029
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...
0
4606
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...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
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
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.