tags:

views:

31

answers:

1

I have a library X, which has a class C which used component Y. However, Y isn't available in 64 bit, so it's been replaced by component Z, but I want to still use Y when available.

So I would like to reference Y and Z and then have

C.vb:

#If 32bit then
Class C
// implementation of C using Y
End Class
#End If

C64.vb

#If 64bit then
Public Class C
// implementation of C using Z
End Class
#End If

Note: C-style Comments due to highlighting errors with vb comments.

The problem, as I see it, is that I will have a reference to Y in the 64 bit version(it's a COM object, so it would be an interop assembly, if that makes a difference). Assuming Y is not called from anywhere in the code, will I be able to instantiate C?

+1  A: 

You're okay on several levels. The #if ensures that your code doesn't use any types from Y, the compiler will actually remove the assembly reference from the final assembly. Even if you did have references to Y types in your code, they can actually be JIT compiled because you do have valid metadata for it. You've got the interop assembly.

The only thing you can't do is create the COM object and call its methods. You can use a regular if rather than #if. Allowing you to avoid building separate assemblies. Testing IntPtr.Size is a good way to find out if you are running in 64-bit mode.

Hans Passant
Thank you, Hans. Y has some functionality which Z doesn't have, and I think it's possible that in some cases I could want to use the 32-bit assembly on a 64-bit system. I would need to build a specific 32-bit version for this, wouldn't I?
Jaime Pardos
Just set the Platform Target setting of the EXE project to x86 and you can always use Y, even on a 64-bit operating system.
Hans Passant
Thank you very much, Hans. You're really helpful. I was asking this because in this case I'd lose the advantages of using a regular "if" instead of "#if" (I have to maintain both versions anyway).
Jaime Pardos