Like Tree2Likes
  • 2 Post By DanFraser

passing a arraylist to a function

This is a discussion on passing a arraylist to a function within the C# Programming forums, part of the General Programming Boards category; Code: using System; using System.Collections; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Bangalore
    Posts
    3

    passing a arraylist to a function

    Code:
    using System;
    using System.Collections;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {        
          class Program
          {        
               static void Main(string[] args) 
               {           
                      int n;    
                      Console.WriteLine("Enter the no of students");  
                      n = int.Parse(Console.ReadLine()); 
                      ArrayList name=new ArrayList();   
                      for(int i=0;i<n;i++)         
                     {               
                           Console.WriteLine("Enter the name of the "+i+"th students");  
                           name.Add(Console.ReadLine());   
                     }   
                     Stu_rec(name);  
                     Console.ReadLine();  
                }
                static void Stu_rec(ArrayList names) 
                {        
                       foreach (string n in names)   
                              Console.WriteLine(n); 
                 }    
          }
    }
    When i try to compile, it gives a error saying "the parameter array must be a single dimensional array"
    what is the problem here.?
    Last edited by vn.shyamkumar; 03-10-2012 at 09:35 PM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    I don't. Is there other code that you aren't showing us that you are compiling along with this?

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    For starters, forget about ArrayList, it's so old it's unreal. Use a generic list for string. The for loop would still be the same but all you would change is

    Code:
    // ArrayList name = new ArrayList();
    // is now
    List<string> name = new List<string>();
    and your method should be

    Code:
    // the first line used to be
    // static void Stu_rec(ArrayList names)
    // but is now:
    static void Stu_rec(List<string> names) 
    {        
      foreach (string n in names)
      {
        Console.WriteLine(n);
      }
    }
    A few tips on style though, if you're in Visual studio, regularly hit Ctrl+E then press D. This will auto align your indentations on your code. If you're recently new to programming always wrap your ifs foreachs and other things in the curly brackets, makes things easier to read.
    itsme86 and VirtualAce like this.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ArrayList Basics - Example
    By wbeasl in forum C# Programming
    Replies: 2
    Last Post: 12-26-2008, 03:41 PM
  2. Trouble using ArrayList
    By stevespai in forum C# Programming
    Replies: 12
    Last Post: 07-31-2006, 11:17 AM
  3. ArrayList question.
    By antex in forum C# Programming
    Replies: 4
    Last Post: 05-19-2006, 07:49 AM
  4. Getting an element from an ArrayList
    By osal in forum C# Programming
    Replies: 4
    Last Post: 08-03-2005, 09:34 AM
  5. 2 Dimensional ArrayList
    By osal in forum C# Programming
    Replies: 3
    Last Post: 08-02-2005, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21