awj
awj

Reputation: 7949

Obtain a reference to a property in C# and assign this to another property

If I have

public class ClassA
{
    public static IEnumerable<string> WhateverA { get; set; }
}

and also

public class ClassB
{
    public static IEnumerable<string> WhateverB { get; set; }
}

Is it possible to obtain a reference to ClassA.WhateverA and assign it to ClassB.WhateverB such that if WhateverA is later assigned a value, WhateverB will consequently be assigned the same?

If I was looking to do the same with methods then I could declare WhateverB as an Action and then assign WhateverA as a delegate, thus giving it a kind of 'pass-through' capability. Is it possible to do the same with a property such that WhataverB always shows the value of WhateverA?

Upvotes: 0

Views: 57

Answers (2)

brent
brent

Reputation: 1488

If you have a reference to ClassA in ClassB you can simply wrap the ClassA property.

public class ClassB
{
    static ClassA a;

    public static IEnumerable<string> WhateverB
    {
        get { return a.WhateverA; }
        set { a.WhateverA = value; }
    }
}

If not, you can use Action<> and Func<> to pass around delegates to the getters and setters - similar to what you said you have done with methods. You just have to treat the setter and getters as separate methods (underneath they are).

To do this with a setter: public class ClassB { static Action> aSetter; static Func> aGetter;

    public static IEnumerable<string> WhateverB
    {
        get
        {
             if (aGetter != null)
             {
                 return aGetter();
             }
        }
        set
        {
             if (aSetter != null)
             {
                 aSetter(value);
             }
        }
    }
}

Upvotes: 1

Mikko Viitala
Mikko Viitala

Reputation: 8404

You could use pub/sub system offered by MVVMLight (or similar) to send and receive notifications between classes and decouple direct dependencies between ClassA and ClassB.

Consider changing your classes like so.

public class ClassA
{
    private static IEnumerable<string> _whateverA;

    public static IEnumerable<string> WhateverA
    {
        get { return _whateverA; }
        set
        {
            _whateverA = value;
            // property changed, send message
            Messenger.Default.Send(new MyMessage(value));
        }
    }
}

public class ClassB
{
    public ClassB()
    {
        // receive message
        Messenger.Default.Register<MyMessage>(this, message => 
        {
            WhateverB = message.Value;
            Console.WriteLine(string.Join(" ", WhateverB));
        });
    }

    public static IEnumerable<string> WhateverB { get; set; }
}

Now whenever ClassA property changes, it'll notify ClassB to change it's property value accordingly. If instance does not exist, it doesn't crash your code at runtime, either.

Above MyMessage is simple class to help passing data around. You decide what this class should look like (hold all of ClassA instance?).

public class MyMessage : MessageBase
{
    public MyMessage(IEnumerable<string> value)
    {
        Value = value;
    }

    public IEnumerable<string> Value { get; private set; } 
}

Now, if you have a super nice program

public class Program
{
    static void Main()
    {
        var b = new ClassB();

        ClassA.WhateverA = new List<string> { "this", "is", "whatever" };
        Console.ReadLine();
    }
}

It'll spit to Console

example


You'll need to install MVVMLight libraries via NuGet to get going.

PM> Install-Package MvvmLightLibs -Version 5.2.0

Upvotes: 0

Related Questions