Nolonar
Nolonar

Reputation: 6122

Visual Studio Bug?

I'm encountered something weird, and I'm not sure if it's a bug in Visual Studio, or if maybe my ignorance is playing tricks on me.

I have two private class variables:

class MyClass
{
    private MyList<A> aList;
    private MyList<B> bList;
    [...]

And somewhere along the code, I'm using those variables for the first time.

    public void MyMethod()
    {
        object[] generatorOutput = Generator.Generate(args);
        aList = (MyList<A>)generatorOutput[0];
        bList = (MyList<B>)generatorOutput[1];
        [...]

Yet Visual Studio tells me, that bList is wrong:

Cannot use local variable 'bList' before it is declared.
The declaration of the local variable hides the Field 'MyNameSpace.MyClass.bList'.

I don't really understand what Visual Studio means. I don't want bList to be local, and it's not supposed to be hiding anything.

If it helps: bList was originally called cList and was a MyList<C> before I decided, that a MyList<B> was more than enough. The error message only appeared after renaming the variable and changing its type. The generatorOutput is always being casted into the correct type, by the way.

So, is this a bug, or am I missing something obvious? I've already tried compiling the code, rewriting the line and even restarting Visual Studio without success...

Upvotes: 1

Views: 243

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

I assume that your MyMethod continues like this:

public void MyMethod()
{
    object[] generatorOutput = Generator.Generate(args);
    aList = (MyList<A>)generatorOutput[0];
    bList = (MyList<B>)generatorOutput[1];

    // ...

    var bList = new MyList<B>();  // <---

Upvotes: 6

Related Questions