!!!Question about Programmatically adding items to

ateltara

New Member
I need to fill a dropdownlist with 1 to 60. So I did it by doing the following code:<BR><BR>for(int i=0; i <=60; i++)<BR>DropdownList1.Items.Add(new ListItem(i.ToString(), i.ToString()))<BR><BR>However, I was thinking this probally is not good idea to use "new ListItem" each time. So I replace the above with the following:<BR><BR>ListItem li = new ListItem();<BR>for(int i=0; i <=60; i++)<BR>{<BR>li.Text = i.ToString();<BR>li.Value = http://aspmessageboard.com/archive/index.php/i.ToString();<BR>DropdownList1.Items.Add(li)<BR>}<BR>But the result turned to be all the items in the list are 60. Is there anything in the second code wrong? I have seen people using the first code all the time, but bot the second code. Does anybody know why? Could anybody give me some ideas why the second code doesn't work?<BR><BR>What's the life cycle of the new item when using<BR>DropdownList1.Items.Add(new ListItem(i.ToString(), i.ToString()))<BR><BR>The last IMPORTANT question is: Does using "new listitem" will cause performance issue, such as more memory allocation, etc?<BR><BR>I would really appreciate any ideas!<BR><BR>Thank you in advance!<BR>The first approach is fine. The second approach IS working: it is doing exactly what you tell it to do. This is what you are telling it to do:<BR><BR>1. Create a ListItem<BR>2. Assign it text and value<BR>3. Add it to the DDL<BR>4. Change the text and value of the SAME ListItem<BR>-- The ListItem in the DDL also changes: it references the one and only ListItem that you are operating on<BR>5. Add it to the DDL<BR>6. Change the values<BR>-- both references in the DDL also change<BR>7. Add it...<BR>ETC....<BR><BR>In short, all of your Items in the DDL have a reference to one, single ListItem. If you change the ListItem, all references change with it....that doing<BR> DropdownList1.Items.Add( ) <BR>some how creates a *NEW* object and shoves it into the collection.<BR><BR>Nope. Doesn't happen in *ANY* of the standard collection classes. And shouldn't.<BR><BR>All you are doing is putting a *REFERENCE* to the given object into the collection. You are *NOT* copying any objects. <BR><BR>And this is as it should be: In general, objects should ONLY be created by specific actions, the most common of which is of course operator New. (In point of fact, operator New() is the *only* way to create new objects, but many classes use a private New() and then use a static "factory" method to create new objects; this is usually because they want more control of how the objects are created and/or they actually only create subclasses of the class you are using and/or they want to create singletons.)<BR><BR>So... You really don't have any choice. You *will* use<BR> New ListItem( )<BR>or it won't work.<BR><BR>You asked about the life cycle of ListItem instances created via<BR> DropdownList1.Items.Add(new ListItem(i.ToString(), i.ToString()))<BR>The objects thus created will exist so long as there are any references to them. Same as any other object. And what references are there to them? Why the reference that is in the array that underlies the Items collection implementation, of course. So when that array no longer has any references, it can be garbage collected and so can the ListItem objects. And when do references to the array go away? Why when there are no longer any references to the DropdownList object that references the array. And so when does it *ALL* go away??? Possibly sooner, but for certain when the ASP.NET "page" (which is actually just another instance, of a particular kind of page class) completes delivering the Response to the browser and is thrown away by the ASP.NET "engine".<BR><BR>You wrote:<BR><BR>In short, all of your Items in the DDL have a reference to one, single ListItem. If you change the ListItem, all references change with it.<BR><BR>Ummmm...I know what you mean, but the wording is...well, sloppy.<BR><BR>The real point is that "all references" do *NOT* change! They *ARE* all referring to that same one and only object.<BR><BR>What you mean is that any code that accesses data (or methods) on that one and only object will see that the data has changed, all in identically the same way.<BR><BR>Pedantic, I know. But I think important for this concept. No?<BR>Thank you for your reply!<BR><BR>So, to complish filling the dropdownlist with 1 to 60, I should stick to the first approach, right? Is that the only way to programmatically add items into dropdownlist?<BR><BR>Since I have been accused of using new ListItem each time by my co-worker. His reason is that it could affect the memory performance, in general concept for using new an object. I feel very uncomfortable about it. I just want to find out if there is a second way to do it. Do you think there is a better way to do it, or the first approach is the only way to do it? <BR><BR>Thank you very much!<BR><BR><BR><BR>Yes, that's what I said :)<BR><BR>I know what you mean and I know that you know what I mean. I was just trying to say that "Even thogh you see 60 Items in the collection, they are all a reference to the same object."<BR><BR>RE: "Pedantic, I know"<BR>We can always count on you, Bill :)First way is absolutely fine!...is to stop doing this with ASP.NET code.<BR><BR>If you know the list will always be from 1 to 60, then stop using an <asp:dropdownlist> and just use an ordinary HTML <select> and hand code the 60 options:<BR><BR><SELECT name="xxx"><BR><OPTION>1<OPTION>2<OPTION>3<OPTION>4 ...<BR><OPTION>60<BR></SELECT><BR><BR>Yeah, you really do *not* need more than that. Incredibly much faster, of course.<BR><BR>Just means that instead of USING the DropDownList object, you will get the value of the selected option using<BR> Request.Form("xxx").Value<BR><BR>Ask your co-worker how he'she does it. No matter how they do it, new() will be used explcitely or implicitely - there's jsut no way around it....and say that if you aren't willing to break out of using only <asp:dropdownlist> it's not only fine, it's the only way.<BR><BR>The second approach is what he thinks how it works. But I don't think he has ever tried that, because he didn't do any UI implementation that has the same requirement. That's why it frustrated me......that he's a doofus and you don't need to listen to him, any more.<BR><BR>This has *NOTHING* to do with UI. It is how *all* collections work in most languages. In fact, aside from *FIXED SIZE* arrays in C and C++, I can't think of any modern language where you *can* do it differently.<BR><BR>Oh, in any of these languages, you *could* implement "copy the object when it is added to the collection" semantics for your own collections. But it would be totally unexpected by 98% of software professionals. And the other 2% would be like your "friend". (And of course the "copy the object" code has to be implemented by using New to create the new copy.)<BR><BR>Thanks, Bill! And, thanks MetaDjinn! <BR>I am so grateful that I can always find an answer at this forum. <BR>...then you could create the options in a JS loop:<BR><BR> <SCRIPT><BR> document.write('<SELECT Name="xxx">');<BR> for ( var i = 1; i <= 60; ++i )<BR> document.write( '<OPTION>' + i + '</OPTION>' );<BR> document.write('</SELECT>');<BR> </SCRIPT><BR><BR>What about the overload that just takes a string.....<BR><BR>http://msdn2.microsoft.com/en-us/library/33w94aaa.aspxwhen the page is posted back, does'n it? If I need to maintain the selected value. <asp:DropDownList> is my only choice, right?<BR><BR>INLINE, not in code behind:<BR><BR><SELECT Name="foobar"><BR><%<BR>// next line inadequate; doesn't handle first-time-on-page; but you get idea<BR>int prior = (int) Request.Form("foobar").Value;<BR>int foo;<BR>for ( foo = 1; foo <= 60; ++foo )<BR>{<BR> String sel = ( foo == prior ) ? "selected" : "";<BR>%><BR> <OPTION <%=sel%> ><%=foo%></OPTION><BR><%<BR>}<BR>%><BR></SELECT><BR><BR>************<BR><BR>But honestly, unless this is on a web page used thousands of times per hour, it's not worth this hassle. Stick with the somewhat klunky but adequate ASP.NET way.<BR><BR>Quote:<BR><BR>Use the Add method to append a ListItem to the end of the collection. This implementation of the method creates a ListItem to represent the text specified by the item parameter. This ListItem is then appended to the collection.<BR><BR>Less code, but performance should be essentially identical.<BR>Yup, even the shortcut/direct method creates an object :)<BR><BR>If performance/bandwidth is critical, the client-side javascript to create the options could be the best thing. It could be encapsulated in a custom server control.
 
Top