Whats the differenct between "items.add ()" and "Items.Add(New ListItem())"

Hi!

I am really struggeling to understand when I need and when I can ignore making new instances of an object.

In the first code example below, example A, I, use the   Items.Add(New ListItem()), but I get the same expected result if I let it out. Why?



The only difference between Example A and B are these rows

A: listbox1.Items.Add(New ListItem("Carbon"))
        listbox1.Items.Add(New ListItem("Oxygen"))

B: Listbox1.Items.Add("Carbon")
        Listbox1.Items.Add("Oxygen")


However, if I try to add both a text and a value, I, can only do so by using


 ListBox1.Items.Add(New ListItem("Carbon", "C"))
    ListBox1.Items.Add(New ListItem("Oxygen", "O"))

This code will not work

 ListBox1.Items.Add("Carbon", "C")
    ListBox1.Items.Add("Oxygen", "O")


I will be greatful for an explaination of this individual case but also for rules of thumbs that I can use. I have a lot of books but maybe there are better ones, that some one can give me a recommendation of.


Example A


 <%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">
    <title> CheckBoxList Constructor Example </title>
<script runat="server">

    Protected Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
        listbox1.Items.Add(New ListItem("Carbon"))
        listbox1.Items.Add(New ListItem("Oxygen"))
    End Sub

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> CheckBoxList Constructor Example </h3>
       Select items from the CheckBoxList.

       <asp:ListBox ID="listbox1" runat="server"></asp:ListBox>
     
      <br /><br />

    

      <br /><br />
       <asp:Button ID="Button1" runat="server" Text="Button" />

   </form>

</body>

</html>

Open in new window


Example B

<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">
    <title> CheckBoxList Constructor Example </title>
<script runat="server">

    Protected Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
        Listbox1.Items.Add("Carbon")
        Listbox1.Items.Add("Oxygen")
    End Sub

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> CheckBoxList Constructor Example </h3>
       Select items from the CheckBoxList.

       <asp:ListBox ID="Listbox1" runat="server"></asp:ListBox>
     
      <br /><br />

    

      <br /><br />
       <asp:Button ID="Button1" runat="server" Text="Button" />

   </form>

</body>

</html>

Open in new window

marcguAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You need to understand method overloads. Basically what "overloading" means is that you have two (or more) different functions/methods that both have the same name, but which take different parameters--return types are inconsequential with regard to overloading. The Add method of the ListBox class is one such example. It has two overloads: one which accepts a new ListItem instance, and one which accepts a string. It does not have an overload which accepts two strings, which is what you are attempting to do in your "This code will not work" section above. You have to pay attention to the parameters required by any method that you attempt to call.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marcguAuthor Commented:
This answer, including the links, will definately bring me forward. Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.