2
public class Departman
{    
    Person _p = new Person();

    public Person p
    {
        get { return _p; }
        set { _p = value; }
    }    
}

public class Person
{
    private string _PersonName;

    public string PersonName
    {
        get { return _PersonName; }
        set { _PersonName = value; }
    }
}

From the outside I can reach the Person name like this

Departman dp = new Departman();
dp.p.PersonName;

However, I cant reach like this:

Departman dp = new Departman { p.PersonName };

What can I do to reach both way PersonName.

Jimbo
  • 25,790
  • 15
  • 86
  • 131
sakir
  • 3,391
  • 8
  • 34
  • 50

3 Answers3

3

Departman dp = new Departman { p.PersonName };

This syntax has no sense. When you create a new object followed with { }, it is an initializer. This is useful to inject a value to a property so you could do :

new Departman { p = new Person { PersonName = "Foo Bar" } }

Marshall777
  • 1,196
  • 5
  • 15
  • what if,I initialize person class inside the constructure of departman? – sakir Jun 07 '13 at 10:21
  • Do I need initialize Person again,it is already initialize inside the departman class? – sakir Jun 07 '13 at 11:00
  • Initializers act like if you write : Departman obj = new Departman(); obj.Person = new Person(); Therefore, the value you specify in the initializer will overwrite what you set in the constructor. See [this post](http://stackoverflow.com/questions/740658/whats-the-difference-between-an-object-initializer-and-a-constructor). By the way, you should either decide to use initializer or constructor for a property but not both as it would be quite unreadable and difficult to maintain over time. – Marshall777 Jun 07 '13 at 12:59
  • so create an instance of Person _p = new Person(); inside departman,redundacy for my example, right? – sakir Jun 07 '13 at 13:15
  • If you set the "p" property inside an initializer, you don't have to set it in the constructor. – Marshall777 Jun 07 '13 at 13:57
2

Assuming that you are looking to initialize person's name, you can do this:

Departman dp = new Departman { p = new Person { PersonName = "Joe" } };

The PersonName property is nested inside the Departman's Person object, so you need to nest initializers as well. You can also expose PersonName directly, like this:

public class Departman {
    Person _p = new Person();
    public Person p {
        get { return _p; }
        set { _p = value; }
    }
    public string PersonName {
        get { return _p.PersonName; }
        set { _p = value.PersonName; }
    }
}

This makes the following possible:

Departman dp = new Departman { PersonName = "Joe" };
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Do I need initialize Person again,it is already initialize inside the departman class? like thsi,Departman dp = new Departman { p = new Person { PersonName = "Joe" } }; – sakir Jun 07 '13 at 11:02
  • @user2460637 If you want to use the curly brace syntax, but do not want to expose `PersonName` separately, then the answer is "yes". – Sergey Kalinichenko Jun 07 '13 at 12:12
  • does it means "curly brace" is not actually initializer??only initialize primitive type? – sakir Jun 07 '13 at 12:31
  • @user2460637 Curly brace by itself is not an initializer; it lets you initialize properties only as part of a `new` expression. Inside curly braces you can omit `new ...` when it can be deduced from the context (see Marc's answer for that) but the braces in the initializer are part of a `new` expression. – Sergey Kalinichenko Jun 07 '13 at 12:40
0

Because you are initializing p in the constructor, you don't need a new Person - you only need to set the p.PersonName, which would be:

Departman dp = new Departman { p = { PersonName = "Marc" } };

However! A property called p is a really bad idea - and having a PersonName on a Person seems rather redundant; IMO that should be:

var dp = new Departman { Person = { Name = "Marc" } };

Actually, you don't even need a set on the p / Person property, since this approach doesn't use it:

public class Departman {
    private readonly Person person = new Person();
    public Person Person { get { return person; } }
}
public class Person {
    public string Name {get;set;}
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • You say `new Departman { p = { PersonName = "Marc" } }` -- does that really modify the existing (constructor-created) instance, or does it create a new one? I thought the latter (or am I misreading your first sentence?). – Dan Puzey Jun 07 '13 at 10:21
  • could u explain more,whats wrong this sentax? Departman dp = new Departman { p = { PersonName = "Marc" } }; – sakir Jun 07 '13 at 11:04
  • Do I need initialize Person again,it is already initialize inside the departman class? – sakir Jun 07 '13 at 11:05