473,503 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object "Literals"

I have:

Rectangle[] rects = new Rectangle[10000];
I want to initialize the values of rects as follows:

Option A:
========
for (int i=0; i<rects.Length; i++)
{
rects[i].X = 0;
rects[i].Y = 0;
rects[i].Size.Width = 100;
rects[i].Size.Height = 20;
}

(By the way, rects[i].Size.Width = 100 above generates a compiler
error. I haven't figured out why.)

What is the impact on memory or performance, if any, if I use the
following instead:

Option B:
========
for (int i=0; i<rects.Length; i++)
rects[i]=new Rectangle(new Point(0,0), new Size(100,20));

Option C:
========
for (int i=0; i<rects.Length; i++)
{
rects[i].Location = new Point(0,0);
rects[i].Size = new Size(100,20);
}
Thank you for your help.

Nov 17 '05 #1
5 1543
A Rectangle is a value type and so when you index into the array you get back a copy. The compiler is trying to prevent you from doing something you are not intending - updating a copy of the array memeber not the array member itself. Option B is the only one that wil actually do what you want.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I have:

Rectangle[] rects = new Rectangle[10000];
I want to initialize the values of rects as follows:

Option A:
========
for (int i=0; i<rects.Length; i++)
{
rects[i].X = 0;
rects[i].Y = 0;
rects[i].Size.Width = 100;
rects[i].Size.Height = 20;
}

(By the way, rects[i].Size.Width = 100 above generates a compiler
error. I haven't figured out why.)

What is the impact on memory or performance, if any, if I use the
following instead:

Option B:
========
for (int i=0; i<rects.Length; i++)
rects[i]=new Rectangle(new Point(0,0), new Size(100,20));

Option C:
========
for (int i=0; i<rects.Length; i++)
{
rects[i].Location = new Point(0,0);
rects[i].Size = new Size(100,20);
}
Thank you for your help.
[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #2
"Sue & Bill" <su**********@gmail.com> ha scritto nel messaggio
Rectangle[] rects = new Rectangle[10000];
I want to initialize the values of rects as follows:

Option A:
========
for (int i=0; i<rects.Length; i++)
{
rects[i].X = 0;
rects[i].Y = 0;
rects[i].Size.Width = 100;
rects[i].Size.Height = 20;
}

(By the way, rects[i].Size.Width = 100 above generates a compiler
error. I haven't figured out why.)
Because the implementation of Rectangle.Size is

public Size Size
{
get { return new Size(this.Width, this.Height); }
set {[snip]}
}

So you cannot assign a value to a "new Size".
You need to use the "set" part:

rects[i].Size = new Size(100, 20);

What is the impact on memory or performance, if any, if I use the
following instead:

Option B:
========
for (int i=0; i<rects.Length; i++)
rects[i]=new Rectangle(new Point(0,0), new Size(100,20));

Option C:
========
for (int i=0; i<rects.Length; i++)
{
rects[i].Location = new Point(0,0);
rects[i].Size = new Size(100,20);
}


Here it is the implementation of Rectangle.ctor(Point, Size)

public Rectangle(Point location, Size size)
{
this.x = location.X;
this.y = location.Y;
this.width = size.Width;
this.height = size.Height;
}

So if you use Rectangle.ctor(int, int, int, int) you avoid the two "new" on
Size and Point.
The same on C).
--
Reporting tool: http://www.neodatatype.net
Nov 17 '05 #3
"Zanna" <zn*******@virgilio.it> ha scritto nel messaggio
news:dQ*********************@news3.tin.it...

The same on C).


That does not work as the A) ;)

--
Reporting tool: http://www.neodatatype.net
Nov 17 '05 #4
Thanks all for the explanation of why .Size.Width cannot be assigned.
A mystery solved, much appreciated.

My main question was whether 10,000 calls of the new statement would
gobble up lots of memory. If not, is there a way to create object
"literals" to assign to an existing instance of an object?

Thanks.

Nov 17 '05 #5
It will not allocate any more memory it will reinitialize the existing array member as Rectangle is a value type

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<11*********************@f14g2000cwb.googlegroups. com>

Thanks all for the explanation of why .Size.Width cannot be assigned.
A mystery solved, much appreciated.

My main question was whether 10,000 calls of the new statement would
gobble up lots of memory. If not, is there a way to create object
"literals" to assign to an existing instance of an object?

Thanks.
[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #6

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

Similar topics

6
1997
by: kobu.selva | last post by:
I was recently part of a little debate on the issue of whether constants and string literals are considered "data objects" in C. I'm more confused now than before. I was always under the...
7
1915
by: Eric Laberge | last post by:
Aloha! This question is meant to be about C99 and unnamed compound objects. As I read, if such a construct as int *p = (int){0}; is used within a function, then it has "automatic storage...
49
14430
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
3
1324
by: Alex Pavluck | last post by:
Hello. On page 124 of "Thinking like a Computer Scientist". There is an exercise to take the following code and with the use of TRY: / EXCEPT: handle the error. Can somone help me out? Here is...
93
3870
by: jacob navia | last post by:
In this group there is a bunch of people that call themselves 'regulars' that insist in something called "portability". Portability for them means the least common denominator. Write your code...
35
3159
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag in the masterpage into 'false' when the user is logged. But i get the error: "Object reference not set to an...
13
2277
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
2
3966
by: Angus | last post by:
I am trying to change the selection in Javascript - but this HTML element is not a standard option control. On the web page it looks like a dropdown list - and you click on the right hand down...
19
2180
by: maya | last post by:
hi, so what is "modern" javascript?? the same as "DOM-scripting"? i.e., editing content (or changing appearance of content) dynamically by massaging javascript objects, html elements, etc? ...
0
7199
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,...
0
7076
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...
0
7274
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,...
0
5576
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,...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
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...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
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 ...
1
732
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.