calling op_Explicit directly

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 3 classes, A, B, and C (written in C#). The C class has two explicit
conversion operators - one that converts class C to class A and one that
converts class C to class B. I need to be able to call the conversion
operator from C to A. To do this in VB I do...
objA = MyNS.C.op_Explicit(objC,A)
However, since there are two of them I get an error on the call saying
basically the call is ambiguous because there's two conversions that only
differ by return type.

My question is: is there a way to indicate that I want the conversion
operator with a return type of A?
 
Jim,
I can think of two possible solutions:

1. Use VS.NET 2005 (aka Whidbey, currently in beta, due out later in 2005)
as it supports defining & consuming the conversion operators. For details
see: http://lab.msdn.microsoft.com/vs2005/

2. Move one or both of the conversion operators into the other classes.
Instead of defining both in C, consider defining one in A & one in B.

Hope this helps
Jay

|I have 3 classes, A, B, and C (written in C#). The C class has two explicit
| conversion operators - one that converts class C to class A and one that
| converts class C to class B. I need to be able to call the conversion
| operator from C to A. To do this in VB I do...
| objA = MyNS.C.op_Explicit(objC,A)
| However, since there are two of them I get an error on the call saying
| basically the call is ambiguous because there's two conversions that only
| differ by return type.
|
| My question is: is there a way to indicate that I want the conversion
| operator with a return type of A?
|
 
Jim,
Just thought of a couple of other options:

Rather then relay on the explicit conversion operators, define a couple of
VB.NET friendly methods in C#. For example C.FromA & C.FromB. NOTE I would
define both FromA & the explicit conversion operator.

If you are not allowed to modify the C# source, you may need to define a C#
helper function that avoids the explicit conversion operators.

Hope this helps
Jay

|I have 3 classes, A, B, and C (written in C#). The C class has two explicit
| conversion operators - one that converts class C to class A and one that
| converts class C to class B. I need to be able to call the conversion
| operator from C to A. To do this in VB I do...
| objA = MyNS.C.op_Explicit(objC,A)
| However, since there are two of them I get an error on the call saying
| basically the call is ambiguous because there's two conversions that only
| differ by return type.
|
| My question is: is there a way to indicate that I want the conversion
| operator with a return type of A?
|
 
Hi Jay,

thanks for the response.

Unfortunately, #2 isn't an option since they're not my classes.

And on idea #1, the operator overload describes handling of certain
overloaded operators, but it doesn't address how to access different
op_Explicit
operators that only differ by return type. There doesn't appear to be a way
for me to indicate to the VB compiler the return type of op_Explicit.

It doesn't appear VB handles this case. Any other ideas?

Jim
 
Jim,
| And on idea #1, the operator overload describes handling of certain
| overloaded operators, but it doesn't address how to access different
| op_Explicit
| operators that only differ by return type. There doesn't appear to be a
way
| for me to indicate to the VB compiler the return type of op_Explicit.
You are correct for VB.NET 2002 & 2003 as they simply do not support
Operator Overloading. VB.NET 2005 fully supports Operator Overloading, ergo
my suggesting option #2!

op_Explicit is an implementation detail of Overload Operators specifically
Operator CType, VB.NET 2002 & 2003 can call op_Explicit however VB.NET 2002
& 2003 cannot resolve the overloaded return types. VB.NET 2005 will see
op_Explicit as an Overload CType operator & call the correct op_Explicit for
the CType that you are attempting to do. For Example:

' VB.NET 2005 syntax
Dim anA As A
Dim aB As B
Dim aC as C

anA = CType(aC, A) ' calls op_Explicit that returns A

aB = CType(aC, B) ' calls op_Explicit that returns B

For info on overloading the CType operator see:

http://msdn2.microsoft.com/library/yf7b9sy7(en-us,vs.80).aspx


In VB.NET 2005 the op_Explicit routines would be defined something like:

Public Class C

...

Public Shared Narrowing Operator CType(ByVal aC As C) As A
' return a new A object...
End Operator

Public Shared Narrowing Operator CType(ByVal aC As C) As B
' return a new B object...
End Operator

End Class

op_Implicit defines an overloaded Widening Operator CType.

Hope this helps
Jay

| Hi Jay,
|
| thanks for the response.
|
| Unfortunately, #2 isn't an option since they're not my classes.
|
| And on idea #1, the operator overload describes handling of certain
| overloaded operators, but it doesn't address how to access different
| op_Explicit
| operators that only differ by return type. There doesn't appear to be a
way
| for me to indicate to the VB compiler the return type of op_Explicit.
|
| It doesn't appear VB handles this case. Any other ideas?
|
| Jim
|
|
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
|
| > Jim,
| > I can think of two possible solutions:
| >
| > 1. Use VS.NET 2005 (aka Whidbey, currently in beta, due out later in
2005)
| > as it supports defining & consuming the conversion operators. For
details
| > see: http://lab.msdn.microsoft.com/vs2005/
| >
| > 2. Move one or both of the conversion operators into the other classes.
| > Instead of defining both in C, consider defining one in A & one in B.
| >
| > 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

Back
Top