You are here: AxTools > CodeSMART 2009 for VS.NET > Documentation > User's Guide > Advanced Code Builders > Implementing Common Object Operations
While browsing this page, why not start downloading the evaluation setup kit (direct download, no questions asked).

Implementing Common Object Operations
Applies to: Visual Studio .NET 2002, Visual Studio .NET 2003, Visual Studio 2005, Visual Studio 2008

The common operations that all objects usually exhibit are equality checking (the Equals method) and cloning (the Clone method).

a) Because Equals is defined by System.Object and as every type is ultimately derived from this base, it results that every type features a basic Equals method. However, in terms of implementation that will definitely not fit most derived types, so you will find yourself in the position of having to override the Equals method. Unfortunately, implementing your own version of Equals is not as easy and straightforward as you might expect because you should also override the GetHashCode method to ensure that there is a correspondence between the algorithm used to implement the object's equality and the algorithm used for computing its hash.

b) For similar reasons, you may need to define means of creating copies of objects by using other approaches than the one provided by the default Clone method. The ICloneable interface does not explicitly state whether the Clone method implements a shallow copy or a deep copy, so you might want to provide your own implementation of the Clone method in the form that makes more sense for your types.

To implement common object operations for a type:

  1. Open the Visual Basic or C# code module containing the type you want to implement common operations for and place the cursor inside that type's code.
  2. In the CodeSMART main menu, select the Implement Common Object Operations entry from the Code Builder Tools popup menu, Implement Advanced Code secondary popup, or choose the Common Object Operations entry from the Implement toolbar popup (quick toolbar and menu references are available).

    The Implement Common Object Operations dialog will be displayed:
  3. Override de default 'Equals' and 'GetHashCode' methods: check this option if you want to provide your own implementation for the Equals and GetHashCode methods. If you do, you must specify if the type is a value or a reference type. For reference types, you can also add a call to the base type's Equals method implementation by if checking the corresponding option. You can choose to overload the equality and inequality operators (option not applicable to VB.NET in Visual Studio 2003).
  4. Implement the 'ICloneable' interface and add the 'Clone' method: check this option if you want your type to provide a custom cloning mechanism.
    • Make a shallow copy: check this option when you want to implement your type's Clone method by calling System.Object's protected MemberwiseClone method. A shallow copy it is said to be created when an object's fields are copied but what the fields refer to is not copied (that is, the new object's field data will point to the original object's field data).
    • Make a deep copy: check this option when you want to implement a Clone method that creates a new object which doesn't share anything with the original. CodeSMART can provide code for deep copying (based on a serialization algorithm) if you check the Generate code for deep cloning option.
  5. Generate XML comments: check this option if you want XML descriptive comments to be generated for the newly inserted code.
  6. You can enclose the generated code between #Region and #End Region directives by checking the corresponding option.
  7. Press the Standards button in order to review the naming standards configuration since this one will be considered when generating code.
  8. Press Insert to generate and insert the desired code based on the above specified settings.
See Also
Advanced Code Builders Overview
Implementing Interfaces
Implementing Object Construction and Destruction
Implementing Member Override
Implementing Object Serialization and Deserialization
Implementing Event Definitions in C#
Inserting Custom Attribute Classes
Inserting Custom Exception Classes
Inserting Custom Formatter Classes
Inserting Strongly Typed Collection Classes

Back to top