2014-02-19 2 views
0

바보 같은 질문이 아니길 바랄뿐입니다.키 누름을 기준으로 문자열 업데이트

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); 
    } 
} 

나는 또한 InputManager 클래스가 있습니다 :

나는 베어 게임 클래스가

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(); 
      } 
     } 
    } 
} 

그러나 아무리 내가 원래 문자열 inputHolder이 문자열이있는 경우에도 영향을받지 않습니다 누르면 어떤 키 C#에서 참조 형식으로 처리됩니다.

변경 시도했습니다. InputManager.stringToUpdate = inputHolder; ~ InputManager.stringToUpdate = ref inputHolder; 및 아무 것도 발생하지 않습니다.

무엇이 여기에 있습니까?

+1

문자열을 변경할 수 없습니다. 문자열을 전달할 때 "내용 포인터를 가리키는 포인터"가 아닌 해당 내용을 참조로 전달합니다. 업데이트가 가능하려면'inputHolder' 참조에 대한 참조가 필요합니다. 그러나 당신은 할 수 없습니다. 확인해보세요. http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type – BatteryBackupUnit

답변

0

이미 말했듯이 문자열은 변경 불가능합니다. inputHolder 문자열을 클래스에 래핑하면 원하는 효과를 얻을 수 있습니다.

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(); 
      } 
     } 
    } 
}