开发者

Property not accessible even though declared public

Here is my problem in C#:

I have the following classes:

public class Entity {
    public int Number { get; set; }
// Other methods, constructor etc, not relevant to this question.
}

public class Manager {
    private Foo() {
        Entity entity = new Entity();
        entity.Number = 1;
    }
}

On the line entity.Number = 1, I get the following compile time error:

 'Entity' does not contain a definition for 'Number' and no extension method 'Number accepting a first argument of type 'Entity' could be found (are you missing a directive or an assembly reference?)

How do I resolve this error? It seems like I should definitely be able to access the Number property. I've tried doing it the non-auto property way as well. (i.e. I wrote a private backer variable and wrote out the get and set myself).

Edit

Some helpful posters have advised me that I have not provided enough information.

My apologies, this is my first time posting a question on StackOverflow or similar sites. Please see below for a more complete picture of the relevant code.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SharedContent;

namespace Glow
{
    public class GlowGame : Microsoft.Xna.Framework.Game
    {
        // Viewport and graphics variables
        GraphicsDeviceManager graphics;

        public GlowGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            Entity entity = new Entity(Content);

            // Error occurs in line below!
            entity.Number = 1;
        }
        
        // Standard game template methods here:  Initialize, LoadContent, Update,
        // Draw, etc.

    }
}

in a separate file named Entity.


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SharedContent
{
    public class Entity
    {
        public int Number { get; set; }
        public Entity(ContentManager content) { }
    }
}

Exact error message:

'SharedContent.Entity' does not contain a definition for 'Number' and no extension method 'Number' accepting a first argument of type 'SharedContent.Entity' could be found (are you missing a using directive or an assembly reference?)

开发者_JS百科Another note, I have checked and rechecked that Entity is in fact point at the correct code and not some other Entity.

Thank you again for your help!


Looking at your example it should work - could you verify that Entity points to the class you defined, and not some built-in class? (right-click Entity in Visual Studio, go to definition).


Although I haven’t tested it, I strongly suspect that if I copy and paste your code into Visual Studio, it will not produce the error you quoted. (Even your edited version.)

Please post an example that actually produces the error. Trim your project down to the bones and post just the bits that are relevant to the problem. In all likelihood, you will find the error in the process yourself :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜