Re: COM interop and Object Required

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance





"Anthony Jones" wrote:

"pblackburn" <pblackburn@xxxxxxxxxxxxxxxx> wrote in message
news:EE783BF0-25CF-46A0-8FB0-87BEEEA4490D@xxxxxxxxxxxxxxxx
Summary
Getting the error message Run time error 424 Object Required when
attempting
to assign a value to a C# COM visible property of type object.

I have created a COM visible class as below

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ComObjectTest
{
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("6350DE7F-8F6E-4623-88B1-C218CB557BBB")]
public interface IComInterface
{
string String {get;set;}
int Long {get;set;}
DateTime Date {get;set;}
Object Value {get;set;}
}


[Guid("42653F00-FD97-4a8d-AF1B-9CB81842C922")]
[ClassInterface(ClassInterfaceType.None),Serializable]
public class ComObject : IComInterface
{

Object m_Value = null;
string m_String = string.Empty;
int m_Integer = 0;
DateTime m_Date = new DateTime();

public ComObject()
{
}

#region IComInterface Members

public Object Value
{
get {return m_Value;}
set {m_Value = value;}
}

public string String
{
get {return m_String;}
set { m_String = value;}
}

public int Long
{
get { return m_Integer; }
set { m_Integer = value;}
}

public DateTime Date
{
get { return m_Date; }
set { m_Date = value; }
}

}
#endregion
}

In VB6 I have added a reference to this class.
The class is displayed as expected in the VB6 object viewer

I have the following VB6 code

Dim o As ComObject

Set o = New ComObject

o.Date = Now()
o.Long = 100
o.String = "Hello World"
o.Value = 123

The final line causes the Object expected error.

Could someone please explain where I am going wrong?



You have the Value property defined as:-

Object Value {get;set;}


on the interface.

Hence VB6 would expect you to use:-

Set o.Value = <some expression the results in an object reference>

C# will cause a value type such as an int to be boxed as a reference type
when assigned to a variable that has the Object type. VB6 doesn't do that
hence attempting to assign an int to an interface member that is expecting
an object will fail.



--
Anthony Jones - MVP ASP/ASP.NET

Thanks
I think the fact that the COM object browser in VB had this property of type
Variant lead me to believe I could simply assign a value. I can Set the
Value to anopther object as you suggested using the SET o.Value = ? method.
So my next question is how do I expose a C# property to VB as a VARIANT?

Thanks again
.



Relevant Pages

  • Re: COM interop and Object Required
    ... string String ... int Long ... public class ComObject: IComInterface ... In VB6 I have added a reference to this class. ...
    (microsoft.public.dotnet.languages.csharp)
  • COM interop and Object Required
    ... to assign a value to a C# COM visible property of type object. ... string String ... public class ComObject: IComInterface ... In VB6 I have added a reference to this class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Gcc compatible header file
    ... A string collection is a table of zero terminated strings that will grow ... typedef struct _StringCollection StringCollection; ... int; ... bool; ...
    (comp.lang.c)