Pedro Pedro - 15 days ago 5
C# Question

C# OpenTK Block must be fully assigned

Hello i am using c# and opentk and i have this code

using System;
using System.Drawing;
using System.IO;
using OpenTK;


namespace PlatformGame
{
struct Level
{
private Block[,] grid;
public Block this[int X, int Y]
{
get
{
return grid[X, Y];
}
set
{
grid[X, Y] = value;
}
}

public Point PlayerStartPos;

private string filename;
public string FileName
{
get
{
return filename;
}
}


public int Width
{
get
{
return grid.GetLength(0);
}
}
public int Height
{
get
{
return grid.GetLength(1);
}
}

public Level(int Width, int Height)
{
grid = new Block[Width, Height];
filename = "none";
PlayerStartPos = new Point(1, 1);

for (int x=0; x< Width; x++)
{
for (int y = 0; y < Height; y++)
{
if (x == 0 || y == 0 || x == Width - 1 || y == Height - 1)
{
grid[x, y] = new Block(BlockType.Water, x, y);
}
else
{
grid[x, y] = new Block(BlockType.Empty, x, y);
}
}
}
}
}

public enum BlockType
{
Empty,
Water,
Grass,
Dirt,
Stone,
Platform,
Ladder,
LadderPlatform
}

struct Block
{
private BlockType type;

private int PosX, PosY;

private bool Water, Grass, Dirt, Stone, Platform, Ladder, LadderPlatform;

public BlockType Type
{
get { return type; }
}

public int X
{
get { return PosX; }
}
public int Y
{
get { return PosY; }
}
public bool IsWater
{
get { return Water; }
}
public bool IsGrass
{
get { return Grass; }
}
public bool IsDirt
{
get { return Dirt; }
}
public bool IsStone
{
get { return Stone; }
}
public bool IsPlatform
{
get { return Platform; }
}
public bool IsLadder
{
get { return Ladder; }
}

public Block(BlockType type,int x,int y)
{
this.PosX = x;
this.PosY = y;
this.type = type;

this.Water = false;
this.Grass = false;
this.Dirt = false;
this.Stone = false;
this.Ladder = false;
this.Platform = false;

switch (type)
{
case BlockType.Ladder:
Ladder = true;
break;
case BlockType.LadderPlatform:
Ladder = true;
Platform = true;
break;
case BlockType.Platform:
Platform = true;
break;
case BlockType.Water:
Water = true;
break;
case BlockType.Grass:
Grass = true;
break;
case BlockType.Dirt:
Dirt = true;
break;
case BlockType.Stone:
Stone = true;
break;
}
}

}
}


for some reason visual studio is giving me this error at
public Block



Field 'Block.LadderPlatform' must be fully assigned before control is returned to the caller PlatformGame


I have tried to understand the problem but i cant solve it

Answer

you miss this.LadderPlatform = false; in your initialisation.

If you add this before your switch statement in public Block(BlockType type,int x,int y) it will work

Like this:

public Block(BlockType type, int x, int y)
{
    this.PosX = x;
    this.PosY = y;
    this.type = type;

    this.Water = false;
    this.Grass = false;
    this.Dirt = false;
    this.Stone = false;
    this.Ladder = false;
    this.Platform = false;
    this.LadderPlatform = false;  //<--- This is missing

    switch (type)
    {
        case BlockType.Ladder:
            Ladder = true;
            break;
        case BlockType.LadderPlatform:
            Ladder = true;
            Platform = true;
            break;
        case BlockType.Platform:
            Platform = true;
            break;
        case BlockType.Water:
            Water = true;
            break;
        case BlockType.Grass:
            Grass = true;
            break;
        case BlockType.Dirt:
            Dirt = true;
            break;
        case BlockType.Stone:
            Stone = true;
            break;
        default:
            break;
    }
}