visual basic to c#
DESCRIPTION
Visual Basic to C#. Computer Programming II Summer 2011. Purpose. This PowerPoint is for students that received instruction in Computer Programming I using the Visual Basic language. Computer Programming II uses the C# language. This lesson will introduces the differences between VB and C#. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/1.jpg)
COMPUTER PROGRAMMING I ISUMMER 2011
Visual Basic to C#
![Page 2: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/2.jpg)
Purpose
This PowerPoint is for students that received instruction in Computer Programming I using the Visual Basic language.
Computer Programming II uses the C# language. This lesson will introduces the differences between VB and C#.
![Page 3: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/3.jpg)
Essential Standard
This PowerPoint reviews variables, decision making statements, arrays, and methods (sub procedures). Major differences between C# and VB are also covered.
C# is very different from VB in one KEY way. All statements MUST end with a semicolon (;). If you forget this, your code will not work. Exceptions: IF statements and loops
The upside to this is two-fold: 1. Multiple coding statements may be on the same line 2. A long code segment can be spilt up without having to use an
underscore like in VB.
![Page 4: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/4.jpg)
Spacing C# Examples
int intAge; string strName; Two different statements- same line
Long line of code: string query = @"SELECT whatever
FROM tableNameWHERE column = 1";
Both are perfectly valid statements in C#.
![Page 5: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/5.jpg)
Semicolon Exceptions
Two exceptions to the semicolon rule:
if(x==b) no semicolon here It is a “clause” - only part of the statement
{ y=7; semicolon as normal}
while(x< i) no semicolon here either – again it is a “clause”{ intGradeTotal=intGrade; semicolon as normal}
![Page 6: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/6.jpg)
Declaring a Variable
In VB we use the Dim keyword to declare a variable: Dim intAge as Integer
C# handles this differently: int intAge;
In C# use the following format: DataType variableName;
All types are spelled out except integer (int) and character (char).
![Page 7: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/7.jpg)
C# Examples
string strName;
char charGrade;
int intAge;
double dblTaxRate;
![Page 8: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/8.jpg)
Coding Blocks
In VB we use the End keyword to end coding blocks like IF statements or loops.
If a = b Then b=cEnd If
In C#, we use braces so this code block would be written:
if(a==b){ b=c;}
![Page 9: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/9.jpg)
Equality Check (==)
You might of noticed something on the last slide- a == in the if statement.
In VB the assignment operator and equality are the same (=). This is NOT true in C#. The equals sign is solely used for assignment. To check for equality you must use ==.
Using = in an if statement to check for equality is a logic error. The code will run properly, but not produce the intended result. (It will always be false.)
![Page 10: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/10.jpg)
Increment Counters
In VB to add one to a counter we use varName += 1, for two we use +=2 and so on. (i+=1)
This shorthand works in C# as well, but C# provides an additional way to add or subtract one from a counter. i++; i--;
++ adds 1 to the value of i while -- subtracts one. Remember the semicolon is required.
To count by 2, or another number, we use the same shorthand is in VB (i+=2).
![Page 11: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/11.jpg)
IF Statements
In C# we do not use THEN keyword as in VB. The comparison is enclosed in ( ) after the if keyword.
if (a < b){ Statements}
All statements after the if (to be run if the if is true) that would be executed are enclosed in braces ( { } ).
![Page 12: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/12.jpg)
IF..Then..Else Example
if(a > b){ b=c; d= 7;}else{ b=5;d=4;}
![Page 13: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/13.jpg)
Else If
C# handles multiple else if statements a little differently as well. There is a space between else and if unlike in VB. As in VB there can be multiple else ifs and a else (default) if all statements are false.
if (a == b) {c = 12;} else if (a>b) {c=14;} else if (a < b) { c = 16; } else { c = 10; }
![Page 14: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/14.jpg)
If Statements in Assignment Statements
Like VB, C# supports an if statement in an assignment statement. However the format is completely different:
string strName = (a < b) ? "less than 10" : "greater than 10";
? is called the ternary operator.
![Page 15: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/15.jpg)
And/Or & Short Circuiting
In VB we use the And/Or keywords in compound if statements.
C# And = & Or = |
To short circuit the statement use && (and) or || (or).
![Page 16: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/16.jpg)
Examples
else if (number < 15 & number > 5) Using and
else if(number > 50 | number < 25) Usingor
else if (number < 15 && number > 5) Using short circuit and
else if(number > 50 || number < 25) Using short circuit or
![Page 17: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/17.jpg)
Select Case
The Select Case statement in VB do not exist in C#. Instead C# has the switch keyword. This operates similarly to Select Case, but has some key differences. Let’s refresh our memory with an example:
Select case intGradesCase 90 to 100
strGrade= “A”
Case 80 to 89strGrade=“B”
Case 70 to 79strGrade=“C”
Case 60 to 69strGrade=“D”
Case Is < 60strGrade=“F”
ElseMessagebox.show(“Please input a valid number!”)
End Select
![Page 18: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/18.jpg)
Switch
switch(intGrades) { case 10: case 9: strGrade="A"; break; case 8: strGrade="B"; break; case 7: strGrade = "C"; break; case 6: strGrade="D"; break; case 5: case 4: case 3: case 2: case 1: strGrade = "F"; break; }
• Notice instead of using the To keyword we can just assign multiple cases to a given statement.
• The break; as the end of each case is required or the program will continue to read through the statements.
• There could be multiple true cases in C# unlike VB which stops after finding ONE true case.
• Break prevents this which is almost always the desired outcome.
![Page 19: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/19.jpg)
Switch 2
If all cases are false and we want an action to occur we use the default keyword:
switch (caseSwitch) { case 1: Console.WriteLine("Case 1"); break; case 2: Console.WriteLine("Case 2"); break; default: Console.WriteLine("Default case"); break; }
![Page 20: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/20.jpg)
Select Case vs. Switch
The C# Switch statement does not allow ranges or comparisons like the Select Case statement in Visual Basic. It also does not allow Boolean comparison.
This is one place where the VB implementation is superior to the C# one.
![Page 21: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/21.jpg)
Loops
VB and C# each support the following loops: Do While For For each
In C# loops are written in the same syntax as an if statement:while (i< intNum) remember no semicolon on this line{ Code to run in the loop; semicolon at the end of each code statement unless using a if statement}
![Page 22: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/22.jpg)
Loop Examples
while (i <= 50) //pretest loop{ intResult = intResult + i; i++;}
do { intResult = intResult + i; i ++;} while (i <= 50) //posttest loop
![Page 23: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/23.jpg)
For Statements
For statements are written completely differently in C#:
for(i=0; i<=50; i++){ intResult = intResult + i;}
VB:For i as Integer = 0 to 50 intResult = intResult + iNext i
Modifier – increment or decrement
ConditionInitializer
Initializer Condition
Modifier – increment or decrement – Uses Step if other than add 1.
![Page 24: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/24.jpg)
Step Keyword
In VB to count by a value other than 1 we use the Step keyword. This is not supported in C#. To count by a value other than 1 we change the end of the statement.
To count by 2’s upward for example: for(i=0; i<=50; i+=2)
To decrease by 2: for(i=100; i>=50; i-=2)
![Page 25: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/25.jpg)
For each
The for each loop is handled differently in C# as well.
foreach(int j in intArray) //no space { lstGrades.Items.Add(j); }
VB: For Each j In intArray lstGrades.Items.Add(j) Next j
![Page 26: Visual Basic to C#](https://reader036.vdocument.in/reader036/viewer/2022062305/56815436550346895dc23915/html5/thumbnails/26.jpg)
Arrays
In VB we use ( ) in array statements. In C# [ ] are used instead.
Dim intArray = New Integer(4) {1, 2, 3, 4, 5} (VB)
int[] intArray = new int[5] {1,2,3,4,5}; (C#)
In VB we put in 1 less than the number of elements we want in the array. This is NOT the case in C#. If we want five elements we use a 5.
Arrays are still index-based starting at 0.