One for the guru's... C# to VB probs... really need help!

bri189a

Well-known member
Joined
Sep 11, 2003
Messages
1,004
Location
VA
So I need to have a custom type that supported all the standard binary and unary operators.

So VB was immediately out of the picture...made my custom type in C# and did all the operator overloading and did some explicit and implicit conversion functions too.

Everything works great in C# environment...an example of code use:

[CS]
ComplexType c = 4;
c++;
c-= 12;
int i = c;
c += i;
System.Int64 i64 = (System.Int64) c;
c = (ComplexType)--i64;
[/code]

All those lines above work fine in C#...notice the implict casting for Integer and the explicit for Int64.

Now I compile that and open a VB project and import the reference to the DLL.

Now in VB I do:
Code:
  Dim c as ComplexType = 15
  c += 5
  c = c + 5
  c = CType(15, ComplexType)
Heres the error on every line:
Cannot convert from Integer to ComplexType... this shouldnt happen because of my implicit casting. Obviously since this is a value type I cant do DirectCast. I also went back and changed my implicit cast functions to explicit, not thinking it would make a differance...but had to try... and it didnt do anything for me of coarse.

Someone told me to try turning Option Strict off; that didnt change anything; I even tried turn Option Explcit off... same error.

Since I wrote the custom type in C# and compiled it, it shouldnt matter what language it was written in from there right? Its all .NET.

Maybe there is a compile switch or something I dont know about or some sort of attribute, or assembly thing.

Can someone point me in the right direction and tell me what Im doing wrong...and again...everything works perfectly in C#.
 
This isnt my specialty at all... but I think you might want to try CType() here. (I know, bummer...) For Coercion between Enums and Integer Types I dont think there is any way around CType(). I have a feeling that Complex Types are likely in the same boat, but Im not a C# guy, and dont have experience with Complex Types, and so I really cant say.

So I would try CType() next, although, to be honest, Im a bit surprised that Option Strict Off did not do the trick...
 
IIRC operator overloading is not entirely built into the .Net framework but is rather a feature of the language compiler, when C# code that uses the various operators is compiled then certain functions are emitted into the MSIL e.g.

when the following is compiled
C#:
public class Class1
{
public static explicit operator Class1 (int i)
{
return new Class1();
}

public static implicit operator Class1 (string s)
{
return new Class1();
}
}

the following 2 method signatures are generated
Code:
.method public hidebysig specialname static class WindowsApplication2.Class1 op_Explicit(int32 i) cil managed

.method public hidebysig specialname static class WindowsApplication2.Class1 op_Implicit(string s) cil managed
similar methods are generated for other operators.

C# will use these methods when your code calls the operator, as things stand at the moment the VB compiler doesnt support these methods and therefore cannot use the overloaded operators (the next version of VB will gain this functionality though).

Also out of general curiosity is there a reason why you are requiring an explicit cast to an int64 but not an int32 - or was this just as a general example?
 
Last edited by a moderator:
It was a general example PD, thanks for the insight on this one, I can sleep tonight. :)
 
Back
Top