Casting in VB.NET/C#

ivankent

Newcomer
Joined
Jul 15, 2003
Messages
4
Does anyone know if VB.NET has a .NET framework alternative to CType().

In ILDASM for the following VB.NET code
Label1.Text = CType(aNumber, String)

where aNumber was declared as float
is

IL_000e: call string [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType::FromSingle(float32)

In C#
Label1.Text = (string)aNumber;

IL_000e: call instance string [mscorlib]System.Single::ToString()

In VB.net this seems to be going out to VB runtimes?, whereas C# is using System.Single within the framework.

What code in VB.NET would produce the same IL as C#?
 
Hi

Thank you for your comments, however DirectCast only operates on reference types, and not value types as needed. I don't really want to box the variable just to use DirectCast, if I can help it.

Convert.ChangeType actually produces the code below, which adds another operation to that produced by CType().

IL_0015: call object [mscorlib]System.Convert::ChangeType(object,
valuetype [mscorlib]System.TypeCode)
IL_001a: call string [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType::FromObject(object)

So I still can't get VB.NET to produce the same IL as the C# syntax.

Any further thoughts?
 
Each type usually comes with their own conversion methods (check documentation on msdn library or browse through the object browser). In your example above you can easily do aNumber.ToString(). I find that I rarely have to do actual casting unless it's with my own classes.

And btw, a basic type will always be boxed when converted to a string, whether you like it or not.
 
Hi

Thanks for that, I could use anything you mentioned.

What I suppose I'm aiming at is understanding the compiler more fully, what code do I write to get the IL that C# produces, in VB.NET syntax. A cast to string in C# uses System.String, whereas VB.NET CType or Convert has to invoke Microsoft.VisualBasic.CompilerServices.StringType::FromSingle(float32).

I would expect the 2 languages to have syntax that compiles down to the same IL, but for this simple exercise, VB.NET is going outside the framework.

I may be trying to read to much into this, so sorry to keep on about it :-)
 
In C# when you use the cast notation it calls the string's implicit cast operator, which just uses the number's ToString() method. When converting from a numeric to a string I always use this method so equivalent IL should be generated.
 
I would expect the 2 languages to have syntax that compiles down to the same IL, but for this simple exercise, VB.NET is going outside the framework.

They are two different languages made by two different sets of people. Just because they both compile to IL doesn't mean they compile exactly the same.
 
They may not, but they can in theory. Code from either language, as long as it stays within the framework, can compile to be the same if you use the same structure.

There is a code decompiler available (can't remember the URL, but they had an online demo, which I tried one of my DLLs on) which can decompile any component to both C# and VB.NET, and the structure is the same either way. This concept works both ways.
 
Back
Top