Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
more resources

Convert Between VB.NET and C#
A good grasp of the differences between VB.NET and C# is necessary to produce equivalent code in both languages.
by Dave Doknjas

April 4, 2005

Technology Toolbox: VB.NET, C#

VB.NET and C# are so similar in expressive power and features that conversion between the two at first glance appears to be little more than adding or removing semicolons and curly braces. In fact, there are substantial differences between the two languages that you should be aware of. I’ll discuss some of the more significant differences between VB.NET and C# (see Figure 1), and I’ll provide some advice about how you can better ensure that your code has equivalent runtime behavior in both languages. Good conversion software will manage these differences for you, but you’ll find it invaluable to have a good grasp of these issues if you’re developing software in both languages or converting from one to the other.

Setting “Option Strict On” increases the type safety of your VB.NET code, but it might come as a surprise (or a genuine shock to C# programmers) that VB.NET’s enforced type safety is actually stricter than that imposed by C#. For example, this line is not allowed in VB.NET with Option Strict set:

Dim thisInteger As Integer = 12 / 4

However, this equivalent C# line is perfectly acceptable, even at the highest compiler warning level:

int thisInteger = 12 / 4;
ADVERTISEMENT

Adjustments to VB.NET code converted from C# are often necessary in order to raise it to the stricter level of type safety demanded by Option Strict.

In VB.NET, you specify the upper bound of an array in a fixed-size array initialization, while you specify the number of elements in C#. Therefore, you need to adjust all fixed-size array initialization statements regardless of the language you’re converting to.

.NET offers many collections and lists that are fine automatically resizable alternatives to arrays (the most obvious is ArrayList), but there will be times when you might want to do a good old-fashioned ReDim Preserve on an array in C#. Here’s how you can spell out in C# what VB.NET does for you behind the scenes:

'[VB.NET]
ReDim Preserve x(NewUpperBound)

//[C#]
//the following is not pretty, but it 
//does simulate VB's ReDim Preserve:
int[] temp = new int[NewUpperBound + 1];
if (x != null)
        Array.Copy(x, temp, 
                Math.Min(x.Length, temp.Length));
x = temp;

The ending condition of a VB.NET for loop is evaluated only once, while the C# for loop ending condition is evaluated on every iteration. If the ending condition is not a constant expression, then you should assign it to a temporary variable and use that variable in the ending condition to ensure the same behavior as the equivalent VB.NET code.

This article requires registration. Please login below or click here to register.
 
E-mail Address:
Password:
Remember me:
 



Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home