Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. String Arrays

String Arrays

Scheduled Pinned Locked Moved C#
databasequestioncsharpcsslinq
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darrall
    wrote on last edited by
    #1

    Can anybody tell me what is wrong with my logic in the following code? I am trying to get a string array to take the names of projects entered one at a time from a text box. I have to be able to add to, delete from and sort the list alphabetically. I can't use SQL because most of the time this program will be used offline. When I build and run it I am getting no errors until the button click after entering the text in the box.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;

    namespace Construction_Survey_Layout
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    // PN is the index. I didn't set it to 0 because I want it to retain it's value
    // It is at 0 the first time run.
    public int PN;

        private void btnNew\_Click(object sender, EventArgs e)
        {
            ArrayList Projects = new ArrayList();            
            // The error was directed at PN here.
            Projects\[PN\] = textBoxAddNew.ToString();
    
            Projects.Add(Projects\[PN\]);
                                    
            textBoxAddNew.Clear();
    
            Projects.Sort();
    
            for(int ctr = 0; ctr <= PN; ctr++)
            {
                listBox1.Items.Add(Projects\[ctr\]);                
            }
            PN++;
            
            // The following is the error message I got after entering a Project Name in the text box
            // and clicked the Add New button:
            //Index was out of range. Must be non-negative and less than the size of the collection.
            //  Parameter name: index
        }
    
        
    }
    

    }

    P L OriginalGriffO 3 Replies Last reply
    0
    • D Darrall

      Can anybody tell me what is wrong with my logic in the following code? I am trying to get a string array to take the names of projects entered one at a time from a text box. I have to be able to add to, delete from and sort the list alphabetically. I can't use SQL because most of the time this program will be used offline. When I build and run it I am getting no errors until the button click after entering the text in the box.

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using System.Collections;

      namespace Construction_Survey_Layout
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }
      // PN is the index. I didn't set it to 0 because I want it to retain it's value
      // It is at 0 the first time run.
      public int PN;

          private void btnNew\_Click(object sender, EventArgs e)
          {
              ArrayList Projects = new ArrayList();            
              // The error was directed at PN here.
              Projects\[PN\] = textBoxAddNew.ToString();
      
              Projects.Add(Projects\[PN\]);
                                      
              textBoxAddNew.Clear();
      
              Projects.Sort();
      
              for(int ctr = 0; ctr <= PN; ctr++)
              {
                  listBox1.Items.Add(Projects\[ctr\]);                
              }
              PN++;
              
              // The following is the error message I got after entering a Project Name in the text box
              // and clicked the Add New button:
              //Index was out of range. Must be non-negative and less than the size of the collection.
              //  Parameter name: index
          }
      
          
      }
      

      }

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Darrall wrote:

      ArrayList Projects = new ArrayList();

      Why do you do that on each click? :confused:

      D 1 Reply Last reply
      0
      • D Darrall

        Can anybody tell me what is wrong with my logic in the following code? I am trying to get a string array to take the names of projects entered one at a time from a text box. I have to be able to add to, delete from and sort the list alphabetically. I can't use SQL because most of the time this program will be used offline. When I build and run it I am getting no errors until the button click after entering the text in the box.

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Collections;

        namespace Construction_Survey_Layout
        {
        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeComponent();
        }
        // PN is the index. I didn't set it to 0 because I want it to retain it's value
        // It is at 0 the first time run.
        public int PN;

            private void btnNew\_Click(object sender, EventArgs e)
            {
                ArrayList Projects = new ArrayList();            
                // The error was directed at PN here.
                Projects\[PN\] = textBoxAddNew.ToString();
        
                Projects.Add(Projects\[PN\]);
                                        
                textBoxAddNew.Clear();
        
                Projects.Sort();
        
                for(int ctr = 0; ctr <= PN; ctr++)
                {
                    listBox1.Items.Add(Projects\[ctr\]);                
                }
                PN++;
                
                // The following is the error message I got after entering a Project Name in the text box
                // and clicked the Add New button:
                //Index was out of range. Must be non-negative and less than the size of the collection.
                //  Parameter name: index
            }
        
            
        }
        

        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Darrall wrote:

        Projects[PN] = textBoxAddNew.ToString();

        The entry at PN doesn't exist yet, since Projects has a count of 0 at that line.

        Darrall wrote:

        Projects.Add(Projects[PN]);

        This line almost never makes any sense (it can, sometimes). Even less in this case, since Projects[PN] can not exist (Projects still has a count of 0) Just do Projects.Add(something) somewhere and forget about PN..? And don't forget to add the rest of the things that you want in there - the way it is now you'd have just 1 item all the time (so sorting it does nothing)

        Darrall wrote:

        ctr <= PN

        Why not just ctr < Projects.Count? Then you could eliminate that confusing PN entirely And please use a List

        D 1 Reply Last reply
        0
        • D Darrall

          Can anybody tell me what is wrong with my logic in the following code? I am trying to get a string array to take the names of projects entered one at a time from a text box. I have to be able to add to, delete from and sort the list alphabetically. I can't use SQL because most of the time this program will be used offline. When I build and run it I am getting no errors until the button click after entering the text in the box.

          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Windows.Forms;
          using System.Collections;

          namespace Construction_Survey_Layout
          {
          public partial class Form1 : Form
          {
          public Form1()
          {
          InitializeComponent();
          }
          // PN is the index. I didn't set it to 0 because I want it to retain it's value
          // It is at 0 the first time run.
          public int PN;

              private void btnNew\_Click(object sender, EventArgs e)
              {
                  ArrayList Projects = new ArrayList();            
                  // The error was directed at PN here.
                  Projects\[PN\] = textBoxAddNew.ToString();
          
                  Projects.Add(Projects\[PN\]);
                                          
                  textBoxAddNew.Clear();
          
                  Projects.Sort();
          
                  for(int ctr = 0; ctr <= PN; ctr++)
                  {
                      listBox1.Items.Add(Projects\[ctr\]);                
                  }
                  PN++;
                  
                  // The following is the error message I got after entering a Project Name in the text box
                  // and clicked the Add New button:
                  //Index was out of range. Must be non-negative and less than the size of the collection.
                  //  Parameter name: index
              }
          
              
          }
          

          }

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          There are a number of things wrong here, not just that you get an error. I assume you are new to C#?

          Darrall wrote:

          // PN is the index. I didn't set it to 0 because I want it to retain it's value // It is at 0 the first time run. public int PN;

          It will retain it's value anyway. Setting the inital vaule with

          public int PN = 0;

          makes no difference at all. Each time you construct a Form1 object you will get a new PN, that starts at zero. If you want one PN that is shared by all instances of Form1, then make it static:

          public static int PN = 0;

          Don't declare fields as public unless you need them accessable outside the class, use private. You can easily create a public property which accesses teh field, which has the advantage that you can provide a getter only, and it becomes readonly to the outside world.

          Darrall wrote:

          ArrayList Projects = new ArrayList();

          This creates Projects as local to the btnNew_Click method - i.e. it is discarded when you exit. Since you keep the PN as part of the class, I would suggest this should be also.

          Darrall wrote:

          // The error was directed at PN here. Projects[PN] = textBoxAddNew.ToString(); Projects.Add(Projects[PN]);

          textBoxAddNew.ToString() does not give you the data typed into the textbox - textBoxAddNew.Text does. Projects[PN} does not exist. If it did, then these two statements would try to add the same string twice. That is why you get the error message - you are trying to access an element that does not exist. Don't use an ArrayList - use a List<String> instead - it restricts what you can add to the list to just strings (arrayList will take Strings, ints, doubles, anything). Why do you bother with PN when the ArrayList and List both have a Count which is going to be exactly the same as PN anyway? Why sort the Projects? Just set the Sorted property of your ListBox. Why are you adding all the Projects strings to the List box each time? Better way to do all this:

          public partial class Form1 : Form
              {
              public Form1()
                  {
                  InitializeComponent();
                  }
              private List<string> Projects = new List<String>();
          
              private void btnNew\_Click(object sender, EventArgs e)
                  {
          

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          D 1 Reply Last reply
          0
          • P PIEBALDconsult

            Darrall wrote:

            ArrayList Projects = new ArrayList();

            Why do you do that on each click? :confused:

            D Offline
            D Offline
            Darrall
            wrote on last edited by
            #5

            Sorry...I wasn't very explicit there. This program is for a construction survey program. Somebody first using this program would enter the name of the first project he worked on. Then as he went along each time he got a project he would add a new one so in reality the click event occurs once every few weeks but the data has to remain in the program.

            P 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              There are a number of things wrong here, not just that you get an error. I assume you are new to C#?

              Darrall wrote:

              // PN is the index. I didn't set it to 0 because I want it to retain it's value // It is at 0 the first time run. public int PN;

              It will retain it's value anyway. Setting the inital vaule with

              public int PN = 0;

              makes no difference at all. Each time you construct a Form1 object you will get a new PN, that starts at zero. If you want one PN that is shared by all instances of Form1, then make it static:

              public static int PN = 0;

              Don't declare fields as public unless you need them accessable outside the class, use private. You can easily create a public property which accesses teh field, which has the advantage that you can provide a getter only, and it becomes readonly to the outside world.

              Darrall wrote:

              ArrayList Projects = new ArrayList();

              This creates Projects as local to the btnNew_Click method - i.e. it is discarded when you exit. Since you keep the PN as part of the class, I would suggest this should be also.

              Darrall wrote:

              // The error was directed at PN here. Projects[PN] = textBoxAddNew.ToString(); Projects.Add(Projects[PN]);

              textBoxAddNew.ToString() does not give you the data typed into the textbox - textBoxAddNew.Text does. Projects[PN} does not exist. If it did, then these two statements would try to add the same string twice. That is why you get the error message - you are trying to access an element that does not exist. Don't use an ArrayList - use a List<String> instead - it restricts what you can add to the list to just strings (arrayList will take Strings, ints, doubles, anything). Why do you bother with PN when the ArrayList and List both have a Count which is going to be exactly the same as PN anyway? Why sort the Projects? Just set the Sorted property of your ListBox. Why are you adding all the Projects strings to the List box each time? Better way to do all this:

              public partial class Form1 : Form
                  {
                  public Form1()
                      {
                      InitializeComponent();
                      }
                  private List<string> Projects = new List<String>();
              
                  private void btnNew\_Click(object sender, EventArgs e)
                      {
              
              D Offline
              D Offline
              Darrall
              wrote on last edited by
              #6

              Thanks very much for your help. Yes...I am new to C#. I used to program years ago but the last language I used was QB45 and I'm now trying to write my old programs in C#. Thanks again :)

              1 Reply Last reply
              0
              • L Lost User

                Darrall wrote:

                Projects[PN] = textBoxAddNew.ToString();

                The entry at PN doesn't exist yet, since Projects has a count of 0 at that line.

                Darrall wrote:

                Projects.Add(Projects[PN]);

                This line almost never makes any sense (it can, sometimes). Even less in this case, since Projects[PN] can not exist (Projects still has a count of 0) Just do Projects.Add(something) somewhere and forget about PN..? And don't forget to add the rest of the things that you want in there - the way it is now you'd have just 1 item all the time (so sorting it does nothing)

                Darrall wrote:

                ctr <= PN

                Why not just ctr < Projects.Count? Then you could eliminate that confusing PN entirely And please use a List

                D Offline
                D Offline
                Darrall
                wrote on last edited by
                #7

                Thanks for your answer :)

                1 Reply Last reply
                0
                • D Darrall

                  Sorry...I wasn't very explicit there. This program is for a construction survey program. Somebody first using this program would enter the name of the first project he worked on. Then as he went along each time he got a project he would add a new one so in reality the click event occurs once every few weeks but the data has to remain in the program.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Yes, but why create a new empty list each time?

                  D 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Yes, but why create a new empty list each time?

                    D Offline
                    D Offline
                    Darrall
                    wrote on last edited by
                    #9

                    Yes I caught that...Thanks :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups