Search code examples
c#stringgetter-setter

String from getter is empty when called in another Project C#


I am a total beginner in C# programming language. I am trying to use Getter and Setter in order to set the string in ProjectA and the retrieve it in Project B.

Project B uses Windows Forms, and I wasnt to set the value of TextBox with the retrieved string.

Project A is a Console Project and it just reads out some stuff from file and stores it in string, which I want to retrieve.

However, this is my call in Project B:

 string cardOwner = Transmit.Program.CardOwner;
            Debug.WriteLine("Card owner = " + cardOwner);
            tb_cardholder.Text = cardOwner;

And this is my Getter / Setter in Project A:

private static string _cardOwner;
  public static string CardOwner
        {
            get
            {
                return _cardOwner;
            }

            set
            {
                _cardOwner = value;
            }
        }

 _cardOwner = System.Text.Encoding.ASCII.GetString(bCardOwner);

But in Project B I get "" empty string.

I have included Project A in Project B (added Reference and wrote "using ProjectA").

Any ideas what's going wrong?

Thanks.


Solution

  • Just because you include a project and use its classes in your project B, it doesn't mean that you also use the instances of these classes.

    Take the following class:

    public class Test
    {
        public string Message { get; set; }
    }
    

    You can put this class into a DLL project (Tools) and reference it from other projects, like a WinForms project ProjectA and a console project ProjectB.

    In both projects, you can write something like:

    Test t = new Test() { Message = "Hello" };
    

    That creates a new instance of the Test class, but the two running applications ProjectA and ProjectB do not exchange the data! They are completely separated.

    The same is true for class properties.