私はクラスの理解を深めようとしていますが、 'Address'クラスは 'Person'クラスの 'ShippingAddress'プロパティの型として使用されていますが、それは以下の通りです道は私にエラー与えるので、アドレスのプロパティは、オブジェクト:どのようにクラスを別のクラスのプロパティとして使用してインスタンス化しますか? [C#]
に見つかりました。**「System.NullReferenceException」種類の未処理の例外がClasses_and_Objects.exe
で発生しました追加情報参照がオブジェクトのインスタンスに設定されていない**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Classes_and_Objects
{
class Program
{
static void Main(string[] args)
{
Person john = new Person();
john.FirstName = "John";
john.LastName = "Doe";
john.ShippingAddress.StreetAddress = "78 Fake Street" ;
john.ShippingAddress.City = "Queens";
john.ShippingAddress.State = "NY";
john.ShippingAddress.PostalCode = "345643";
john.ShippingAddress.Country = "United States";
}
}
public class Address
{
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address ShippingAddress { get; set; }
}
}
}