即使声明为公开,也无法访问该属性

发布于 2024-11-05 06:17:01 字数 2654 浏览 4 评论 0原文

这是我在 C# 中遇到的问题:

我有以下类:

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;
    }
}

entity.Number = 1 行上,我收到以下编译时错误:

 '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?)

如何解决此错误?看来我绝对应该能够访问 Number 属性。我也尝试过以非汽车财产的方式来做。 (即我编写了一个私有支持者变量并自己写出了 get 和 set )。

编辑

一些有用的海报告诉我,我没有提供足够的信息。
抱歉,这是我第一次在 StackOverflow 或类似网站上发布问题。请参阅下面的相关代码的更完整图片。

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) { }
    }
}

确切的错误消息:

'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?)

另一个注意事项,我已经检查并重新检查Entity实际上指向正确的代码,而不是其他Entity

再次感谢您的帮助!

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?)

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-11-12 06:17:01

看看你的例子,它应该工作 - 你能验证Entity指向你定义的类,而不是一些内置类吗? (在 Visual Studio 中右键单击Entity,转到定义)。

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).

來不及說愛妳 2024-11-12 06:17:01

虽然我还没有测试过,但我强烈怀疑如果我将代码复制并粘贴到 Visual Studio 中,它不会产生您引用的错误。 (甚至是您编辑的版本。)

请发布一个实际产生错误的示例。将你的项目精简到只剩下骨头,只发布与问题相关的部分。您很可能会自己发现该过程中的错误:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文