0

I have two model classes:

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    public int AddressId { get; set; }
    public Address AddressInfo { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string streetName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

If any value gets update in Person.AddressInfo.AddressId, how can I update the Person.AddressId automatically?

harriyott
  • 10,505
  • 10
  • 64
  • 103
  • 1
    Why does `Person` need to have the `AddressId` if it already has a reference to the `Address`? – BJ Myers Apr 27 '17 at 17:01
  • 2
    A better question might be why are you storing this AddressId property twice? – Rufus L Apr 27 '17 at 17:01
  • Why not just have `AddressId` return the value of `AddressInfo.AddressId`? – Rufus L Apr 27 '17 at 17:01
  • your setup seem strange. you have both, address Id and Address info. Unless this is for Entity Framework... and how changing **any** property affects address id? – T.S. Apr 27 '17 at 17:01
  • 1
    How do you expect to handle the case where `AddressInfo` is `null`, and someone tries to set `Person.AddressId`? – Rufus L Apr 27 '17 at 17:03

3 Answers3

3

what about this?

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    public int AddressId 
    { 
        get{ return AddressInfo?.AddressId ?? 0 } 
        set{ AddressInfo?.AddressId = value; }
    }
    public Address AddressInfo { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string streetName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

This uses the AddressInfo as the back storage

Gusman
  • 14,905
  • 2
  • 34
  • 50
2

You could simply write following into the Person class:

 public int AddressId{
    get{return this.AddressInfo?.AddressId ?? 0;}
    set{this.AddressInfo?.AddressId= value;}
 }

Or better to write:

 public int? AddressId{
    get{return this.AddressInfo?.AddressId;}
    set{this.AddressInfo?.AddressId= value;}
 }
Siamand
  • 1,080
  • 10
  • 19
0

below code can help you out,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Address test = new Address();
            test.AddressId = 0;
            test.City = "xyzzzzzzzzzzzzzzz";
            test.streetName = "xyz";
            test.State = "xyzzzzzzzzzzzzzzzxxxxxxxxxxxxxxxxxxx";

            Person ptest = new Person
            {
                PersonId = 1,
                Name = "test1",
                AddressInfo = test,
                AddressId = 5,
            };
        }
    }

    public class Address
    {
        public int AddressId { get; set; }
        public string streetName { get; set; }
        public string City { get; set; }
        public string State { get; set; }
    }
    public class Person
    {
        public int PersonId { get; set; }
        public string Name { get; set; }
        public int AddressId { 

            get{ return AddressInfo != null ? AddressInfo.AddressId : 0;}
            set { AddressInfo.AddressId = value; }
    }
        public Address AddressInfo { get; set; }
    }
}

Before assigning value to addressid ensure that addressinfo not null, if its null assign data to addressinfo then you can able to update the value, otherwise you will get object reference error.

Vinutha N
  • 156
  • 6