Question

I hope this isn't a stupid question but I cannot figure this out.

I have a barebones game class:

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    String inputHolder;

    public Game1()
        : base()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        inputHolder = "Enter your keys: ";
    }

    protected override void Initialize()
    {
        base.Initialize();


        InputManager.stringToUpdate = inputHolder;

    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
        {
            Exit();
        }
        base.Update(gameTime);

        InputManager.update(Keyboard.GetState());
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        base.Draw(gameTime);

        Console.WriteLine(inputHolder);
    }
}

I also have an InputManager class:

static class InputManager
{
    public static string stringToUpdate;

    public static void update(KeyboardState kbState)
    {
        if (stringToUpdate != null)
        {
            foreach (Keys k in kbState.GetPressedKeys())
            {
                stringToUpdate += k.ToString();
            }
        }
    }
}

However no matter what keys I press the original string inputHolder is not affected even though strings are treated as reference types in C#.

I have tried changing InputManager.stringToUpdate = inputHolder; to InputManager.stringToUpdate = ref inputHolder; and nothing happens.

What am I missing here?

Was it helpful?

Solution

As has already been said, strings are immutable. Wrapping the inputHolder string in a class will give you the desired effect:

public class InputHolder
{
    public string Input { get; set; }
}

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    InputHolder inputHolder = new InputHolder();

    public Game1()
        : base()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        inputHolder.Input = "Enter your keys: ";
    }

    protected override void Initialize()
    {
        base.Initialize();
        InputManager.inputHolderToUpdate = inputHolder;
    }

    // etc

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        base.Draw(gameTime);
        Console.WriteLine(inputHolder.Input);
    }
} 

static class InputManager
{
    public static InputHolder inputHolderToUpdate;

    public static void update(KeyboardState kbState)
    {
        if (inputHolderToUpdate != null)
        {
            foreach (Keys k in kbState.GetPressedKeys())
            {
                inputHolderToUpdate.Input = inputHolderToUpdate.Input + k.ToString();
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top