0

I'm doing an assignment where the I should store some string data in arrays. Each new string should be stored in a separate array, that's why I need a method that returns a new array with a name given from a user. What I want is a new array with a uniq name each time I call this method.

On the 2d line I get an error: A local or parameter named '_arrayNameFromUser' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter test

So how do I create a new array with a user-specified name?

A local or parameter named '_arrayNameFromUser' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter   test
static string[] NewArary(string arrayNameFromUser)
    {            
        string[] arrayNameFromUser = new string[2];

        return arrayNameFromUser;
    }
4
  • 3
    You already have variable named _arrayNameFromUser in the method body - it's a method parameter. So you cannot declare another variable with exactly same name. Commented Apr 13, 2017 at 8:20
  • 1
    Also note, that in mode code guidelines, _ would indicate a private instance field and not a local variable or method parameter Commented Apr 13, 2017 at 8:21
  • @Icepickle changed that, thanks Commented Apr 13, 2017 at 8:39
  • 2
    Can you add the precise description of your assignment? I think (like some of the others) there is a misconception here. Commented Apr 13, 2017 at 9:02

2 Answers 2

4

Give it a different name than the parameter:

static string[] NewArary(string name)
{            
    string[] ret = new string[2];
    ret[0] = name;
    return ret;
}

Note that C# naming conventions only use _underscorePrefixed names for fields, not parameters or locals, and often it's restricted to only static fields too (as instance fields can be accessed with this.), see the below example of conventional capitalization of identifiers:

public class TypeName
{
    private static readonly String _staticField = "foo";

    private Int32 field;

    public Int32 Property { get { return this.field; } }

    public const String PublicConstant = "bar";

    public void Method(Uri parameter) {

        Int32 local = 123;
    }
}

(Though my use of Int32 instead of int and String instead of string is a controversial stance :D)

Sign up to request clarification or add additional context in comments.

2 Comments

This is wrong because it will simply return an array with name "ret" and with first element name (containing what a user have entered). What I want is a new array with a uniq name each time I call this method.
@Gusto You cannot do that - compiled code is static and fixed, including identifiers. You cannot parameterise variable names like that. I think you misunderstand your programming assignment.
2

You've got some confusion here between compile time/runtime. You don't create new variables at runtime.

I suspect that what you need is a Dictionary<string,string[]>. Then, instead of calling NewArary (sic), you'd instead do:

Dictionary<string,string[]> arrays = new Dictionary<string,string[]>();

//NewArary("abc");
arrays.Add("abc", new string[]{});

And then later you'd obtain the abc array by doing:

var thatArray = arrays["abc"];

2 Comments

I cannot use dictionary, I MUST use arrays ONLY (this is an academic assignment)
@Gusto - then you've somewhere misunderstood your assignment since you can't create new "variables" at runtime.

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.