Published: Sunday, January 27, 2002
From VB.NET to C# and Back Again, Part 2
By Darren Neimke and
Scott Mitchell
Read Part 1
In Part 1 we examined some of the primary differences between
the syntax of VB.NET and C#. Additionally, we looked at how to two languages declared variables differently.
In this part we'll look at each language's use of operators, arrays, and functions!
Examining the Operators
VB.NET and C# have roughly the same set of operators. For example, to perform addition between
two numbers, use the + operator in either language. The one major difference is the
string concatenation operator. VB.NET uses the ampersand (&), while C# uses the
+. Both languages also have a short-hand notation for this - &= and
+=, which takes a string on the left hand side and concatenates the string on the right
hand side. That is, if we had:
// C# example...
someString += someOtherString;
' VB.NET example...
someString &= someOtherString
|
at the conclusion, someString would equal whatever value it equaled before we reached the
line shown above, concatenated with the value of someOtherString More clearly, if
someString initially equaled "Scott and someOtherString
equaled "Mitchell", at the conclusion of the above line, someString would
equal "Scott Mitchell".
Arrays in VB.NET and C#
Arrays in VB.NET and C# are created and managed a bit differently. First of all, to index an element in
an array in VB.NET, you use parenthesis, as in the following example:
In C#, however, square brackets are used, like so:
When creating arrays in VB.NET, the syntax used is, essentially, the syntax used to create any variable.
If you wanted to create an array named ages with 0 to n Integer elements, you'd do:
Again, note that this would create a total of n + 1 elements (0 .. n). In C#, the array
declaration is a bit different:
int [] ages = new int[n];
|
Note that we place the [] before the variable name; this indicates that ages
is going to be an array. Next, we have to allocate n ints by saying
= new int[n]. Note that this creates a total of n elements, elements indexed from
0 to n - 1! This is different than in VB.NET, which creates a total of one extra element when
compared to the way C# does it.
Creating and Using Functions and Procedures
In VB.NET there are two classes of functions: those that return values and those that don't. Those functions
that don't return values are called subroutines, and are created a bit differently than functions. In VB.NET,
to create a function, which returns a value, use the following syntax:
Function FunctionName(arg1 as Type, ... argN as Type) as ReturnType
...
Return someVariableOfReturnType
End Function
|
Note that when creating a function you must specify a return type at the end of the first line of the
function declaration. Then, somewhere in the body of the function (usually at the end), you must return
a value of the proper ReturnType. Functions, of course, can take zero to many input parameters,
arg1 through argN, each with their own Type. A subroutine (or procedure, as it's
sometimes called), is defined a bit differently:
Sub ProcedureName(arg1 as Type, ... argN as Type)
...
End Sub
|
Note that the Sub keyword is used instead of the Function keyword, and that
no value need be returned.
In C#, to create either a function or a procedure (a function that doesn't return a value) you must
use the following syntax:
ReturnType FunctionName(Type arg1, ... Type argN)
{
...
return someVariableOfReturnType
}
|
In C# if you don't want the function to return a value, specify the ReturnType as void.
Note that the function's body is delimited by the curly braces ({ ... }) and that, as with the
VB.NET function, we must have a return statement, returning a variable of the proper type (with
functions whose ReturnType is void no return is necessary). Also note
that in the function arguments the Type comes before the arg name, just like when declaring
variables in C#.
Casting
While it may help to think of a variable as nothing but a simple value, a variable can be formally defined
by a number of properties, such as its value, scope, lifetime, and type. The type of a variable determines
the set of legal values that it can be assigned; for example, a variable of type integer (Int32)
can be assigned values from -231..231-1. When you attempt to assign two variables of
unequivalent types, say assigning a string to an integer, a process called casting must occur.
Essentially, casting is the process of converting a variable from one type to another (from a string to an
integer in our previous example). Casting comes in two flavors: implicit and explicit. Implicit casting,
as the name implies, happens automatically; with explicit casting, extra syntax must be used to specify
that a cast should occur. VB.NET allows for a lot of implicit casting, while C# requires casts to be explicit.
For example, if we wanted to have an ArrayList of Integer values, and then wanted to work
with a particular values, we need to use explicit casting in C#:
ArrayList ages = new ArrayList();
ages.Add(23);
int x = ages[0]; // ERROR: ages[0] returns an object - need to cast to int
|
However, the above code (in VB.NET syntax, of course) will work fine. To cast in C#, simply place
a (DesiredType) section in front of the variable you wish to cast. To make the
above code compile, we need to cast s to an int:
ArrayList ages = new ArrayList();
ages.Add(23);
int x = (int) ages[0]; // No error due to explicit cast to int
|
Not all types can be casted to other types. If you attempt to illegally cast a type, you will get an error
message informing you that the cast is illegal. In VB.NET, explicit casting can be used via the
CType function. The syntax is as follows:
VariableOfTypeConvertToType = CType(ObjectToCast, ConvertToType)
|
Now that we've examined the fundamental differences between VB.NET and C#, let's examine an actual exercise
of translating an ASP.NET Web page from C# to VB.NET. We'll do this in Part 3.
Read Part 3!