Understanding New on the List-object

M

Morten Snedker

I find it difficult to understand the difference between

Me.cmbChoose.Items.Add(New ListItem("hi"))
and
Me.cmbChoose.Items.Add("hi")

How will my application work different if I use one or another? Is one
of them more correct than the other?

Regards /Snedker
 
K

Karl Seguin [MVP]

They are just overloads.

the .Add("hi") will internally call the add (new ListItem("hi")).

It's just there for your convinience.

You would normally use the new ListItem when you also want to specify a
value, so you'd do new ListItem("Hi", "3");

One isn't better than the other.

Karl
 
M

Mark Rae

I find it difficult to understand the difference between

Me.cmbChoose.Items.Add(New ListItem("hi"))
and
Me.cmbChoose.Items.Add("hi")

How will my application work different if I use one or another? Is one
of them more correct than the other?

..NET supports the concept of method "overloading" - if you don't know what
that is, Google it...

In this case, the Add method of the Items collection of a DropDownList has
two overloads - one which requires a ListItem object and the other which
requires a string to be passed into it as an argument.

ASP.NET takes the DropDownList object and renders it as an HTML <select>
when it streams the markup down to the client.

When an Item is added to the Items collection by passing in a string,
ASP.NET renders an HTML <option> where the internal value and visible text
are the value of the string.

When an Item is added to the Items collection by passing a ListItem object,
ASP.NET renders an HTML <option> where the value is ListItem's value
property and the visible text is the ListItem's text property.

Once your page has been rendered to the browser, do a View Source and you'll
see the difference straightaway...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top