0

I'm quite new to c#. Would like to have default values in my POCO. Path2 should depend on Path1 or extend it respectively.

public class Config
{
    public string Path1 { get; set; }
    public string Path2 { get; set; }
...
}

public Config()
{ Path1 = "testpath";
  Path2 = path1 + "bla";
  ...}

If now the user changes Path1 in the UI, I want Path2 also changed accordingly.

Config testconfig = new Config();
testconfig.Path1 = "changed";

But Path2 stays at "testpath bla" instead of "changed bla".

I guess I am missing something essential here. Could someone kick me in the right direction please?

Regards

mJay
  • 713
  • 3
  • 11
  • 23

2 Answers2

3

If the second part of Path2 is constant then you can use this:

public string Path2 
{
    get { return string.Format("{0}{1}", Path1, "bla"); }
}

instead of making Path2 an auto property.

EDIT:

According to the comments, maybe this is what you want

public class Config
{
    private string path1;
    private string path2;

    public string Path1 
    { 
        get { return path1;}
        set
        {
            path1 = value;
            Path2 = string.Format("{0}bla", value);
        }
    }

    public string Path2 
    {
        get { return path2; }
        set { path2 = value; }
    }

    public Config()
    {
        Path1 = "testpath";
    }
}

This will synchronize Path1 and Path2 any time when Path1 is set but also allows Path2 to be set completely separate

Flat Eric
  • 7,971
  • 9
  • 36
  • 45
  • that would mean path2 would always depend on path1, right? In my case this should only be the default value in first place. User should be able to specify Path2 also by his own... – mJay Nov 16 '14 at 14:58
  • There is no reason to use `string.Format` here. – SLaks Nov 16 '14 at 14:58
  • @mJay What should happen if the code first sets Path2, and then sets Path1? Should it leave the value that was explicitly set to Path2, or should it overwrite it with Path1+extra ? – Lasse V. Karlsen Nov 16 '14 at 14:59
  • @LasseV.Karlsen that should never happen. My goal is to have Path1 default set to "c:\" for instance and path2 to "c:\test" but also provide the user the possibility to define complete own,independend paths – mJay Nov 16 '14 at 15:01
  • 1
    @mJay: Did my changes match your needs? – Flat Eric Nov 16 '14 at 15:29
  • yes it works good, thank you. the string.format... is not necessary i think. It is mainly the solution from @Haedrian. So both suit my needs – mJay Nov 16 '14 at 15:36
1

Strings don't work that way. When you append it, you're creating a separate new string (internally I mean), so your changes aren't going to 'propagate'.

To solve this for a fixed value, you can use Flat Eric's answer.

Having read your comments about a 'default value' and still make it avaliable for change, I'll propose something like this

private string _path2 = null;

public string Path1 {get;set;}
public string Path2 
{
get {

if (String.IsNullOrEmpty(_path2))
{
return Path1 + "bla"; //Or your default value
}
else 
{
return _path2;
}

}

set 
{
_path2 = value;
}

}
Community
  • 1
  • 1
Haedrian
  • 4,240
  • 2
  • 32
  • 53
  • Just a quick additional note, strings are what is known as immutable, which is what the "separate new string" means. There have been questions asked here as to why this is [like this](http://stackoverflow.com/questions/93091/why-cant-strings-be-mutable-in-java-and-net) – Kevin Anderson Nov 16 '14 at 15:13