1

I have 2 functions, one that gets a name and the other that is being set up to Delete from the list of name. There are no compliation errors, but my DeleteName doesn't display anything. Probally a simple fix, what did I overlook?

    public static List<string> GetName(List<string> aString)
    {
        aString = new List<string>();
        localList.Add("cat");
        localList.Add("apple");
        localList.Add("bass");
        localList.Add("dog");
      return aString;
    }
    public static List<string> DeleteName(List<string> aString)
    {
        aString = new List<string>();
        GetName(aString);
        foreach (string x in aString)
        {
            Console.WriteLine(x);
        }
        return aString;
    }
4
  • 2
    I'm very sure that this code does not compile without errors. localList is not defined. Commented Apr 19, 2011 at 18:06
  • Hmmm why are you creating aString at DeleteName function? you are actually receiving that as a list... Commented Apr 19, 2011 at 18:07
  • 2
    Is that your full DeleteName method? It doesn't really do what the name says it does? Commented Apr 19, 2011 at 18:09
  • 1
    this whole court is out of order! Commented Apr 19, 2011 at 18:44

6 Answers 6

5

You're passing aString to GetName but not doing anything with the resulting List. In GetName() there seems to be no reason to pass in aString since you're setting it to a new instance. You're also not doing anything with the aString passing to DeleteName. If you wanted to combine the first collection with the second you could try something like this:

public static List<string> GetName(List<string> aString)
{
    List<string> localList= new List<string>(aString);

    localList.Add("cat");
    localList.Add("apple");
    localList.Add("bass");
    localList.Add("dog");

    return localList;
}

public static List<string> DeleteName(List<string> aString)
{
    aString = GetName(aString);
    foreach (string x in aString)
    {
        Console.WriteLine(x);
    }
    return aString;
}
Sign up to request clarification or add additional context in comments.

2 Comments

What would be the benefit of passing aString here?
Depends what the OP is trying to do, but I'm guessing OP is just testing things out
4

It seems odd that you are passing the List and then returning it as well. Try changing your code to this:

public static List<string> GetName()
{
    List<string> aString = new List<string>();
    aString.Add("cat");
    aString.Add("apple");
    aString.Add("bass");
    aString.Add("dog");
  return aString;
}
public static List<string> DeleteName()
{
    List<string>  aString = GetName();
    foreach (string x in aString)
    {
        Console.WriteLine(x);
    }
    return aString;
}

As @hunter points out you could do this without declaring your list in GetName. Code:

    public static void GetName(List<string> aString)
    {
        aString.Add("cat");
        aString.Add("apple");
        aString.Add("bass");
        aString.Add("dog");
    }
    public static List<string> DeleteName()
    {
        List<string> aString = new List<string>();
        GetName(aString);
        foreach (string x in aString)
        {
            Console.WriteLine(x);
        }
        return aString;
    }

Also, as @Dustin Laine pointed out but then deleted, you will get the same result if you do not set aString to a new object in GetName(). Code:

public static List<string> GetName(List<string> aString)
{
    //aString = new List<string>();
    aString.Add("cat");
    aString.Add("apple");
    aString.Add("bass");
    aString.Add("dog");
    return aString;
}
public static List<string> DeleteName(List<string> aString)
{
    aString = new List<string>();
    GetName(aString);
    foreach (string x in aString)
    {
        Console.WriteLine(x);
    }
    return aString;
}

This still seems like a very strange way to write your code and I would not recommend it.

7 Comments

why declare aString in GetName() at all?
This will compile without errors, but the console will still not display anything.
@jpavlov, have you tested this? Both of my examples above display what you would expect.
@hunter, I guess you don't have to. Do you know if the second version will be more efficient?
@Abe I think that's more what the OP is going for but I would just make GetName() return a list with no parameters and aString in DeleteName would just be set to the result of GetName()
|
3

Changing it to

public static List<string> GetName(ref List<string> aString)

should fix it. aString in GetName is a pointer to the list created in DeleteName. On line 3 of that method, you are setting aString to point to a different location, and then working with that location. You then return it, but don't use it. This leaves the list created in DeleteName unchanged. Another possible fix since you do return the list is to change DeleteName's call to be

aString = GetName(null);

But that's weird as well, since the list is never actually used in GetName.

Comments

0

Did you mean to make them ref parameters? If you don't want that, it would be aString = GetName(aString) instead of GetName(aString) by itself.

Comments

0

because you are writing the foreach result. You probably will do:

    foreach(string x in aString)
{
      astring.Add(x);
}
Console.WriteLine(aString);

// or 

return aString;

Comments

0

You're not storing the result of aString. You should do something like aString = GetName(aString).

There are a lot of other things wrong with this code as posted, though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.