locked
use the "new" keyword to create an object instance C#

    Question

  •  

    Hi there
    I am having a problem with this I am loosing memory I dont remember how to do this here is the error that is giving me

    Value cannot be null.
    Parameter name: item

    use the "new" keyword to create an object instance C#

    and these are the 2 lines of code that I am having problem with just trying to populate 2 listbox with array

    SongName = new string[100];

    url = new string[100];

    songListBox1.Items.AddRange(SongName);// add array  
    songUrlListBox.Items.AddRange(url);// add array  
     


    Thanks in advance

    Tuesday, December 02, 2008 7:28 PM

Answers

  • superl,

    When declared SongName and url's elements have a null value.  Use String.Empty to give them an initial value:

    1 using System;  
    2 using System.Drawing;  
    3 using System.Windows.Forms;  
    4  
    5 public class frm : Form  
    6 {  
    7     private static string[] SongName;  
    8     private static string[] url;  
    9     private static ListBox songListBox1 = new ListBox();  
    10     private static ListBox songUrlListBox = new ListBox();  
    11  
    12     public frm()  
    13     {  
    14         Initalize();  
    15     }  
    16  
    17     private void Initalize()  
    18     {  
    19         SongName = new string[100];  
    20         url = new string[100];  
    21  
    22         for (int i = 0; i < SongName.Length; i++)  
    23         {  
    24             SongName[i] = String.Empty;  
    25             /* Since SongName.Length = url.Length we can also assign url's
    26             value to String.Empty as well.*/ 
    27             url[i] = String.Empty;  
    28         }  
    29  
    30         songListBox1.Items.AddRange(SongName);// add array     
    31         songUrlListBox.Items.AddRange(url);// add array     
    32  
    33         songListBox1.Location = new Point(0, 0);  
    34         songUrlListBox.Location = new Point(songListBox1.Width, 0);  
    35  
    36         this.Controls.Add(songListBox1);  
    37         this.Controls.Add(songUrlListBox);  
    38     }  
    39  
    40     [STAThread]  
    41     static void Main()  
    42     {  
    43         Application.EnableVisualStyles();  
    44         Application.Run(new frm());  
    45     }  
    46


    Now, I am not sure why you want a ListBox of Empty members but there it is.  If you have any issue feel free to post.

    Best Regards,

    Adjutor


    • Proposed as answer by Adjutor Tuesday, December 02, 2008 9:08 PM
    • Marked as answer by superl Wednesday, December 03, 2008 7:02 PM
    Tuesday, December 02, 2008 9:04 PM

All replies

  • superl,

    When declared SongName and url's elements have a null value.  Use String.Empty to give them an initial value:

    1 using System;  
    2 using System.Drawing;  
    3 using System.Windows.Forms;  
    4  
    5 public class frm : Form  
    6 {  
    7     private static string[] SongName;  
    8     private static string[] url;  
    9     private static ListBox songListBox1 = new ListBox();  
    10     private static ListBox songUrlListBox = new ListBox();  
    11  
    12     public frm()  
    13     {  
    14         Initalize();  
    15     }  
    16  
    17     private void Initalize()  
    18     {  
    19         SongName = new string[100];  
    20         url = new string[100];  
    21  
    22         for (int i = 0; i < SongName.Length; i++)  
    23         {  
    24             SongName[i] = String.Empty;  
    25             /* Since SongName.Length = url.Length we can also assign url's
    26             value to String.Empty as well.*/ 
    27             url[i] = String.Empty;  
    28         }  
    29  
    30         songListBox1.Items.AddRange(SongName);// add array     
    31         songUrlListBox.Items.AddRange(url);// add array     
    32  
    33         songListBox1.Location = new Point(0, 0);  
    34         songUrlListBox.Location = new Point(songListBox1.Width, 0);  
    35  
    36         this.Controls.Add(songListBox1);  
    37         this.Controls.Add(songUrlListBox);  
    38     }  
    39  
    40     [STAThread]  
    41     static void Main()  
    42     {  
    43         Application.EnableVisualStyles();  
    44         Application.Run(new frm());  
    45     }  
    46


    Now, I am not sure why you want a ListBox of Empty members but there it is.  If you have any issue feel free to post.

    Best Regards,

    Adjutor


    • Proposed as answer by Adjutor Tuesday, December 02, 2008 9:08 PM
    • Marked as answer by superl Wednesday, December 03, 2008 7:02 PM
    Tuesday, December 02, 2008 9:04 PM
  • Thank you
    • Marked as answer by superl Tuesday, December 02, 2008 9:19 PM
    • Unmarked as answer by superl Wednesday, December 03, 2008 7:02 PM
    Tuesday, December 02, 2008 9:19 PM
  • superl,

    If you please, can you mark my post as the "Answer", you marked your own?

    Regards,

    Adjutor
    Tuesday, December 02, 2008 9:24 PM
  • Sorry and again thanks
    Wednesday, December 03, 2008 7:03 PM