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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

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...
There is no reason to use string.Format here.
@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 ?
@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: Did my changes match your needs?
|
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;
}

}

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.