title: Get, "cannot cast 'Field' to 'Field' type (same class)" when trying to Append an object in vb6 from C# library using COM-interop
body: <p>I have a C# library that is used by legacy vb6 code.  This works really well for most everything except I'm running into an issue appending an object.</p>
  <p>In part I'm attempting to emulate some of the access db function calls and redirect them to a different db solution.  But that isn't very relevant to the issue I'm running into, just some background.</p>
  <p>I have another object and class very similar to this one; does the same thing too with an Append.  The code is only different in that it's not an &quot;object&quot; that is used but a fixed type.  I tried doing it this way as generics are not supported in the COM standard.</p>
  <p>Any ideas or help would be very helpful.</p>
  <h2>In vb6</h2>
  <pre><code>Dim fld As Field
  Set fld = new Field
  NewTd.Fields.Append fld     'gives an error that Field can't be cast to type Field. 
  </code></pre>
  <h2>C#</h2>
  <p>Field:</p>
  <pre><code> [Guid(&quot;2515418d-04af-484e-bb3b-fe53a6121f73&quot;)]
   [ClassInterface(ClassInterfaceType.None)]
   public class Field : IField
   {
       public string Name { get; set; }
       public string Value { get; set; }
  
       public int Type { get; set; } //private int TypePV;
       // 3 more public ints.
  
  
       enum FieldType
       {
           // different field types.
       }
  
  ---
  
   [Guid(&quot;fd362bff-da4e-4419-a379-1ed84ff74f1b&quot;)]
   public interface IField
   {
          string Name { get; set; }
          string Value { get; set; }
          int Type { get; set; }
          // three more ints.
  
   }
  </code></pre>
  <p>Where Append is defined.</p>
  <pre><code>[Guid(&quot;0cf3c33f-8ca8-4a5b-8382-d1612558fcad&quot;)]
  [ClassInterface(ClassInterfaceType.None)]
  public class FieldsO : IFieldsO
  {
      public object obj;
      public FieldsO(object cobj)
      {
          obj = cobj;
      }
  
      public object this[string param]
      {
          get 
          {
              if (obj.GetType() == typeof(Recordset))
                  return ((Recordset)obj)[param];
              if (obj.GetType() == typeof(TableDef))
                  return ((TableDef)obj)[param];
              return null;
          }
          set 
          { 
              if (obj.GetType() == typeof(Recordset))
                  ((Recordset)obj)[param] = value; 
              if (obj.GetType() == typeof(TableDef))
                  ((TableDef)obj)[param] = (IField)value; 
          }
      }
  
      public void Append(Field io) {
          if (obj.GetType() == typeof(TableDef)) {
              if (!((TableDef)obj)._Fields.ContainsKey(io.Name))
                  ((TableDef)obj)._Fields.Add(io.Name, io);
          }
      }
  
      public int Count() {
          if (obj.GetType() == typeof(Recordset))
              return ((Recordset)obj).dt.Columns.Count;
          if (obj.GetType() == typeof(TableDef))
              return ((TableDef)obj)._Fields.Count;
          return -1;
      }
  }
  
  ---
  
   [Guid(&quot;fe528160-1505-448e-bb9c-8ccfd9e3643d&quot;)]
   public interface IFieldsO
      {
         object this[string param] { get; set; }
         int Count();
         void Append(Field io);
      }
  </code></pre>