ArcObjects Library Reference  

TestClass

About the Clonable object Sample

[C#]

TestClass.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ClonableObject;

namespace TestApp
{
  public sealed class TestClass
  {
    #region class constructor (empty constructor)
    public TestClass()
    {

    }
    #endregion

    public void Test()
    {
      Console.WriteLine("Creating an instance of the clonable object.");
      ClonableObjClass cloneable = new ClonableObjClass();
      
      Console.WriteLine("Assigning properties");
      cloneable.SpatialReference = CreateGeographicSpatialReference();
      cloneable.Name = "test 1";
      cloneable.Point = new PointClass();
      cloneable.Point.PutCoords(35.02,31.4);

      cloneable.ManagedArray = new ArrayList();
      cloneable.ManagedArray.Add("One");
      cloneable.ManagedArray.Add("Two");
      cloneable.ManagedArray.Add("Three");

      Console.WriteLine("New object's properties");
      Console.WriteLine("-----------------------");
      Console.WriteLine("Name: " + cloneable.Name);
      Console.WriteLine("ID: " + cloneable.ID.ToString());
      Console.WriteLine("Version: " + cloneable.Version.ToString());
      Console.WriteLine("Array values: ");
      foreach (object obj in cloneable.ManagedArray)
      {
        Console.WriteLine((string)obj);
      }
      Console.WriteLine("Spatial Reference parameters:");
      Console.WriteLine("Name: " + cloneable.SpatialReference.Name);
      IGeographicCoordinateSystem gcs = (IGeographicCoordinateSystem)cloneable.SpatialReference;
      Console.WriteLine("Alias: " + gcs.Alias);
      Console.WriteLine("Datum: " + gcs.Datum.Name);
      Console.WriteLine("");


      Console.WriteLine("Cloning the object...");
      ClonableObjClass clonee = cloneable.Clone() as ClonableObjClass;
      Console.WriteLine("");
      Console.WriteLine("Cloned object's properties:");
      Console.WriteLine("---------------------------");
      Console.WriteLine("Name: " + clonee.Name);
      Console.WriteLine("ID: " + clonee.ID.ToString());
      Console.WriteLine("Version: " + clonee.Version.ToString());
      Console.WriteLine("Array values: ");
      foreach (object obj in clonee.ManagedArray)
      {
        Console.WriteLine((string)obj);
      }
      Console.WriteLine("Spatial Reference parameters:");
      Console.WriteLine("Name: " + clonee.SpatialReference.Name);
      gcs = (IGeographicCoordinateSystem)clonee.SpatialReference;
      Console.WriteLine("Alias: " + gcs.Alias);
      Console.WriteLine("Datum: " + gcs.Datum.Name);
      Console.WriteLine("");    
       
    }

    /// <summary>
    /// create a crazy coordinate system
    /// </summary>
    /// <returns></returns>
    private ISpatialReference CreateGeographicSpatialReference()
    {
      ISpatialReferenceFactory spatialRefFatcory = new SpatialReferenceEnvironmentClass();
      IGeographicCoordinateSystem geoCoordSys;
      geoCoordSys = spatialRefFatcory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

      //assign a user defined datum to the SR (just for the test)
      IDatum datum = new DatumClass();
      IDatumEdit datumEdit = (IDatumEdit)datum;
      ISpheroid spheroid = spatialRefFatcory.CreateSpheroid((int)esriSRSpheroidType.esriSRSpheroid_Clarke1880);
      datumEdit.DefineEx("MyDatum", "My Datum", "MDTM", "", spheroid);

      IPrimeMeridian primeMeridian = spatialRefFatcory.CreatePrimeMeridian((int)esriSRPrimeMType.esriSRPrimeM_Greenwich);
      IAngularUnit angularUnits = spatialRefFatcory.CreateUnit((int)esriSRUnitType.esriSRUnit_Degree) as IAngularUnit;
      IGeographicCoordinateSystemEdit geoCoordSysEdit = (IGeographicCoordinateSystemEdit)geoCoordSys;
      geoCoordSysEdit.DefineEx("MyGeoCoordSys", "MyGeoCoordSys", "MGCS", "", "", datum, primeMeridian, angularUnits);
      
      geoCoordSys.SetFalseOriginAndUnits(-180.0, -180.0, 5000000.0);
      geoCoordSys.SetZFalseOriginAndUnits(0.0, 100000.0);
      geoCoordSys.SetMFalseOriginAndUnits(0.0, 100000.0);

      return geoCoordSys as ISpatialReference;
    }

  }
}

[Visual Basic .NET]

TestClass.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports ClonableObject

Public NotInheritable Class TestClass
#Region "class constructor (empty constructor)"
  Public Sub New()

  End Sub
#End Region

  Public Sub Test()
    Console.WriteLine("Creating an instance of the clonable object.")
    Dim cloneable As ClonableObjClass = New ClonableObjClass()

    Console.WriteLine("Assigning properties")
    cloneable.SpatialReference = CreateGeographicSpatialReference()
    cloneable.Name = "test 1"
    cloneable.Point = New PointClass()
    cloneable.Point.PutCoords(35.02, 31.4)

    cloneable.ManagedArray = New ArrayList()
    cloneable.ManagedArray.Add("One")
    cloneable.ManagedArray.Add("Two")
    cloneable.ManagedArray.Add("Three")

    Console.WriteLine("New object's properties")
    Console.WriteLine("-----------------------")
    Console.WriteLine("Name: " & cloneable.Name)
    Console.WriteLine("ID: " & cloneable.ID.ToString())
    Console.WriteLine("Version: " & cloneable.Version.ToString())
    Console.WriteLine("Array values: ")
    For Each obj As Object In cloneable.ManagedArray
      Console.WriteLine(CStr(obj))
    Next obj
    Console.WriteLine("Spatial Reference parameters:")
    Console.WriteLine("Name: " & cloneable.SpatialReference.Name)
    Dim gcs As IGeographicCoordinateSystem = CType(cloneable.SpatialReference, IGeographicCoordinateSystem)
    Console.WriteLine("Alias: " & gcs.Alias)
    Console.WriteLine("Datum: " & gcs.Datum.Name)
    Console.WriteLine("")


    Console.WriteLine("Cloning the object...")
    Dim clonee As ClonableObjClass = TryCast(cloneable.Clone(), ClonableObjClass)
    Console.WriteLine("")
    Console.WriteLine("Cloned object's properties:")
    Console.WriteLine("---------------------------")
    Console.WriteLine("Name: " & clonee.Name)
    Console.WriteLine("ID: " & clonee.ID.ToString())
    Console.WriteLine("Version: " & clonee.Version.ToString())
    Console.WriteLine("Array values: ")
    For Each obj As Object In clonee.ManagedArray
      Console.WriteLine(CStr(obj))
    Next obj
    Console.WriteLine("Spatial Reference parameters:")
    Console.WriteLine("Name: " & clonee.SpatialReference.Name)
    gcs = CType(clonee.SpatialReference, IGeographicCoordinateSystem)
    Console.WriteLine("Alias: " & gcs.Alias)
    Console.WriteLine("Datum: " & gcs.Datum.Name)
    Console.WriteLine("")

  End Sub

  ''' <summary>
  ''' create a crazy coordinate system
  ''' </summary>
  ''' <returns></returns>
  Private Function CreateGeographicSpatialReference() As ISpatialReference
    Dim spatialRefFatcory As ISpatialReferenceFactory = New SpatialReferenceEnvironmentClass()
    Dim geoCoordSys As IGeographicCoordinateSystem
    geoCoordSys = spatialRefFatcory.CreateGeographicCoordinateSystem(CInt(Fix(esriSRGeoCSType.esriSRGeoCS_WGS1984)))

    'assign a user defined datum to the SR (just for the test)
    Dim datum As IDatum = New DatumClass()
    Dim datumEdit As IDatumEdit = CType(datum, IDatumEdit)
    Dim spheroid As ISpheroid = spatialRefFatcory.CreateSpheroid(CInt(Fix(esriSRSpheroidType.esriSRSpheroid_Clarke1880)))
    datumEdit.DefineEx("MyDatum", "My Datum", "MDTM", "", spheroid)

    Dim primeMeridian As IPrimeMeridian = spatialRefFatcory.CreatePrimeMeridian(CInt(Fix(esriSRPrimeMType.esriSRPrimeM_Greenwich)))
    Dim angularUnits As IAngularUnit = TryCast(spatialRefFatcory.CreateUnit(CInt(Fix(esriSRUnitType.esriSRUnit_Degree))), IAngularUnit)
    Dim geoCoordSysEdit As IGeographicCoordinateSystemEdit = CType(geoCoordSys, IGeographicCoordinateSystemEdit)
    geoCoordSysEdit.DefineEx("MyGeoCoordSys", "MyGeoCoordSys", "MGCS", "", "", datum, primeMeridian, angularUnits)

    geoCoordSys.SetFalseOriginAndUnits(-180.0, -180.0, 5000000.0)
    geoCoordSys.SetZFalseOriginAndUnits(0.0, 100000.0)
    geoCoordSys.SetMFalseOriginAndUnits(0.0, 100000.0)

    Return TryCast(geoCoordSys, ISpatialReference)
  End Function

End Class