mrd
mrd

Reputation: 2183

.NET Properties

Can someone help me to figure out how can I achieve following:

PersonalInfo info = new PersonalInfo();
info.Contact.Name = "name";
info.Contact.Telephone = "2323232";

Thanks.


The following was achieved.

PersonalInfo info = new PersonalInfo(); 
info.Contact.Name = "name"; 
info.Contact.Telephone = "2323232";

But in the case of following, what should I do?

PersonalInfo info = new PersonalInfo(); 
info.Contact.Name = "name"; 
info.Contact.Telephone = "2323232";
info.Contact.Office.Address = "Sweden"

Thanks for your help.

Upvotes: 0

Views: 244

Answers (3)

cocc
cocc

Reputation: 33

public class PersonalInfo
{
    public Contact contact = new Contact(); 
}

public class Contact
{
    public string Name { get; set; }
    public string Telephone { get; set; }
}

Upvotes: 0

WraithNath
WraithNath

Reputation: 18013

private void Test()
{
    PersonalInfo pi = new PersonalInfo();

    pi.Contact = new Contact();

    pi.Contact.Name = "test";
}

public class Contact
{
    public string Name {get;set;}
    public string Telephone {get;set}
}

public class PersonalInfo
{
    public Contact Contact {get;set;}
}

If you want to automatically instantiate the Contact details add this constructor to the personalInfo class:

    public PersonalInfo()
    {
        this.Contact = new Contact();
    }

Upvotes: 1

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

public class Contact{
  public string Name {get;set;}
  public string Telephone {get;set;}
}

public class PersonalInfo{
  public Contact Contact {get;set;}
  public PersonalInfo(){
    this.Contact = new Contact();
  }
}

var info = new PersonalInfo();
info.Contact.Name = "name";
info.Contact.Telephone = "2323232";

;)

Upvotes: 3

Related Questions