473,581 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about struct (e.g. System.Drawing. Point)

Point is a struct
but I can do both

Point a;
Write(a.X);
Write(a.Y);

and
Point a = new Point(1,1);
Write(a.X);
Write(a.Y);
.....seems confusing!
Nov 15 '05 #1
3 3801
Action <am***@hotmail. com> wrote:
Point is a struct
but I can do both

Point a;
Write(a.X);
Write(a.Y);
Yes, although it's a bad idea to do that - I'd always write the above
as

Point a = new Point();
Write (a.X);
Write (a.Y);

It'll do the same thing (always, I believe) but it means that the whole
of the struct is definitely assigned.
and
Point a = new Point(1,1);
Write(a.X);
Write(a.Y);
....seems confusing!


Why? Basically "new" returns a new *value* for value-types, not a new
reference.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Does this mean that
Point a = new Point(1,2);
Write (a.X);
Write (a.Y);
1. a new Point is created
2. Value of the newly created Point is assigned to Point "a"
3. the "new Point" is destroyed, as Point "a" is a struct, it didn't really
points to the "new Point", so the "new Point" has zero reference => GC clean
it up....

is this the case?
thx!

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** @news.microsoft .com...
Action <am***@hotmail. com> wrote:
Point is a struct
but I can do both

Point a;
Write(a.X);
Write(a.Y);


Yes, although it's a bad idea to do that - I'd always write the above
as

Point a = new Point();
Write (a.X);
Write (a.Y);

It'll do the same thing (always, I believe) but it means that the whole
of the struct is definitely assigned.
and
Point a = new Point(1,1);
Write(a.X);
Write(a.Y);
....seems confusing!


Why? Basically "new" returns a new *value* for value-types, not a new
reference.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Action <am***@hotmail. com> wrote:
Does this mean that
Point a = new Point(1,2);
Write (a.X);
Write (a.Y);
1. a new Point is created
2. Value of the newly created Point is assigned to Point "a"
3. the "new Point" is destroyed, as Point "a" is a struct, it didn't really
points to the "new Point", so the "new Point" has zero reference => GC clean
it up....

is this the case?


Not quite - because Point is a value type, there *is* nothing on the
heap to be cleaned up. It's just the same as doing:

int a=5;
int b=10;
int x = a+b;

- the addition is performed, and the value is assigned to x. Exactly
the same kind of thing is happening here - the point is created, and
then assigned to the variable. At no point does anything need to be on
the heap - there are no references involved.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

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

Similar topics

1
469
by: Bilo | last post by:
Need Help with System.Drawing.Point b = new System.Drawing.Point(int x , int y) I want to move a window when mous is down and mous is moving private void label1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && video.Fullscreen != true) {
2
1478
by: Richard | last post by:
I each time get errors when I use an .bmp or ,.gif file as source but whenever I use a .jpg file it works perfect. Why is that? and how to fix it? I almost have no knowledge about the system.drawing and so I am in the dark about this. This is my code
5
4351
by: hzgt9b | last post by:
I'm building a dataset that writes out a Point type value. Here's the code that I've got: 1 Dim dsContent As DataSet = New DataSet("content") 2 Dim dtAsset As DataTable = New DataTable("asset") 3 Dim dcPk(0) As DataColumn 4 dcPk(0) = _ 5 dtAsset.Columns.Add("fullName", Type.GetType("System.String")) 6 ...
5
3358
by: Jerry J | last post by:
I want to use the System.Drawing.Image class. According to the help file, this is an abstract base class. Because it is supposedly abstract, I created another class that inherits from it. However, when I did this I got the following error: 'System.Drawing.Image.Image()' is inaccessible due to its protection level Looking at other online...
1
9439
by: Marc the Demi-Programmer | last post by:
I am overriding WncProc to make sure my form's location stays within specified parameters. Basically, it has to stay at the top of the screen and not be off to either of the sides. All that works when the form is in its minimal size. However, when the form is maximized using the appropriate button the code that is supposed to set the...
1
1170
by: dr | last post by:
how to get the System.Drawing.Point of FormStartPosition, e.g. if i want to recenter the form on the screen at some point other then startup.
2
13083
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, what is the unit of System.Drawing.Size? when i'm writing Size = (1024,784), is it pixel? or something else? Thanks,
5
3570
by: Dave Bootsma | last post by:
I have an application where I want to redraw a polygon from points I retrieve from a file. The file is read with streamreader line by line each line contains the points for each polygon. Below is a typical line from my file. 500,748 500,678 552,678 552,696 584,696 584,714 612,714 612,748 Each set of x,y coordinates is separated by a space....
2
3265
by: ouzsharp | last post by:
The follwing class works well when used in a C# console application: namespace ScreenShotDemo { /// <summary> /// Provides functions to capture the entire screen, or a particular window, and save it to a file. /// </summary> public class ScreenCapture {
0
7876
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...
0
7804
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7910
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...
0
8180
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...
0
6563
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...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2307
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
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.