tags:

views:

860

answers:

2

In C# you can use the implicit keyword to define an implicit user-defined type conversion operator.

In VB.NET you can define a CType conversion operator that will explicitly convert a user-defined type into another type.

Is there a way to declare an implicit conversion operator in VB.NET?

I can't seem to find any information on this....

Edit: I found my answer in the MSDN documentation for the Widening operator statement. Apparently the CType Widening operator is "called" for implicit conversions whereas the CType Narrowing operator is called for explicit conversions.

At first I thought this documentation was incorrect because I was experiencing an exception during testing. I re-tested and found something very strange. The function I implemented as the widening conversion operator works fine when an implicit cast is done using the "=" operator.

For example, the following will implicitly cast the Something type into MyClass. It calls my Widening conversion implementation correctly and everything works without error:

Dim y As Something
Dim x As MyClass = y

However if the implicit cast is done in a foreach loop it does not work.

For for example, the following code will throw an exception ("Unable to cast object of type 'Something' to type 'MyClass'") when the Something type is implicitly casted to MyClass in the For Each Loop:

 Dim anArrayOfSomethingTypes() As Something  = getArrayOfSomethings()
 For Each x As MyType In anArrayOfSomethingTypes 
  ....
 Next

If anyone has any insight on this I would greatly appreciate it.

I think I've implemented this incorrectly now that I understand the meaning of the Widening and Narrowing operators. I have implemented it correctly. I shouldn't have doubted myself.

Thanks,

-Frinny

+4  A: 

In VB.Net using the Widening CType operator to create an implicit conversion

Class C1
    Public Shared Widening Operator CType(ByVal p1 As C1) As C2

    End Operator
End Class

The opposite, an explicit conversion, can be done by swapping Narrowing for Widening in the above definition.

JaredPar
Maybe I don't understand the difference between Widening and Narrowing. I've already implemented a Widening Operator and a Narrowing Operator. The Widening Operator accepts a parameter of SomeType and converts it into an instance of MyObject. The narrowing operator takes a parameter of MyObject and converts it to SomeType. I guess my question really is "Is my user-defined CType operator automatically used when Explicit Casting occurs between these two types?"
Frinavale
ERR!! I meant Implicit. My question really is "Is my user-defined CType operator automatically used when IMPLICIT Casting occurs between these two types?" (Big Ooops)
Frinavale
Ok, thanks for your help.I think I skipped over your fist sentence or something. The Widening Operator called for implicit conversions. I found my answer hehttp://stackoverflow.com/questions/1312549/is-there-a-way-to-define-an-implicit-conversion-operator-in-vb-net/1312595#1312595re:
Frinavale
I just tried it, and I got an error: Unable to cast object of type 'OtherNamespace.Something' to type 'MyNamespace.MyClass'.
Frinavale
Apparently, the Widening CType implementation is not automtically called during implicit casting.
Frinavale
I do not see the error when I use the CType method to cast the Something to MyClass (which uses the Widening CType implementation)
Frinavale
Aha! It does work if the implicit cast is done using the "=". For example: Dim x As MyClass = y (where y is an 'OtherNamespace.Something'). However if the implicit cast is done in a foreach loop it does not work. For for example For Each x As MyType in anArrayOfSomethingTypes ....
Frinavale
Please see my updated question. I don't understand why I'm seeing this strange behaviour.
Frinavale
+1  A: 

Look at Widening & Narrowing

http://msdn.microsoft.com/en-ca/library/yf7b9sy7%28VS.80%29.aspx

Quinn Wilson