Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Performance / July 2005

Tip: Looking for answers? Try searching our database.

accessing complicated ArrayList members

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SharpCoderMP - 20 Jul 2005 12:09 GMT
hi,

i'm using ArrayList very often - it's so easy to use and flexible.
unfortuntely i run in some trouble with it. generaly speaking this
collection is in fact read only when it comes down to storing something
complicated - like object or structure. in general many people say that
when you change object or structure stored in arraylist then this object
(or structure) should be created as new one and put into the arraylist
instead of modyfinig existing data.

but what about perfomance? ie. i need to store structure that contains
bitmap amog some simple values. so it takes some memory. so when i would
like to change one field of this structure i am forced to create a new
one, with all the data from old one and put it again inside arraylist.
as long as this bitmap has few kilobytes it's not such a big deal, but
what if the bitmap has 50kb or 500kb?

is there a way to do it in efficient way? can i somehow modyfy just one
field from my structure stored inside arraylist?
Alvin Bruney [MVP - ASP.NET] - 20 Jul 2005 23:57 GMT
generaly speaking this
> collection is in fact read only when it comes down to storing something
> complicated - like object or structure.
where did you get that from?

. so it takes some memory. so when i would
> like to change one field of this structure i am forced to create a new
> one, with all the data from old one and put it again inside arraylist. as
> long as this bitmap has few kilobytes it's not such a big deal,
Are you writing some sort of loop that iterates the array collection and you
need to modify these items? If this is the case, you need to post that part
of the code, because from your description, you code is suspect.
Signature

Regards,
Alvin Bruney - ASP.NET MVP

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
Now available @ www.lulu.com/owc, Amazon.com etc

> hi,
>
[quoted text clipped - 15 lines]
> is there a way to do it in efficient way? can i somehow modyfy just one
> field from my structure stored inside arraylist?
SharpCoderMP - 21 Jul 2005 09:48 GMT
>>collection is in fact read only when it comes down to storing something
>>complicated - like object or structure.
> where did you get that from?
well in fact it's just a loose interpretation of some conventions that i
red about (on this newsgroup amog others). the problem is that, when i
need to get something out of this list i need to unbox it first. after
that i get copy of oryginal data, so infact modifying it wont affect
data stored inside arraylist. right?

>>like to change one field of this structure i am forced to create a new
>>one, with all the data from old one and put it again inside arraylist. as
>>long as this bitmap has few kilobytes it's not such a big deal,
> Are you writing some sort of loop that iterates the array collection and you
> need to modify these items? If this is the case, you need to post that part
> of the code, because from your description, you code is suspect.
yes it is done inside the loop.
it looks more or less like this:

for (i=0;i<this.imagesListA.Count;i++)
{
    tempImgElement = (ImageElement) this.imagesListA[i];
    [...some code...]
    //img is 'some' rectangle

    //update ImageElement with new location
    this.imagesListA[i] = new ImageElement(tempImgElement.Bitmap,
        tempImgElement.Valid,
        tempImgElement.FileName,
        new Rectangle(img.X+this.AutoScrollPosition.X,
            img.Y+this.AutoScrollPosition.Y,
            img.Width,img.Height),
        tempImgElement.State);

    [...rest of the code...]
}

in this case i only want to update one field of a structure that looks
like this:

public struct ImageElement
{
    private Bitmap bitmap;
    private bool valid;
    private string fileName;
    private int height;
    private int width;
    private Rectangle imageRect;
    private ImageState state;

    [...all remaining stuff...]
}
Rogas69 - 21 Jul 2005 11:42 GMT
Hi,
Have a look at this article

http://www.c-sharpcorner.com/Code/2003/July/BoxNUnboxSSF.asp

If you use Whidbey and framework 2.0, consider also using generics and/or
reference types (classes) instead of structures.

Peter
SharpCoderMP - 21 Jul 2005 12:55 GMT
well generics are realy cool but i'm forced to use 1.1
this article is indead interesting but it shows what i already know. the
point is how to minimalize boxing and unbxing problems when dealing with
arraylist and complicated data that is stored inside.
ie. is there a way to avoid creation of new object/structure when i need
to modify only part of it?
it would be nice to have something that will work like this:
((myStruct) ArrayList[index]).Field = "new_data";
instead of
ArrayList[index] = new myStruct("new_data","old_data2","old_data3"...);

> Hi,
> Have a look at this article
[quoted text clipped - 5 lines]
>
> Peter
Jon Skeet [C# MVP] - 21 Jul 2005 18:14 GMT
> well generics are realy cool but i'm forced to use 1.1
> this article is indead interesting but it shows what i already know. the
[quoted text clipped - 6 lines]
> instead of
> ArrayList[index] = new myStruct("new_data","old_data2","old_data3"...);

The best way of doing that is to use a class instead.

Your structure looks like it could really do with being a class anyway,
to be honest.

Signature

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

SharpCoderMP - 22 Jul 2005 17:07 GMT
> The best way of doing that is to use a class instead.
>
> Your structure looks like it could really do with being a class anyway,
> to be honest.
well, infact it was supposed to be very simple struct... but then it
grew up alittle :)

but anyway is there any difference? when i put something inside araylist
it is stored as a "general" object. even if i turn structure into my own
class it wont change anythig because i'll still have to handle it the
same way when dealing with arraylist. and i don't want to write my own
arraylistOfMyOwnType.
Jon Skeet [C# MVP] - 22 Jul 2005 17:46 GMT
> > The best way of doing that is to use a class instead.
> >
[quoted text clipped - 4 lines]
>
> but anyway is there any difference?

Absolutely! Structures are value types, classes are reference types.

> when i put something inside araylist it is stored as a "general" object.

... which means that if you're adding a value type, you end up boxing
it, and then unboxing it when you cast it after fetching it from the
array list.

> even if i turn structure into my own
> class it wont change anythig because i'll still have to handle it the
> same way when dealing with arraylist. and i don't want to write my own
> arraylistOfMyOwnType.

No, you won't have to handle it the same way. You'll be able to do:

((MyType) ArrayList[index]).Field = "new_data";

And you'll be able to do that precisely because there won't be any
boxing/unboxing involved.

Signature

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

SharpCoderMP - 22 Jul 2005 17:59 GMT
> No, you won't have to handle it the same way. You'll be able to do:
>
> ((MyType) ArrayList[index]).Field = "new_data";
>
> And you'll be able to do that precisely because there won't be any
> boxing/unboxing involved.
oh... thanks! it looks like a'll have to change only keyword "struct"
into "class" and all will be fine :)
sometimes i think of very simple ways of doing something, then i add
lots of aditional functionality and i end up with something that work
like it should but gets too complicated. it's just because some
sollutions aren't scalable...

thanks!
SharpCoderMP - 23 Jul 2005 12:14 GMT
ok. that helped alot. i tested class instead of structure and all works
great now. thanks.
Alvin Bruney [MVP - ASP.NET] - 23 Jul 2005 03:00 GMT
He shouldn't have to bend over backwards to implement this functionality or
change structure definitions. Code like this should work
foreach(ImageList imList in new Array(ImageListA))
{
   //change items without exception being thrown
   imList.changewhateverfieldyouwanthere = "new value";
   //rest of code
}
roughly

Signature

Regards,
Alvin Bruney - ASP.NET MVP

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
Now available @ www.lulu.com/owc, Amazon.com etc

>> well generics are realy cool but i'm forced to use 1.1
>> this article is indead interesting but it shows what i already know. the
[quoted text clipped - 11 lines]
> Your structure looks like it could really do with being a class anyway,
> to be honest.
Jon Skeet [C# MVP] - 23 Jul 2005 07:50 GMT
> He shouldn't have to bend over backwards to implement this functionality or
> change structure definitions.

But it almost certainly shouldn't be a structure definition in the
first place. I can't see that the OP wants value semantics in the first
place. The whole problem is because he's got value semantics when he
wants reference semantics.

> Code like this should work
> foreach(ImageList imList in new Array(ImageListA))
[quoted text clipped - 4 lines]
> }
> roughly

What exactly do you mean by "should" here? It cerainly *won't* work if
ImageList is a value type. (ImageList isn't actually involved in this
thread as far as I can see.)

foreach iteration variables are always read-only, and even if they
weren't, the value in the ArrayList wouldn't be changed, because it
would have to be an unboxed version which would be changed.

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.