473,651 Members | 2,750 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 1553
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.publi c.dotnet.langua ges.csharp]
Nov 17 '05 #2
"Sue & Bill" <su**********@g mail.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*******@virg ilio.it> ha scritto nel messaggio
news:dQ******** *************@n ews3.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.publi c.dotnet.langua ges.csharp/<11************ *********@f14g2 000cwb.googlegr oups.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.publi c.dotnet.langua ges.csharp]
Nov 17 '05 #6

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

Similar topics

6
2012
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 understanding that only "named" storage areas(from the standard) were data objects. Since constants and string literals don't have lvalues, they can't be data objects. Yet, I was shown the first page of chapter 2 in K&R2, which states that variables...
7
1929
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 duration associated with the enclosing block". So I tried the annexed code, and it compiles without a warning, and works as I expected.
49
14475
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 http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
3
1330
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 the code: def inputNumber(n): if n == 17: raise 'BadNumberError: ', '17 is off limits.' else: print n, 'is a nice number'
93
3938
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 so that it will compile in all old and broken compilers, preferably in such a fashion that it can be moved with no effort from the embedded system in the coffe machine to the 64 bit processor in your desktop.
35
3200
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 instance of an object" for the line 'If mpg.FindControl("lkred").Visible = True Then'. I couldn't find sofar the solution. Any help would be appreciated ... Thanks
13
2307
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 add-ons for this purpose? If not, any urls which demonstrates code snippets management?
2
3984
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 arrow and you see the entries in a dropdown. But how would I control this thing from javascript? Here is the HTML: <div class="selection" style="top:0; left:89; width:159; height:21;"
19
2215
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? (in conjunction with css, you know, the usual...;) this is what is meant by "modern" javascript?? so how do folks feel about this who think javascript is so evil they disable it from their browsers?? do sites designed with "modern" javascript...
0
8802
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...
0
8697
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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
8579
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...
1
6158
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
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
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1909
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.