开发者

Reading values out from an ArrayList in an independent function

I am having troubling reading my valu开发者_Python百科es out of an ArrayList. The compiler goes into the ReadOutFromArray function, but skips the Console.WriteLine(st)? Can anyone tell me where I went wrong. Been on it for a couple of hours chasing my tail. Thanks.

using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace BoolEx
{
    class Program
    {
        static void Decision(ArrayList decis)
        {
            bool ans = true;

            decis = new ArrayList();
            //ArrayList aList = new ArrayList();
            while (ans)
            {
                Console.WriteLine("1=True 0=False");
                int x = Int32.Parse(Console.ReadLine());

                if (x == 1)
                {
                    ans = true;
                }
                else
                {
                    ans = false;
                }

                if (ans == true)
                {
                    ReadInArray(decis);
                }
                else
                {

                    ReadOutArray(decis);
                }
            }
        }
        static void ReadInArray(ArrayList f)
        { 
            f= new ArrayList();
            Console.WriteLine("Enter in a name");
            f.Add(Console.ReadLine());
        }

        static void ReadOutArray(ArrayList d)
        {
            d = new ArrayList();
            ReadInArray(d);

            foreach (string st in d)
            {
                Console.WriteLine(st);
            }
        }

        static void Main(string[] args)
        { 
            ArrayList g = new ArrayList();
            Decision(g);
        }
    }
}


The problem is your ReadInArray method:

static void ReadInArray(ArrayList f)
{ 
    f= new ArrayList();
    Console.WriteLine("Enter in a name");
    f.Add(Console.ReadLine());
}

In the first line of the method, you're basically saying, "I don't care what ArrayList reference was passed in - I'm going to overwrite the local variable f with a reference to a new ArrayList."

I suspect you meant something like this:

static void ReadInArray(ArrayList f)
{ 
    f.Clear();
    Console.WriteLine("Enter in a name");
    f.Add(Console.ReadLine());
}

If you don't understand why that changes things, see my parameter passing article.

Other things you should consider:

  • If you're only going to read a single line, why not use something like this:

    static string ReadNameFromUser()
    {
        Console.WriteLine("Enter in a name");
        return Console.ReadLine();
    }
    

    The same sort of thing occurs elsewhere. Don't try to use collections for all your input and output. Returning a value is much clearer than populating a list which is passed into the method.

  • Given that you can obviously refer to generic collections (given your using directives) you should really consider using List<string> instead of ArrayList

  • Code like this:

    if (x == 1)
    {
        ans = true;
    }
    else
    {
        ans = false;
    }
    

    ... would be better written as

    ans = (x == 1);
    

    (The brackets are optional, but help readability.)

  • Code like this:

    if (ans == true)
    

    is better written as:

    if (ans)
    


Although I do agree with everything Skeet has mentioned, it appears the poster is trying to understand some things and I think Jon might have missed that.

First if all you want to do is fill a list and print it try this:

static void (main)
 {
   ArrayList l = new ArrayList();
   FillMyList(l);
   DisplayMyList(l);
 }

public static void FillMyList(ArrayList temp)
 {
  for(int i = 0; i < 10; i++)
     temp.Add(i);
 }

public static void DisplayMyList(ArrayList temp)
 { 
 foreach(int i in temp)
  Console.WriteLine(i);
 }

Second thing is take what Jon Skeet has mentioned and definately understand some things. Booleans are just true / false (unless you introduce the nullable types) but for now keep it simple. ArrayList is really old school, it kind of suffers like the HashTable in that you can easily run into trouble adding different data types into the object (read up on boxing of data types and unboxing). Finally, you should really replace anything with this System.Collection.ArrayList to System.Collections.Generic.List.

The list class is a generic class and is made available so that you don't have to deal with the issues that you could encounter when dealing with array lists or hash tables.

Edit

I noticed you were asking users to add items to the list. You can do this using a do while loop instead of the for loop I posted, something to this effect (note i have not tested any of this):

public static void FillMyList(ArrayList temp)
 {
  char c='y';

  do {
     Console.WriteLine("Enter a value");
     int x = Int32.Parse(Console.ReadLine());  
     temp.Add(x);
     Console.WriteLine("Continue adding numbers to list, if so type y");
     char c = Console.ReadLine();
  }while(c=='y' || c=='Y');
 }

Again I am just giving you examples here, you will have to handle user input in case someone doesn't enter the correct information, exceptions, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜