Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use declared dictionary inside the same class?

Tags:

c#

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
like image 248
Fake Name Avatar asked Sep 14 '26 00:09

Fake Name


2 Answers

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.

like image 110
Ameen Avatar answered Sep 15 '26 12:09

Ameen


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);
    }
}
like image 25
Dave Zych Avatar answered Sep 15 '26 14:09

Dave Zych



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!