Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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#?

Posted

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?

Posted

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.

Gamer extraordinaire. Programmer wannabe.
Posted

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 :-)

Posted

What about something like this?

 

Label1.Text = aNumber.ToString

Being smarter than you look is always better than looking smarter than you are.
  • *Gurus*
Posted
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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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.

Gamer extraordinaire. Programmer wannabe.
  • *Experts*
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...