バージョン

CustomerData

Visual Basic の場合:

Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public NotInheritable Class CustomerData
    Private Sub New()
    End Sub
    Public Shared Function GetCustomers() As ObservableCollection(Of Customer)
        Dim リスト As New ObservableCollection(Of Customer)()
        'Object Initializers を使用して Customers を ObservableCollection に追加します。
        list.Add(New 顧客 With {.ID = 123, .FullName = New 名前 With {.FirstName = "John", .LastName = "Smith"}})
        list.Add(New 顧客 With {.ID = 456, .FullName = New 名前 With {.FirstName = "John", .LastName = "Smith"}})
        list.Add(New 顧客 With {.ID = 789, .FullName = New 名前 With {.FirstName = "Mary", .LastName = "Smith"}})
        '以下のコードにコメントしたように、オブジェクトの作成、プロパティの設定、そしてそれらをコレクションに追加する古い方法を使用できます。
        'Customer customer;
        'Name name;
        'customer = new Customer();
        'customer.ID = 123;
        'name = new Name();
        'name.FirstName = "John";
        'name.LastName = "Smith";
        'customer.FullName = name;
        'list.Add(customer);
        'customer = new Customer();
        'customer.ID = 456;
        'name = new Name();
        'name.FirstName = "Mike";
        'name.LastName = "Smith";
        'customer.FullName = name;
        'list.Add(customer);
        'customer = new Customer();
        'customer.ID = 789;
        'name = new Name();
        'name.FirstName = "Mary";
        'name.LastName = "Smith";
        'customer.FullName = name;
        'list.Add(customer);
        Return リスト
    End Function
End Class
Public Class 顧客
    Implements INotifyPropertyChanged
    Private _id As Integer
    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            If _id <> value Then
                _id = value
                OnPropertyChanged("ID")
            End If
        End Set
    End Property
    Private _fullName As 名前
    Public Property FullName() As 名前
        Get
            Return _fullName
        End Get
        Set(ByVal value As Name)
            If _fullName <> value Then
                _fullName = value
                OnPropertyChanged("FullName")
            End If
        End Set
    End Property
#Region "INotifyPropertyChanged Members"
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
#End 地域
    Private Sub OnPropertyChanged(ByVal PropertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class
Public Class 名前
    Implements INotifyPropertyChanged
    Private _firstName As String
    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal value As String)
            _firstName = value
            OnPropertyChanged("FirstName")
        End Set
    End Property
    Private _lastName As String
    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(ByVal value As String)
            _lastName = value
            OnPropertyChanged("LastName")
        End Set
    End Property
#Region "Overrides"
    Public Shared Operator =(ByVal a As Name, ByVal b As Name) As Boolean
        If Object.ReferenceEquals(a, Nothing) AndAlso Object.ReferenceEquals(b, Nothing) Then
            Return True
        ElseIf Object.ReferenceEquals(a, Nothing) OrElse Object.ReferenceEquals(b, Nothing) Then
            Return False
        End If
        Return a.FirstName = b.FirstName AndAlso a.LastName = b.LastName
    End Operator
    Public Shared Operator <>(ByVal a As Name, ByVal b As Name) As Boolean
        Return Not (a = b)
    End Operator
    Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
        Dim b As Name = TryCast(obj, Name)
        Return Me = b
    End Function
    Public Overloads Overrides Function GetHashCode() As Integer
        Return (Me.FirstName + " " + Me.LastName).GetHashCode()
    End Function
#End Region
#Region "INotifyPropertyChanged Members"
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
#End 地域
    Private Sub OnPropertyChanged(ByVal PropertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class

C# の場合:

using System.Collections.ObjectModel;
using System.ComponentModel;
namespace IGDocumentation
{
    public static class CustomerData
    {
        public static ObservableCollection<Customer> GetCustomers()
        {
            ObservableCollection<Customer> list = new ObservableCollection<Customer>();
            //Object Initializers を使用して Customers を ObservableCollection に追加します。
            list.Add(new Customer { ID = 123, FullName = new Name { FirstName = "John", LastName = "Smith" } });
            list.Add(new Customer { ID = 456, FullName = new Name { FirstName = "John", LastName = "Smith" } });
            list.Add(new Customer { ID = 789, FullName = new Name { FirstName = "Mary", LastName = "Smith" } });
            //以下のコードにコメントしたように、オブジェクトの作成、プロパティの設定、そしてそれらをコレクションに追加する古い方法を使用できます。
            //Customer customer;
            //Name name;
            //customer = new Customer();
            //customer.ID = 123;
            //name = new Name();
            //name.FirstName = "John";
            //name.LastName = "Smith";
            //customer.FullName = name;
            //list.Add(customer);
            //customer = new Customer();
            //customer.ID = 456;
            //name = new Name();
            //name.FirstName = "Mike";
            //name.LastName = "Smith";
            //customer.FullName = name;
            //list.Add(customer);
            //customer = new Customer();
            //customer.ID = 789;
            //name = new Name();
            //name.FirstName = "Mary";
            //name.LastName = "Smith";
            //customer.FullName = name;
            //list.Add(customer);
            return list;
        }
    }
    public class Customer:INotifyPropertyChanged
    {
        private int _id;
        public int ID
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    _id = value;
                    OnPropertyChanged("ID");
                }
            }
        }
        private Name _fullName;
        public Name FullName
        {
            get { return _fullName; }
            set
            {
                if (_fullName != value)
                {
                    _fullName = value;
                    OnPropertyChanged("FullName");
                }
            }
        }
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public class Name:INotifyPropertyChanged
    {
        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName");
            }
        }
        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                OnPropertyChanged("LastName");
            }
        }
        #region Overrides
        public static bool operator ==(Name a, Name b)
        {
            if (object.ReferenceEquals(a, null) && object.ReferenceEquals(b, null))
            {
                return true;
            }
            else if (object.ReferenceEquals(a, null) || object.ReferenceEquals(b, null))
            {
                return false;
            }
            return a.FirstName == b.FirstName && a.LastName == b.LastName;
        }
        public static bool operator !=(Name a, Name b)
        {
            return !(a == b);
        }
        public override bool Equals(object obj)
        {
            Name b = obj as Name;
            return this == b;
        }
        public override int GetHashCode()
        {
            return (this.FirstName + " " + this.LastName).GetHashCode();
        }
        #endregion
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}