Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表设置为方法返回值,但不能使用该方法_C#_List_Class_Methods - Fatal编程技术网

C# 将列表设置为方法返回值,但不能使用该方法

C# 将列表设置为方法返回值,但不能使用该方法,c#,list,class,methods,C#,List,Class,Methods,我试图将一个新的列表变量设置为类的方法返回值的结果,如下所示: public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; public static SpriteBatch spriteBatch; // ...stuff... // Class initialization private Map map = new Map() { Widt

我试图将一个新的列表变量设置为类的方法返回值的结果,如下所示:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    public static SpriteBatch spriteBatch;

    // ...stuff...

    // Class initialization
    private Map map = new Map() { Width = 10, Height = 10, TileWidth = 16, TileHeight = 16 };

    // Variable declaration
    List<Vector2> blockPos = new List<Vector2>();
    blockPos = map.generateMap(); // Doesn't work

    Texture2D dirtTex;

    // ...more stuff...
}
地图类别:

class Map
{
    // Variable declaration
    public int Width { get; set; } // Width of map in tiles
    public int Height { get; set; } // Height of map in tiles
    public int TileWidth { get; set; }
    public int TileHeight { get; set; }
    Random rnd = new Random();

    // Generate a list of Vector2 positions for blocks
    public List<Vector2> generateMap()
    {
        List<Vector2> blockLocations = new List<Vector2>();

        // For each tile in the map...
        for (int w = 0; w < Width; w++)
        {
            for (int h = 0; h < Height; h++)
            {
                // ...decide whether or not to place a tile...
                if (rnd.Next(0, 1) == 1)
                {
                    // ...and if so, add a tile at that location.
                    blockLocations.Add(new Vector2(w * TileWidth, h * TileHeight));
                }
            }
        }

        return blockLocations;
    }
}

通过声明变量,然后在LoadContent()方法中将其设置为map.generateMap()进行修复:

公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
公共静态SpriteBatch SpriteBatch;
// ...
//类初始化
私有地图Map=newmap(){Width=10,Height=10,TileWidth=16,TileHeight=16};
//变量声明
List blockPos=新列表();
纹理2d-dirtTex;
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
dirtTex=内容物负载(“污垢”);
blockPos=map.generateMap();
}
// ...
}

我们是否缺少方法或构造函数?你不能把语句放在类里面…你不能把它放在构造函数里吗?我已经编辑了你的标题。请参见“”,其中的共识是“不,他们不应该”。这与您的上一个问题相同:您在类中放置的内容和在可执行代码块中放置的内容受到限制,如方法。你应该找到一本很好的初学者C#指导书/课程来帮助你了解其中的差异。@JLe我刚才尝试使用构造函数,但代码似乎无法运行-我已将其添加到帖子中。
class Map
{
    // Variable declaration
    public int Width { get; set; } // Width of map in tiles
    public int Height { get; set; } // Height of map in tiles
    public int TileWidth { get; set; }
    public int TileHeight { get; set; }
    Random rnd = new Random();

    // Generate a list of Vector2 positions for blocks
    public List<Vector2> generateMap()
    {
        List<Vector2> blockLocations = new List<Vector2>();

        // For each tile in the map...
        for (int w = 0; w < Width; w++)
        {
            for (int h = 0; h < Height; h++)
            {
                // ...decide whether or not to place a tile...
                if (rnd.Next(0, 1) == 1)
                {
                    // ...and if so, add a tile at that location.
                    blockLocations.Add(new Vector2(w * TileWidth, h * TileHeight));
                }
            }
        }

        return blockLocations;
    }
}
public void getPos()
{
    blockPos = map.generateMap();
}
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    public static SpriteBatch spriteBatch;

    // ...

    // Class initialization
    private Map map = new Map() { Width = 10, Height = 10, TileWidth = 16, TileHeight = 16 };

    // Variable declaration
    List<Vector2> blockPos = new List<Vector2>();

    Texture2D dirtTex;

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        dirtTex = Content.Load<Texture2D>("dirt");

        blockPos = map.generateMap();
    }

    // ...
}