Experienced developers probably won't have much trouble using the new features of the languages, or even converting from one language to another. However, many people use ASP and VBScript daily to build great sites, but have little experience of advanced development features, such as the object- oriented features in .NET. That's actually a testament to how simple ASP is, but now that ASP.NET is moving up a gear, it's important that you make the most of these features.
To that end, this section will give a few samples in Visual Basic .NET and C#, covering a few common areas. This will help should you want to convert existing code, write new code in a language that you aren't an expert in, or perhaps just examine someone else's code. We won't cover the definition of classes and class members again in this section, as they've had a good examination earlier in the chapter.
The first point to look at is that of variable declaration. The following examples show the difference between VB.NEt and C# when declaring variables .
Visual Basic .NET has the same variable declaration syntax as the previous version, but now has the ability to set initial values at variable declaration time. For example:
Dim Name As String = "Rob Smith" Dim Age As Integer = 28 Dim coolDude As New Person("Vince", "Patel")
C# follows the C/C++ style of variable declaration:
string Name = "Rob Smith"; int Age = 28; coolDude = new Person("Vince", "Patel");
Declaring procedures is similar across all languages, as the following examples show.
Procedures and functions follow similar syntax to previous versions:
Private Function GetDiscounts(Company As String) As DataSet Public Sub UpdateDiscounts(Company As String, Discount As Double)
The major difference is that by default all parameters are now passed by value, and not by reference. Moreover, remember that optional parameters also now require a default value:
' incorrect Function GetDiscounts(Optional Comp As String) As DataSet ' correct Function GetDiscounts(Optional Comp As String = "Wrox") As DataSet
Returning values from functions now uses the Return statement, rather than setting the function name to the desired value. For example:
Function IsActive() As Boolean ' some code here Return True End Function
The way you call procedures has also changed. The rule is that arguments to all procedure calls must be enclosed in parentheses. For example:
UpdateDiscounts "Wrox", 5 ' no longer works UpdateDiscounts("Wrox", 5) ' new syntax
C# doesn't have any notion of procedures “ there are only functions that either return or don't return values (in which case the type is void ). For example:
bool IsActive() { // some code here return true; } void UpdateDiscounts(string Company, double Discount) { return; }
Important | To call procedures, C# requires that parentheses be used. |
A few syntactical differences confuse many people when switching languages for the firsttime. The first is that Visual Basic isn't case sensitive, but the other languages - are it still catches me out! Other things are the use of line terminators in C# and JScript, which use a semicolon. Many people switching to these languages complain about them, but the reason they are so great is that it makes the language free form “ the end of the line doesn't end the current statement. This is unlike Visual Basic, where the end of the line is the end of the statement, and a line continuation character is required for long lines.
The syntax of loop constructs differs from Vb.NET to C#.
There are four loop constructs in Visual Basic, and the syntax of one has changed in Visual Basic .NET. The first is the For Next loop:
For counter = start To end [Step step ] Next [ counter ]
For example:
For count = 1 To 10 ... Next
The second is the While loop, for which the syntax has changed “ the new syntax is:
While condition End While
For example:
While count < 10 ... End While
Note | In previous versions of Visual Basic, the loop was terminated with a Wend statement. |
The third is the Do Loop , which has two forms:
Do [(While Until) condition] Loop
Or:
Do Loop [(While Until) condition ]
The difference between these two is the placement of the test condition. In the first instance, the test is executed before any loop content, and therefore the content may not be executed. In the second case, the test is at the end of the loop, so the content is always executed at least once. For example:
Do While count < 10 Loop Do Loop While count < 10
The For Each loop construct is for iterating through collections:
For Each element In collection Next [ element ]
For example:
Dim ctl As Control For Each ctl In Page.Controls ... Next
C# has the same number of loop constructs as Visual Basic. The first is the for loop:
for ([ initializers ] ; [ expression ] ; [ iterators ])
For example:
for (count = 0 ; count < 10 ; count++)
Each of these parts is optional. For example:
for ( ; count < 10; count++) for ( ; ; count++) for (count = 0 ; ; count++) for ( ; ; )
Note | The last of these produces an infinite loop. |
The second is the while loop:
while (expression)
For example:
while (count < 10)
The third is the do...while loop:
do statement while ( expressions );
For example:
do while (count < 10);
The foreach loop construct is for iterating through collections:
foreach ( type identifier in expression )
For example:
foreach (Control ctl in Page.Controls)
You can also use this for looping through arrays:
String[] Authors = new String[] {"Alex", "Brian", "Dave", "Karli", "Rich", "Rob"}; foreach (String Author in Authors) Console.WriteLine("{0}", Author);
One point to note about loops in C# is that the loop affects the code block after the loop. This can be a single line or a bracketed block; for example:
for (count = 0 ; count < 10 ; count++) Console.WriteLine("{0}", count);
If more than one line is required as part of the loop, this can be written as:
for (count = 0 ; count < 10 ; count++) { Console.Write("The value is now: "); Console.WriteLine("{0}", count); }
Type conversion of one data type to another causes a great deal of confusion, especially for those programmers who are used to a typeless language such as VBScript. When dealing with strongly typed languages, you have to let the compiler or runtime convert between types (if it can) or explicitly perform the conversion yourself. The method of conversion depends upon the language.
In Visual Basic .NET there are two ways to do this. The first uses CType :
Dim AgeString As String Dim Age As Integer AgeString = "25" Age = CType(AgeString, Integer)
The CType function takes an object and a data type, and returns the object converted to the data type.
The other way is to use the data type as a cast function:
Age = CInt(AgeString)
In C# just place the type in parentheses before the expression you wish to convert. For example:
Age = (int)AgeString;