Re: COM interop and Object Required
- From: pblackburn <pblackburn@xxxxxxxxxxxxxxxx>
- Date: Thu, 24 Jul 2008 07:34:03 -0700
"Anthony Jones" wrote:
"pblackburn" <pblackburn@xxxxxxxxxxxxxxxx> wrote in messageThanks
news:EE783BF0-25CF-46A0-8FB0-87BEEEA4490D@xxxxxxxxxxxxxxxx
Summaryattempting
Getting the error message Run time error 424 Object Required when
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
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
.
- Follow-Ups:
- Re: COM interop and Object Required
- From: "Jialiang Ge [MSFT]"
- Re: COM interop and Object Required
- References:
- COM interop and Object Required
- From: pblackburn
- Re: COM interop and Object Required
- From: Anthony Jones
- COM interop and Object Required
- Prev by Date: Re: tableadapters, Stored Procedure and temp tables
- Next by Date: Dynamic XLinq
- Previous by thread: Re: COM interop and Object Required
- Next by thread: Re: COM interop and Object Required
- Index(es):
Relevant Pages
|