Example Step 2: Create the VirtualGiftCertificateCollection Class
This topic describes how to create a new class that represents a collection of virtual gift certificates.
To create the VirtualGiftCertificateCollection class
In Visual Studio, open the VirtualGiftCertificates project that you created by following the instructions in the topic Example Step 1: Create the VirtualGiftCertificate Class.
Define a new class named VirtualGiftCertificateCollection in the VirtualGiftCertificates namespace. Add the Serializable attribute to the class definition, and indicate that the class implements the ICollection and ISerializable interfaces.
Define the following private member variables:
virtualGiftCertificates: to store the collection of virtual gift certificates.
parentOrderForm: to notify the OrderForm object of any changes to the virtual gift certificate collection.
Any additional member variables that you need in order to store the state of the object.
Define a constructor for the VirtualGiftCertificateCollection class. Make the constructor initialize the member variables.
Define a deserialization constructor that creates an instance of the VirtualGiftCertificateCollection class from serialization information. Within the deserialization constructor, retrieve the values of the member variables of the object.
Define the GetObjectData method, and add the SerializationFormatter security demand. Within the GetObjectData method, serialize the value of each member variable.
Implement the ICollection interface by defining the Count, IsSynchronized, and SyncRoot properties and the GetEnumerator method.
Define an integer-based indexer that returns the specified element of the collection.
Define an Add method that adds a new VirtualGiftCertificate object to the collection. Within the Add method, set the ParentOrderForm property of the VirtualGiftCertificate object, and call the SetChildDirty method of the OrderForm object.
Define a Remove method that removes a VirtualGiftCertificate object from the collection and reverses the other actions that the Add method performed.
Define a Clear method that initializes a VirtualGiftCertificateCollection object and then calls the SetChildDirty method of the OrderForm object.
Define a CopyTo method.
Example
The following example defines the VirtualGiftCertificateCollection class and the methods that the class requires to be associated with a Commerce Server Core Systems order.
You can find the source code for the complete sample in the %COMMERCE_SERVER_ROOT%/SDK/Samples/OrdersExtensibility folder.
[Serializable]
public class VirtualGiftCertificateCollection : ICollection, ISerializable
{
private ArrayList virtualGiftCertificates;
private OrderForm parentOrderForm;
public VirtualGiftCertificateCollection(OrderForm parentOrderForm)
{
if (parentOrderForm == null)
{ throw new ArgumentNullException("parentOrderForm"); }
this.virtualGiftCertificates = new ArrayList();
this.parentOrderForm = parentOrderForm;
}
public VirtualGiftCertificateCollection(SerializationInfo info, StreamingContext context)
{
this.parentOrderForm = (OrderForm)info.GetValue("ParentOrderForm", typeof(OrderForm));
this.virtualGiftCertificates = (ArrayList)info.GetValue("VirtualGiftCertificates", typeof(ArrayList));
}
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("ParentOrderForm", this.parentOrderForm);
info.AddValue("VirtualGiftCertificates", this.virtualGiftCertificates);
}
public int Count
{
get
{
return this.virtualGiftCertificates.Count;
}
}
public bool IsSynchronized
{
get
{
return false;
}
}
public Object SyncRoot
{
get
{
return this;
}
}
public IEnumerator GetEnumerator()
{
return this.virtualGiftCertificates.GetEnumerator();
}
public VirtualGiftCertificate this[int index]
{
get
{
return (VirtualGiftCertificate)this.virtualGiftCertificates[index];
}
}
public void Add(VirtualGiftCertificate vgc)
{
if (vgc.ParentOrderForm != null)
{
throw new ArgumentException ("Virtual Gift Certificate is already a member of a collection.");
}
vgc.SetParent(this.parentOrderForm);
this.virtualGiftCertificates.Add(vgc);
this.parentOrderForm.SetChildDirty();
}
public void Remove(VirtualGiftCertificate vgc)
{
if (vgc == null)
{
throw new ArgumentNullException ("Virtual Gift Certificate");
}
this.virtualGiftCertificates.Remove(vgc);
vgc.SetParent(null);
this.parentOrderForm.SetChildDirty();
}
public void Clear()
{
foreach (VirtualGiftCertificate vgc in virtualGiftCertificates)
{
vgc.ParentOrderForm = null;
}
this.virtualGiftCertificates.Clear();
this.parentOrderForm.SetChildDirty();
}
void ICollection.CopyTo(Array array, int index)
{
this.virtualGiftCertificates.CopyTo(array, index);
}
}