CType if I *know* that its the same?

F

Fredrik Melin

I have the following code that I get "Cannot convert from
DacsaSupport.Inventory to DacsaNET.Inventory"


Public Function RetrieveItem(ByVal Product_ID As String) As Inventory
Dim RemoteInventory As New DacsaSupport.Inventory
If RemoteInventory.Populate(Product_ID) Then
Return CType(RemoteInventory, Inventory)
End If
End Function

The thing is that I know its the same type of object, because they actually
use the same vb file, but of course .NET thinks he cannot convert because
they are from diffrent assemblies.

Is there a way to trick it to convert anyway? Else I can fix it by using
reflection and looping through all variables copying them, but I hoped for
an easier solution.

The reason why I am doing this is DacsaSupport have the functionallity to
connect to a diffrent company, so what the function does is a copy function
between diffrent companies...
 
D

Dan Forster

Fredrik,

This may not be a solution that you would consider if you are already far
down the track but...

Why not inherit from a base class? That way you can work with the base class
while having the two distinct classes in your DacsaSupport and DacsaNET
assemblies.

You are then only ever casting to the base class and having a shared
assembly. (Presumably the two objects differ in some way otherwise you would
have used a shared assembly for the object already?)

Dan
 
A

Armin Zingler

Fredrik Melin said:
I have the following code that I get "Cannot convert from
DacsaSupport.Inventory to DacsaNET.Inventory"


Public Function RetrieveItem(ByVal Product_ID As String) As
Inventory
Dim RemoteInventory As New DacsaSupport.Inventory
If RemoteInventory.Populate(Product_ID) Then
Return CType(RemoteInventory, Inventory)
End If
End Function

The thing is that I know its the same type of object, because they
actually use the same vb file, but of course .NET thinks he cannot
convert because they are from diffrent assemblies.

Then it is not the same type. They only happen to have the same name. Put
the Inventory class in a classlibrary and use it in the other assembly, then
you refer to the *same* type.
 
M

Maxim V. Karpov

Fredrik,
It is possible for two independent Classes to be converted form one type to
another if they both implement the same interface; therefore interface will
be common deliminator for you.

Maxim

[www.ipattern.com do you?]
 
J

Jay B. Harlow [MVP - Outlook]

Fredrik,
The thing is that I know its the same type of object, because they actually
use the same vb file, but of course .NET thinks he cannot convert because
they are from diffrent assemblies.
As the others have suggested, you have TWO distinct types!


If you are sharing the source file, then each assembly has a distinct type,
hence the casting problem. You need share an assembly between both programs.
Remember that a "Type" (a class, interface, structure) is known to
the CLR not just by the Class name itself. The Assembly, Version, and
Namespace & some other stuff are also used to create a fully qualified type
name.

For details on the "Fully Qualified Type Names" see:

http://msdn.microsoft.com/library/d...ml/cpconSpecifyingFullyQualifiedTypeNames.asp

Is there a way to trick it to convert anyway? Else I can fix it by using
reflection and looping through all variables copying them, but I hoped for
an easier solution.

No "Tricks" needed, if you define the class in a shared assembly it will
work as you expect, no extra code needed!
The reason why I am doing this is DacsaSupport have the functionallity to
connect to a diffrent company, so what the function does is a copy function
between diffrent companies...
I would recommend you put the base Inventory definition in its own Assembly
each company would refer to this Assembly deriving their Inventory from the
base Inventory, the common code would only use the base Inventory...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top