1

I have a project that is divided into 3 seperate visual studio projects, a Game, Console and Library project, I am trying to get a static variable from the library to change from my console project and then read it in the game project. Here is some code to clear that up:

This is my library code, my one variable I want to change

namespace LibraryProject.Values
{
    public class Variables
    {
        public static string LastSaid { get; set; }
    }
}

This is the console project, where I change this string in the library project:

namespace ConsoleProject
{
        private void Client_OnMessageRecieved(object sender, OnMessageReceivedArgs e)
        {
            Console.WriteLine("Recieved message, the message was: " + e.ChatMessage.Message);
            Variables.LastSaid = e.ChatMessage.Message;
            Console.WriteLine("LastSaid is now: " + Variables.LastSaid);
        }
}

and finally this is my gameproject, that shows the value on the screen.

namespace LibraryProject
{
    public Interface(ContentManager content)
    {
    TextString lastSaid;


        public void Update()
        {
            lastSaid = new TextString(fonts.Font1, Variables.LastSaid, new Vector2(100, 100), Color.White);
        }
}

My TextString class:

namespace LibraryProject
{
    class TextString
    {
        SpriteFont Font;
        string Text;
        Vector2 Position;
        Color Color;

        public TextString(SpriteFont font, string text, Vector2 position, Color color)
        {
            this.Font = font;
            this.Text = text;
            this.Position = position;
            this.Color = color;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (Text != null)
            {
                spriteBatch.DrawString(Font, Text, Position, Color);
            }
        }
    }
}

My game project:

namespace GameProject
{
    public class GameCore : Game
    {
        protected override void LoadContent()
        {
            Interface = new Interface(Content);
        }
        protected override void Update()
        {

            Interface.Update(gameTime);
}
}
}

I can change the value in the Console project just fine, my console outputs that the LastSaid variable in the library project has changed, but when I finally want to output the LastSaid variable in my game project, it does not change at all when I check up on the variable, it stays at the value it was instanced at. Can someone help me explain why this happens?

Stan
  • 3,659
  • 14
  • 35
  • 42
  • 2
    create the class `static` – Ehsan Sajjad Feb 19 '18 at 17:19
  • Show us your actual code. `lastSaid` doesn't exist. – SLaks Feb 19 '18 at 17:19
  • Try `LibraryProject.Variables.LastSaid` (with the full namespace) – John Wu Feb 19 '18 at 17:20
  • Make sure the value is `set` before you try and `get` it, and also note you need to probably call `Update` after it is set. – crashmstr Feb 19 '18 at 17:24
  • I added some of my code which should clarify it abit, I am sure the value is set when I get it, @crashmstr – Stan Feb 19 '18 at 17:24
  • That didn't work, @JohnWu – Stan Feb 19 '18 at 17:26
  • lastSaid being updated in Update seems to be a TextString Class rather than Variables class. Are you updating correct field? – touchofevil Feb 19 '18 at 17:26
  • I'm trying to directly access the variable in the Variables class when making the textstring class @touchofevil – Stan Feb 19 '18 at 17:27
  • 4
    Is your Game project running in the same process as the Console and library projects? – Polyfun Feb 19 '18 at 17:31
  • @Polyfun The Library project is not something that runs, it's just a class library, but the game and console projects are two different running processes, yeah. – Stan Feb 19 '18 at 17:35
  • See this : https://stackoverflow.com/questions/10016061/is-a-static-object-the-same-object-in-multiple-running-applications – PaulF Feb 19 '18 at 17:39
  • What you've run into here is one of the many reasons global static state is considered bad https://stackoverflow.com/questions/3151768/are-global-static-classes-and-methods-bad – craftworkgames Feb 20 '18 at 22:09

1 Answers1

3

I assume that your GameProject and ConsoleProject are two applications.

If this is the case, the static variable is not shared between the two processes, they both have an in memory instance of it. Even if the static variable belongs to a library.

This other Q&A is similar : Static members behavior with multiple instance of application - C#

M'hand BOUGHIAS
  • 730
  • 10
  • 13