Namespace and COM library nme conflict

  • Thread starter Thread starter James Bradley
  • Start date Start date
J

James Bradley

Hi Folks,

I am coming up to speed with C# and I have run into an interesting
issue.

I have an old VB COM library that I need to access with a ProgId ==
'xyz'

I also have a C# class that is in a namespace like this:
"A.xyz.abc..."

I have added the COM library as a reference, but when I try to
reference out of the COM library, the compiler and Intellisense don't
see it:

xyz.objectname Temp = new xyz.objectname();

This fails, because it is looking in the namespace a.xyz for
objectname, not in the COM object.

Is there a way I can explicitly reference the object in the COM
library, or is there a way to alias the COM library?

Thanks!

Jami Bradley
reverse 2nd level domain: (e-mail address removed)
 
James,

You should be able to alias the namespace like this:

// Alias the A.xyz namespace.
using Axyz = A.xyz;

Then, you should be able to use:

// The class in A.xyz.
Axyz.abc pobj1 = new Axyz.abc;

// The com class.
xyz.objectname pobj2 = new xyz.objectname();

Hope this helps.
 
I saw that option in the using statement and thought that it would
work. Unfortunately, my problem is that I am *in* the namespace
A.xyz.abc, so I don't think I can alias my own namespace.

It looks something like this:

namespace A.xyz.abc
{
public class def
{
public void func()
{
// This statement attempts to access A.xyz.objectname:
xyz.objectname pobj2 = new xyz.objectname();
}
}
}
 
James,

You can try to alias each object that you want to use, but I can see how
this could be unwieldy. Also, you could create the interop assembly
yourself using the TLBIMP program and then use the /namespace option to
indicate a different namespace which doesn't conflict.
 
Thanks! I'll look into tlbimp and those options - I haven't done that
process manually with vs.net yet :-)

Is it possible to alias a COM object? For example, if I have
xyz.object1 in my COM library, can I use some alias to access that? I
thought the aliasing was only for the namespaces. I only have 3 or 4
objects like this, so individual aliasing is not all that bad.

Jami
 
James,

Yes, you can alias individual types, like so:

// Alias the xyz.object type.
using MyComObject = xyz.object;

This will alias the one type with MyComObject. It ^should^ work, but
being in the namespace, I think you will still have problems (call it a
hunch). However, it wouldn't be so hard to try.
 
I tried aliasing the COM object and it worked great! Because the
using is outside of the namespace declaration, it had no trouble
finidng the COM object.

Here is what I ended up using:
using MyComLibrary = xyz;

and now I can access it directly:
MyComLibrary.object pObj = new MyComLibrary.object();

Thank you for all your help!

Jami
 
James,
In addition to Nicholas's suggestions:

Have you tried to create an alias for the COM namespace before you enter the
A namespace?

using xyzcom = xyz
namespace A.xyz.abc
{
xyzcom.objectname pobj2 = new xyzcom.objectname();

Hope this helps
Jay
 
I tried aliasing the COM object and it worked great! Because the
using is outside of the namespace declaration, it had no trouble
finidng the COM object.

Here is what I ended up using:
using MyComLibrary = xyz;

and now I can access it directly:
MyComLibrary.object pObj = new MyComLibrary.object();

Thank you for all your help!

Jami
 
Back
Top