Tizz
Tizz

Reputation: 840

How does one save a reference to an object in another class?

This should be a very simple solution for the avid C# developer. I am looking to change the value of a string within a class, so within a thread, the string can change without me doing anything. Here is a small example of what I mean, simplified, but you should get the idea.

class A_CLass
{
    string keptString;

    void keepString( ref string theString ) 
    {
        keptString = theString;
    }

    // This will get called when the thread is triggered
    void changeString( string theString ) 
    {
        keptString = theString;
    }
}

void f1()
{
    A_Class a = new A_Class();
    string base_string = "asdf";

    a.keepString( ref base_string );
    ...
    // Thread is signaled
    ...
    // Now base_string should be "fdsa"
}

void threadedFunction()
{
    // When the thread is triggered ...
    a.changeString( "fdsa" );
}

Basically I want to keep a reference of 'base_string' in the A_Class, so threaded methods can change the value, and within f1(), I can see the changed value, in this case "fdsa".

Thanks!

Upvotes: 0

Views: 939

Answers (4)

RouR
RouR

Reputation: 6336

class A_Class
{
    Ref<string> link;
    void  A_Class( Ref<string> link) 
    {
        this.link= link;
    }
    void somefunction( string str ) 
    {
        if(link.Value.Length > 2)
            link.Value = str;
    }
}


public class Ref<T> 
{
    private Func<T> getter;
    private Action<T> setter;
    public Ref(Func<T> getter, Action<T> setter)
    {
        this.getter = getter;
        this.setter = setter;
    }
    public T Value
    {
        get
        {
            return getter();
        }
        set
        {
            setter(value);
        }
    }
}

Upvotes: 0

Etienne de Martel
Etienne de Martel

Reputation: 36852

You'll have to use an intermediate wrapper class:

public class Wrapper<T> // generic, so can be used with any type
{
    public T Value { get; set; }
    public Wrapper(T val) { Value = val; }
}

class A_CLass
{
    Wrapper<string> keptString;

    void keepString(string theString) 
    {
        keptString = new Wrapper<string>(theString);
    }

    void changeString(string theString) 
    {
        keptString.Value = theString;
    }
}

Upvotes: 1

Adam Jones
Adam Jones

Reputation: 721

It looks like you are wanting to store a reference to the reference (like a pointer to a pointer). One way to do something like this would be to pass a method that sets the string into your class like this:

class A_Class
{
    Action<string> setter;
    void storeSetter( Action<string> setter ) 
    {
        this.setter = setter;
    }
    void callSetter( string str ) 
    {
        setter(str);
    }
}

Then pass in a lambda that sets the string like:

public class OtherClass
{
    private string someString;
    private void test()
    {
        var a = new A_Class();
        a.keepString((s)=>{someString = s;});
    }
}

Once your class has this string setting method, you can call the method to set the string.

Upvotes: 1

Scott
Scott

Reputation: 999

Use a StringBuilder for this purpose.

Represents a mutable string of characters.

Upvotes: 2

Related Questions