Published: Sunday, January 27, 2002
From VB.NET to C# and Back Again
By Darren Neimke and
Scott Mitchell
Introduction
In classic ASP, ASP pages could be developed using a number of different scripting languages, such as
VBScript, JScript, PerlScript,
and even Python. In ASP.NET,
developers can use an even wider array of programming languages, from COBOL.NET to Perl.NET,
to even crazier languages, like Objective CAML.NET. ASP.NET Web pages can also be created using
Visual Basic.NET, C# (a Java-like language), and JScript.NET.
With classic ASP, the most popular scripting language, by far, was VBScript. Nearly every code example you'll
find on the Web or in a book on ASP uses VBScript. With ASP.NET, however, a large number of developers
are starting to use C#, Microsoft's newest language offering. As explained
by Anders Hejlsberg (creator of C#), C# is Java-like language that borrows the good parts of Java
and C/C++ and removes the bad parts. In the end, C#'s syntax is a lot like Java's (of which JScript is
derived from, so if you're familiar with JScript, moving to C# will be easy enough).
In any case, as more and more developers begin using C#, more and more ASP.NET examples using C# will
appear on the Web and in books. In my latest book, ASP.NET: Tips, Tutorials,
and Code, all of the code examples are provided in both C# and VB.NET. Many ASP.NET Web sites have
sections that show code exclusively in C#. While those familiar with Java, C/C++, or JScript may find
this trend toward C# code samples a pleasant one, developers who have been using Visual Basic their entire
career may be a bit disconcerted. They may find a perfect article showing C# code to accomplish some task
they need to accomplish, but, if they don't understand C#, the code will look like gobbledygook to them.
The same thing goes for developers who are fluent with Java and may be flabbergasted when examining a
chunk of VB.NET code.
Ultimately, it is the .NET Framework of Classes that provides the functionality to perform tasks, while the
languages themselves - VB.NET, C# etc. - are simply used to connect into it. For example, if you were
connecting to a Microsoft Access database, you'd use the OleDbConnection class in the
System.Data.OleDb namespace of the .NET Framework. (If you are unfamiliar with the .NET
Framework you may wish to read this short summary on the .NET Framework.)
Since the classes and methods used by an ASP.NET Web page, regardless of what language it is written in,
are the same, the only differences between a C# and VB.NET code is its syntax. This article, then, will
examine the syntactical differences between C# and VB.NET, and serve as a means for easily converting
from one language to another.
Preliminary Syntactical Differences Between C# and VB.NET
One of the major differences between VB.NET and C# is that C# is case-sensitive while VB.NET is not. That
is, in VB.NET the variable strName can be defined as strName and used throughout
the ASP.NET Web page as strname, StrName, or any other case combination. In C#,
however, case matters, so strname and StrName are considered two different variables.
Where this really can be confounding is when using classes and methods in the .NET Framework. For example,
notice the following statement written in VB.NET syntax:
response.write("Hello, World!")
|
However, in the .NET Framework the Response object is defined as Response (note
the capital R) and the Write method is capitalized too. Hence, in C# code you'd need to have:
Response.Write("Hello, World!");
|
If you did not capitalize the R and W you would receive an error message when trying to view the ASP.NET
Web page through a browser.
In every programming language, two very important things are the statement delimiters and block delimiters.
Statement delimiters are the delimiters used to separate each statement from one another. Note the
semicolon at the end of the above C# example; in C#, the semicolon is the statement delimiter. In VB.NET, each
statement is delimited by a carraige return, meaning, essentially, each line of code goes on its own
line. Since C#'s statement delimiter is the semicolon (;), at the end of
each statement you must have a semicolon. A block is a set of grouped statements. For example, if you have
an If statement in VB.NET with the following form:
If booleanValue then
Statement1
Statement2
...
StatementN
End If
|
The statements Statement1 through StatmentN are all in a block. Note that the block is
marked by the beginning If statement and the closing End If. In C#, curly
braces are used as block delimiters. That is, in C# our if code would look like:
if (booleanValue)
{
Statement1
Statement2
...
StatementN
}
|
Note that if must be lowercase, and that there is no then keyword. Also, the
booleanValue must be surrounded by parenthesis. These curly braces ({ ... }) are used
whenever a block is needed in C#, such as bodies of functions, while statements,
for statements, etc. Note that VB.NET uses more explicit block delimiters, such as
Function ... End Function, While ... End While, For ... Next, etc.
Some other minor differences: in VB.NET we specify commented lines using the apostrophe (');
in C# we can have single line comments, marked by //, or we can create mutli-line comments
using the delimiters /* and */. That is, everything between those delimiters
is considered a comment.
Declaring Variables
In VB.NET variables are declared via the Dim statement, in the following syntax:
Dim VariableName as Type [ = value]
|
The VariableName is the name of the variable (such as strName, the Type is
the type of the variable (such as Integer or String), and the = value
is an optional addition, which sets the variable to a specified value. For example, if we wanted to create
an Integer named iAge with an initial value of 23, we could do:
In C#, the syntax is just a little different. We omit the Dim statement and place the Type
before the VariableName, like so:
Type VariableName [ = value];
// an example
int iAge = 23;
|
Note that there are some differences in the type names between C# and VB.NET. The following table lists
some of the common data types in VB.NET and their equivalent names in C#. Realize, though, that despite
these minor differences in the spelling of the data types that these data types map back to the same
data types specified in the .NET Framework. That is, a VB.NET Integer and a C# int
are both instances of the System.Int32 .NET Framework structure. In fact, instead of
specifying iAge and Integer or int in the above examples, we could
have said: Int32 iAge = 23; or Dim iAge as Int32 = 23, which would have had the
same effect as declaring it an Integer or int.
| Differences in Data Type Names |
| VB.NET | C# |
Integer | int |
String | string |
Single | float |
There are still many syntactical differences we need to examine between VB.NET and C#, including differences
in operator, array, and function usage, declaration, and allocation. We'll examine all of these pertinent
issues in Part 2.
Read Part 2!