Link to home
Start Free TrialLog in
Avatar of marcgu
marcguFlag for Sweden

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marcgu

ASKER

This answer, including the links, will definately bring me forward. Thanks