Understanding New on the List-object

  • Thread starter Thread starter Morten Snedker
  • Start date Start date
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
 
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
 
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...
 
Back
Top