Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Interop Inheritance question

Status
Not open for further replies.

redoctober

Programmer
Oct 25, 2000
37
CA
Hi all,
I have a question about inheritance when you get to deal with Interop. I'm building a library in C# (VS2003) which i later want to use in VB6 project. Generally i got it all working except for one thing that puzzles me. I have some basic inheritance in my C# classes. Something like this:
Code:
using System;
using System.Runtime.InteropServices;

namespace MyLib
{
	[Guid("6C6B174A-32BA-4139-838C-DAB72831D7AF")]
	public interface A
	{
		[DispId(1)]
		int mA {set; get;}
	}

	

	[Guid("B5D7B4CF-49FE-403f-8E66-6919FB1901CA")]
	public interface B:A
	{
		[DispId(2)]
		int mB {set; get;}
	}

	[Guid("A5E0F4A2-5740-4d29-992C-31C6D0087E81")]
	[ClassInterface(ClassInterfaceType.None)]
	public class C:B
	{
		private int m_A;
		private int m_B;

		public int mA
		{
			get {return m_A;}
			set {m_A = value;}
		}
		public int mB
		{
			get {return m_B;}
			set {m_B = value;}
		}
	}
}

Basically, i create two interfaces A and B where B inherits A and then i create a class C that inherits interface B. Everything compiles fine, but when i try to use it in VB6 project and i create a variable of class C, the properties of the interface A are not accessible.
Code:
Dim T As MyLib.C
Set T = New MyLib.C
T.mA = 1
This code gives me Run-time error '438': "Object doesn't support the property or method" when i try to assign T.mA. Obviously the inheritance works differently in this case, but i can't figure out how. It seems like a textbook question but i can't seem to find the answer. Does anybody know how to do it properly?

I'd appreciate any help.

Eugene.
 
I'm just guessing here, from what I remember VB6 & COM only support one interface for a reference. So what you have to do is create a second reference variable with the interface type you want and then assign the object to it. Then you can use the functions on the new interface. Like this:

Code:
Dim T As MyLib.C
Set T = New MyLib.C

Dim TinterfaceA As MyLib.A
SET TinterfaceA = T

TinterfaceA.mA = 1
 
Well, that's sort of what i ended up doing for the time being, which seemed like a complete hack. And i thought there's got to be a proper way of doing this. But perhaps there isn't...

Thanks for the reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top