Search code examples
c#comvb6com-interop

Get, "cannot cast 'Field' to 'Field' type (same class)" when trying to Append an object in vb6 from C# library using COM-interop


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.

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.

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 "object" that is used but a fixed type. I tried doing it this way as generics are not supported in the COM standard.

Any ideas or help would be very helpful.

In vb6

Dim fld As Field
Set fld = new Field
NewTd.Fields.Append fld     'gives an error that Field can't be cast to type Field. 

C#

Field:

 [Guid("2515418d-04af-484e-bb3b-fe53a6121f73")]
 [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("fd362bff-da4e-4419-a379-1ed84ff74f1b")]
 public interface IField
 {
        string Name { get; set; }
        string Value { get; set; }
        int Type { get; set; }
        // three more ints.

 }

Where Append is defined.

[Guid("0cf3c33f-8ca8-4a5b-8382-d1612558fcad")]
[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("fe528160-1505-448e-bb9c-8ccfd9e3643d")]
 public interface IFieldsO
    {
       object this[string param] { get; set; }
       int Count();
       void Append(Field io);
    }

Solution

  • The answer was simple. I changed the code to expect an "object" on Append and found that the vb6 error code was more specific. It was having issues with where the code originated from. From there all i did was make the project (compile it into an executable) and everything worked as it should, no exceptions, nothing.

    The solution, as it often seems to be, is not to depend on the vb6 IDE when dealing with odd errors.