2

This is class Test

public class Test
{
    public string mystr;
}

And i call it from method :

  string my = "ABC";
  Test test = new Test();
  test.mystr = my;
  test.mystr = "";

Result of a bit code above are : my = "ABC" and test.mystr = ""

How can I set my to empty string "" when I change test.mystr = ""?

Nitin Sawant
  • 7,278
  • 9
  • 52
  • 98
Hùng Lê Xuân
  • 215
  • 1
  • 5
  • 20
  • 2
    I'm thinking you'll need to wrap a String in a custom type where a Value property holds the actual String value. Then add implicit type conversion to String for your custom type. (Don't have time to write the code so someone can as an answer.) – cfeduke May 09 '13 at 11:45
  • have a look at this http://msdn.microsoft.com/en-us/library/s6938f28.aspx – Murugan May 09 '13 at 11:49
  • This question got closed before I could finish redacting my answer, please see [this answer by Eric Lippert](http://stackoverflow.com/a/2982037/703016) that explains why it is not possible and how to obtain similar behavior. – user703016 May 09 '13 at 12:04

3 Answers3

7

If I understand correctly, you want the variables my and test.myStr to be linked, so if one changes, the other changes?

The answer is simple: It cannot!

A string is an immutable class. Multiple references can point to a string instance, but once this instance is modified, a string instance is created with the new value. So a new reference is assigned to a variable, while the other variables still point to the other instances.

There is a workarounds, but I suspect you will not be happy with it:

public class Test
{
    public string mystr;
}

Test myTest1 = new Test { myStr = "Hello" };
Test myTest2 = myTest1;

Now, if you change myTest1.myStr, the variable myTest2.myStr will also be modified, but that is simply because the myTest1 and myTest2 are the same instances.

There are other solutions like these, but the all come down to the same aspect: A class holding a reference to a string.

zerkms
  • 249,484
  • 69
  • 436
  • 539
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
  • Right, if the type remains as the built in .NET Fx String you're not going to be able to do anything like you could as if it were a pointer in C. – cfeduke May 09 '13 at 11:49
2

Strings in .NET are immutable and don't work like that. One approach you could try is to use a mutable wrapper for the strings.

public class StringReference
{
   public string Value {get; set;}

   public StringReference(string value)
   {
      Value = value;
   }
}

public class Test
{
    internal StringReference mystr;
}

StringReference my = new StringReference("ABC");
Test test = new Test();
test.mystr = my;
test.mystr.Value = "";
// my.Value is now "" as well
JLRishe
  • 99,490
  • 19
  • 131
  • 169
0
string my = "ABC";
Test test = new Test();

Note here, that there is no relationship between your Test class and the string my. I am not entirely sure what you are trying to achieve, but we could do it like this:

public class Test
{
    private string _mystr;
    private Action<string> _action;

    public Test(Action<string> action)
    {
        _action = action;
    }

    // Let's make mystr a property
    public string mystr
    {
        get { return _mystr; }
        set
        {
            _mystr = value;
            _action(_mystr);
        }
    }
}

Now you can do this:

string my = "ABC";
Test test = new Test((mystr) => { if(string.IsNullOrEmpty(mystr)) my = ""; });
test.mystr = my;
test.mystr = "";
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128