473,232 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 software developers and data experts.

COM interop and Object Required

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),Serializab le]
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?

Many thanks
Jul 24 '08 #1
4 4600
"pblackburn" <pb********@community.nospamwrote in message
news:EE**********************************@microsof t.com...
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),Serializab le]
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
Jul 24 '08 #2


"Anthony Jones" wrote:
"pblackburn" <pb********@community.nospamwrote in message
news:EE**********************************@microsof t.com...
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),Serializab le]
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
Jul 24 '08 #3
Good morning pblackburn. Welcome to Microsoft Newsgroup support service. My
name is Jialiang Ge [MSFT]. It's my pleasure to work with you on this issue.

The symptom you found is caused by a known code defect of Microsoft's
tlbexp. Let's first look at the resolution of the issue, then I will
explain the background information and "WHY" in detail.

**RESOLUTION**
We need to use late binding to set value for the Object property "Value".

Dim obj As Object
Set obj = New ComObject
obj.Value = 123

(Note: obj is declared as Object due for "late binding")

**CAUSE**
Looking at the type library of the .NET component with oleview.exe, you
will find that the Value property is defined in this way:

interface IComInterface : IDispatch {
[id(00000000), propget]
HRESULT Value([out, retval] VARIANT* pRetVal);
[id(00000000), propputref]
HRESULT Value([in] VARIANT pRetVal);
};

The value is exposed as "VARIANT", instead of an Object. (This can answer
your follow up question for Anthony Jones), however, why can't we use it as
a normal VARIANT in VB6 to set an integer? It's because of the setting
"propputref". "propputref" only allows the value to be a reference type. We
need to pass value types instead (specifically an int, string or decimal
value) which doesn't work because the setter is marked as a 'propputref'.
An expected setting is "propput". Microsoft has admitted this code defect
when our tool generates the tlb. The workaround is to use "late binding",
instead of "early binding", as is demonstrated in the "RESOLUTION" part of
my message. Pblackburn, please have a try and let me know whether it works
for you. If you have any other concerns or questions, please DON'T hesitate
to tell me.

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 25 '08 #4
Hello pblackburn,

I am writing to check the status of the issue on your side. Would you mind
letting me know the result of my workarounds? If you need further
assistance, feel free to let me know. I will be more than happy to be of
assistance.

Have a great day!

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================

Jul 29 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nadav | last post by:
Hi, Introduction *************** I have a system build of a collection of 'Native COM objects' and '.NET COM interop' objects, all of the COM objects are managed through a 'Native COM' layer,...
0
by: Mohan Ekambaram | last post by:
Hi, I'm working on a session sharing component that uses db to share the data between asp & aspx pages. Its written in .net & has a wrapper comp in VB6 which can be used to access it from...
4
by: Martin Maat | last post by:
Hi. I am using a COM component from managed code doing the following: Type type = Type.GetTypeFromCLSID(new Guid("B70FAAE6-4F85-480A-B1C5-DC9A6F175BFC"), serverMachineName, true); history =...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
12
by: Anil Krishnamurthy | last post by:
We have an ASP.NET application that uses COM objects through Interop. The web application requires access to network and database resources and hence, needs to impersonate a domain account. The...
8
by: Rob Edwards | last post by:
When trying to add the Microsoft CDO for Exchange Management Library (aka CDOEXM.dll) I receive the following message: "A reference to 'Microsoft CDO for Exchange Management Library' could not be...
3
by: Jeffery Franzen | last post by:
Anyone know where the documentation is regarding Activex controls in asp web forms? I'm using VS.NET 2002 enterprise and am trying to use Activex controls in vb.net web form app. I do the add...
4
by: john | last post by:
I'm having some Interop problems. I really need someone's help on this, i'm running out of ideas. I have upgraded my Data access controls to the latest ver. Now then i have a form with these...
7
by: R Reyes | last post by:
Can someone please explain to me why I can't get the MS Word Interop assembly to work in my VS2005 project? I'm trying to manipulate MS Word from my Web Form application and I can't get passed...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.