Fake Name
Fake Name

Reputation: 119

Can't use declared dictionary inside the same class?

Ok so, I can't figure out what the hell is going on.

I declared and initialised a dictionary:

public Dictionary<byte, Color> blobType = new Dictionary<byte, Color>();

But I can't use it inside the class, intelliscence wont show it either. I get errors if I try to use it like this:

blobType.add(1, Color.White);

Or if I don't initialize it and try to later:

    public Dictionary<byte, Color> blobType;
    blobType = new Dictionary<byte, Color>();

Still can't use it, its like it doesn't see the blobType that it is there.

I tried renaming the variable, do it in VS2012, still the same thing happens. So it can access it outside of the class when the class is an object in another class. But VS2010 C# Express refuses to acknowledge its existence in the class I declared it in. What's going on?

As requested, entire class:

namespace blob
{
    class Blob
    {
        public Texture2D texture;

        public Dictionary<byte, Color> blobType = new Dictionary<byte, Color>();
        blobType.add(1, Color.White);

        public Vector2 position;

        private float scale = 1;
        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        public Blob(Texture2D texture, float scale)
        {
            this.texture = texture;
            this.Scale = scale;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, null, Color.White, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
        }
    }
}

EDIT2: Capitalising Add, same thing. Errors:

Error   1   Invalid token '(' in class, struct, or interface member declaration C:\Users\Iurie\Documents\Visual Studio 2010\Projects\blob\blob\blob\Blob.cs 20  21  blob

Error   2   Invalid token ')' in class, struct, or interface member declaration C:\Users\Iurie\Documents\Visual Studio 2010\Projects\blob\blob\blob\Blob.cs 20  36  blob

Error   3   'blob.Blob.blobType' is a 'field' but is used like a 'type' C:\Users\Iurie\Documents\Visual Studio 2010\Projects\blob\blob\blob\Blob.cs 20  9   blob

Error   4   'Microsoft.Xna.Framework.Color.White' is a 'property' but is used like a 'type' C:\Users\Iurie\Documents\Visual Studio 2010\Projects\blob\blob\blob\Blob.cs 20  31  blob

Upvotes: 2

Views: 2261

Answers (3)

Trisped
Trisped

Reputation: 5999

This will do what you are looking for:

class Blob
{
    public Texture2D texture;

    public Dictionary<byte, Color> blobType = new Dictionary<byte, Color>() { { 1, Color.White } };

    public Vector2 position;

    private float scale = 1;
    public float Scale
    {
        get { return scale; }
        set { scale = value; }
    }

    public Blob(Texture2D texture, float scale)
    {
        this.texture = texture;
        this.Scale = scale;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, null, Color.White, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
    }
}

Upvotes: 0

Dave Zych
Dave Zych

Reputation: 21887

You can't execute code outside of a method. To add default values call add in the constructor.

class Blob
{
    public Texture2D texture;

    public Dictionary<byte, Color> blobType = new Dictionary<byte, Color>();

    public Blob() 
    {
        blobType.add(1, Color.White);
    }
}

Upvotes: 2

Ameen
Ameen

Reputation: 2586

You can't have executing code outside of a method in C#. To add a set of default entries to your dictionary add them in the constructor of the Blob class.

Upvotes: 8

Related Questions