Home > Software engineering >  Try to display a int through a static field, but return a weird value (C#)
Try to display a int through a static field, but return a weird value (C#)

Time:07-01

I'm trying to cut down on how much duplication I have on my code, so I decided to make one of my classes a static class since I decided that its data should really be shared with everyone. Here's the static method below:

// A static class, that holds all object's coordinates, and methods to return & update their values.
internal static class Coordinate
{
    private static int[,] PlayerCoordinate { get; set; }
    public static int[,] GateCoordinate { get; }
    public static int[,] FountainCoordinate { get; }

    static Coordinate() // FIRST VALUE IS X (column), SECOND VALUE IS Y (row).
    {
        PlayerCoordinate = new int[,] { { 0, 0 } };
        GateCoordinate = PlayerCoordinate; // Just starts off in the same place as player.
        FountainCoordinate = new int[,] { { 2, 0 } };
    }

    // A static method, that sends the data of all object coordinates, deconstructed into seperate ints.
    public static int PlayerColumn() { return PlayerCoordinate[0, 0]; }
    public static int PlayerRow() { return PlayerCoordinate[0, 1]; }
    public static int GateColumn() { return GateCoordinate[0, 0]; }
    public static int GateRow() { return GateCoordinate[0, 1]; }
    public static int FountainColumn() { return FountainCoordinate[0, 0]; }
    public static int FountainRow() { return FountainCoordinate[0, 1]; }

    // Updates the coordinates of the player.
    public static void UpdatePlayerCoordinate(int column, int row) { PlayerCoordinate = new int[,] { { column, row } }; }
}

The main issue comes in from my GameManager class. On the console, the beginning section should print out "You are the room at (Column=0, Row=0), but it prints this instead: enter image description here

Here is the code for my GameManager class:

internal class GameManager
{
    private bool IsGameOver;
    private Player Player;
    private Updater Updater;
    // Don't need to call Fountain or Coordinate since they're static

    public GameManager()
    {
        IsGameOver = false;

        Player = new();
        Updater = new();
    }

    public void RunGame()
    {
        while (!IsGameOver)
        {
            Console.WriteLine("----------------------------------------------------------");
            Updater.DisplayPlayerPosition(); // This is the main line that I'm having issues with as of right now. All other functions past this are another problem.
            Updater.DisplayPlayerSenses();
            string playerInput = Player.GetInput();
            Updater.MovePlayer(playerInput);
            IsGameOver = Updater.CheckForWin();
        }
    }
}

And just to make sure, here is the code from my updater class, with the specific method that I'm having issues with:

internal class Updater
{
    // No fields

    // Default constructor

    // Gets the text to show the player his current position.
    public void DisplayPlayerPosition() // This is the method that I'm having issues with.
    {
        Console.WriteLine($"You are in the room at (Column={Coordinate.PlayerColumn}, Row={Coordinate.PlayerRow})");
    }
...

I'm fairly new to the static keyword so I believe that I may be missing smth. I personally believe that it's because the class itself hasn't been initialized (like I haven't called the constructor for the Coordinate class, and apparently you can't call a static constructor anyways), but that's just me. If I could get any help, I'd greatly appreciate it!

CodePudding user response:

PlayerColumn() and PlayerRow() are methods, but you are accesing them in the WriteLine statement as if they are properties.

Update your WriteLine to:

Console.WriteLine($"You are in the room at (Column={Coordinate.PlayerColumn()}, Row={Coordinate.PlayerRow()})");
  • Related