C# 不能使用实例引用访问成员;用类型名称来限定它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9549527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:51:12  来源:igfitidea点击:

Member cannot be accessed with an instance reference; qualify it with a type name instead

c#staticxna

提问by Cyral

I have a game. It has a Levelclass for drawing the level, managing blocks, etc. I also have a Tileclass for each individual tile (type, x, y), and a block class for the type of block.

我有一个游戏。它有一个Level用于绘制关卡、管理块等的Tile类。我还有一个用于每个单独的图块(类型、x、y)的类,以及一个用于块类型的块类。

I get the error:

我收到错误:

Member cannot be accessed with an instance reference; qualify it with a type name instead

Member cannot be accessed with an instance reference; qualify it with a type name instead

In

Texture2D texture = tiles[x, y].blockType.Dirt_Block.texture;

Here is my Tile.cs

这是我的 Tile.cs

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Game.Tiles;

namespace Game
{
public struct Tile
{
    public BlockType blockType;
    public  int X;
    public  int Y;

    /// <summary>
    /// Creates a new tile for one position
    /// </summary>
    /// <param name="blocktype">Type of block</param>
    /// <param name="x">X Axis position</param>
    /// <param name="y">Y Axis position</param>
    public Tile(BlockType blocktype,int x,int y)
    {
        blockType = blocktype;
        X = x;
        Y = y;
    }
}
}

And block.cs...

还有block.cs...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Game.Tiles;


namespace Game.Tiles
{

/// <summary>
/// Controls the collision detection and response behavior of a tile.
/// </summary>
enum BlockCollision
{
    /// <summary>
    /// A passable tile is one which does not hinder player motion at all.
    /// </summary>
    Passable = 0,

    /// <summary>
    /// An impassable tile is one which does not allow the player to move through
    /// it at all. It is completely solid.
    /// </summary>
    Impassable = 1,

    /// <summary>
    /// A platform tile is one which behaves like a passable tile except when the
    /// player is above it. A player can jump up through a platform as well as move
    /// past it to the left and right, but can not fall down through the top of it.
    /// </summary>
    Platform = 2,
}
public enum BlockType
{
     Dirt_Block = new Block("Dirt Block",,BlockCollision.Impassable,250,0,0)
}
/// <summary>
/// Stores the appearance and collision behavior of a tile.
/// </summary>
public struct Block
{

    public static string Name;
    public static Texture2D Texture;
    public static BlockCollision Collision;
    public static int Width = 16;
    public static int Height = 16;
    public static int maxAmount;
    public static int Worth;
    public static int Strength;

    public static Vector2 Size = new Vector2(Width, Height);

    /// <summary>
    /// Contructs a new block
    /// </summary>
    /// <param name="name">Name of the block</param>
    /// <param name="texture">Texture to use as image</param>
    /// <param name="collision">Collision type</param>
    /// <param name="maxamount">Maximum amount of these blocks you can have</param>
    /// <param name="worth">How much this block is worth</param>
    /// <param name="strength">How hard the block is (how much you will have to hit to mine it)</param>
    public Block(string name, Texture2D texture,BlockCollision collision, int       maxamount, int worth, int strength)
    {
        Name = name;
        Texture = texture;
        Collision = collision;
        Width = 16;
        Height = 16;
        maxAmount = maxamount;
        Worth = worth;
        Strength = strength;
    }
}
}

I'm not even sure if its an efficient way to make a new tile(blockType,x,y)for every block on the grid. Any help would be appreciated. Mainly; How can I get the value of blockType.Dirt_Block.Texture? But I get the error about something not being static or whatever.

我什至不确定它是否是new tile(blockType,x,y)为网格上的每个块制作一个的有效方法。任何帮助,将不胜感激。主要是;我怎样才能获得 的价值blockType.Dirt_Block.Texture?但是我得到关于某些东西不是静态的或其他什么的错误。

http://pastebin.com/s50AB6kz

http://pastebin.com/s50AB6kz

EDIT:Looking back at this I feel really stupid, but I will leave it here since obviously 10k views means it has helped someone

编辑:回想起来,我觉得真的很愚蠢,但我会把它留在这里,因为显然 10k 次观看意味着它帮助了某人

采纳答案by Thomas Levesque

This code is not legal, because an enum member can only have an integer value.

这段代码是不合法的,因为枚举成员只能有一个整数值。

public enum BlockType
{
     Dirt_Block = new Block("Dirt Block",,BlockCollision.Impassable,250,0,0)
}

Dirt_Blockis a static member of the BlockTypetype, so you can't access the Dirt_Blockmember on an instance of BlockType.

Dirt_Block是的静态成员BlockType的类型,所以你不能访问Dirt_Block部件上的一个实例BlockType

And I think you misunderstand what enums are; enum members can't have enums of their own.

我认为你误解了枚举是什么;枚举成员不能拥有自己的枚举。

回答by D Stanley

staticvariables are the same for ALL instances of that type. Take all of the statics off of Block and you should be fine.

static变量对于该类型的所有实例都是相同的。把所有的statics 从 Block 上取下来,你应该没事。

Also make BlockTypeand Blockclasses instead of an enumand struct, respectively, since they are neither enums nor structs.

也分别用 makeBlockTypeBlockclasses 代替 anenumstruct,因为它们既不是enums 也不struct是 s。