 |
Visual Basic to C# tricks, #Develop (SharpDevelop)
|
 |
|
It is surprising how close together VB.NET and C#.NET are as
programming languages. Each uses the Microsoft .NET Framework. The classes are
addressed identically with intellisense working on both. The most obvious differences are the way code
blocks are broken, as C# is a curly brace language.
In an earlier Group29.com Forum Post, SharpDevelop (#develop) - .NET GUI IDE FREE!, there are notes about a free tool called SharpDevelop.
SharpDevelop SharpDevelop (#Develop) is a free IDE for C#, VB.NET and Boo projects on Microsoft's .NET platform.
http://www.icsharpcode.net/OpenSource/SD/
Why SharpDevelop, when Visual Studio Express is free?
Check out the Visual Studio Express and SharpDevelop Compared table to see the differences.
Probably the biggest plus is that SharpDevelop has a built-in C# to VB
to C# code conversion tool. There is also source code for said tool
that programmers have adopted for their own use. One example is the Developer Fusion C# to VB conversion web site
There are also reports on the web that you can place this application
on a USB flash drive and run it from any computer that has .NET
installed.
I have used the Developer Fusion conversion site extensively to convert code between C# and VB back and forth. I had a co-worker get into a religious discussion about how C# is superior to VB. I took one of his C# apps and ran it through the conversion tool. Within an hour it was working and ran through the test scripts. Converting from VB to C# is actually a little more difficult, because C# is more rigid in terms of case sensitivity, and the use of string functions. Also C# differentiates between parentheses in functions and brackets in arrays. VB always uses parentheses.
Original Basic string functions
I find that knowing the equivalent to the original Microsoft
Basic string functions has been the biggest barrier for me to mastering C# and
Java. Surprisingly, there are not really equivalents of these in PERL, which is
a language I have much experience. The only VB constant I ever have used is the
vbCrLf that first appeared in the original Visual Basic. It was a shortened
version of Chr(13) + Chr(10).
ASC ( S ) returns B
Returns the ASCII value of the first character in S.
B=Convert.ToInt16(S);
CHR$ ( B ) returns S
Returns a string consisting of a single character whose
ASCII value is B.
S=Convert.ToChar(B).ToString();
S = vbCrLf is the same as
S = Convert.ToChar(13) + Convert.ToChar(10);
LEFT$ ( S1 , I ) returns S2
RIGHT$ ( S1 , I ) returns S2
Returns a string containing, respectively, the leftmost or
rightmost I characters in S1.
S2 = S1.Substring(0, I);
S2 = S1.Substring(S1.Length - I,
I);
MID$ ( S1 , I1 , I2 ) returns S2
Returns substring of S1 that starts at index I1 and has
length I2.
S2 = S1.Substring(Start (0 not
1),Length)
LEN ( S ) returns I
Returns the length of S.
I = S.Length;
STR$ ( N ) returns S
Returns the string representation of N, e.g. STR$(3.14) =
"3.14".
S = N.ToString();
VAL ( S ) returns N
Returns the value of S considered as a number, e.g.
VAL("3.14") = 3.14. If S does not contain a valid number, then VAL
returns 0.
N = Convert.ToIn32(S) (Also Convert.ToDouble(S) ) .
STRING$ ( I , S1 ) returns S2
Returns a string of length I, all of whose characters are
the first character of S1.
No “direct” equivalent of this
statement used for formatting headers or dividing lines with repeating
characters.
S2 = new
System.Text.StringBuilder().Insert(0,"myString",count).ToString()
Date Functions
Some functions become inherently more complex in C#.
Format(Now,”MMddyyyy”) becomes String.Format(“{0:MMddyyyy}”,DateTime.Now)
GetMonthName and similar shortcuts do not exist in C#
public static string GetMonthName(int month, bool abbreviate,
IFormatProvider provider)
{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(provider);
if
(abbreviate) return info.GetAbbreviatedMonthName(month);
return
info.GetMonthName(month);
}
Case for VB.NET
If I were to make a case for Visual Basic .NET as a
desirable programming language, I would concentrate on these bullet points.
- Case Insensitivity and case correction on
variables: When you use a variable name in a code block, the case automatically
corrects to match the case of the variable declaration. That is if you have
declared MyCalculationString, and type “mycalculationsTRING”, the newly typed
variable will correct to “MyCalculationString”.
- Looser requirements on methods and syntax:
tostring and tostring() both work
Case for C#.NET
If I were to make a case for C# as a desirable programming
language, I would concentrate on these bullet points.
- Bracket code blocks in curly braces and
semi-colons make an easy transition from C, C++ and Java.
- Arrays use the square brackets to denote the
element set instead of parentheses
Some differences between c# and vb.net
Feature
Visual Basic .NET
Visual C# .NET
Case sensitive
Not case sensitive:
response.write("Yo") ' OK
Case sensitive:
response.write("Yo"); // Error Response.Write("Yo"); // OK
Functional blocks
Use beginning and ending statements to declare functional
blocks of code:
Sub Show(strX as String)
Response.Write(strX)
End Sub
Use braces to declare functional blocks of code:
void Show (string strX)
{
Response.Write(strX);
}
Type conversion
Implicit type conversions are permitted by default:
Dim intX As Integer
intX = 3.14 ' Permitted
You can limit conversions by including an Option Strict On statement at the beginning of
modules.
Type conversions are performed explicitly by casts:
int intX;
intX = 3.14; // Error!
intX = (int)3.14; //Cast, OK.
Or, use type conversion methods:
string strX;
strX = intX.ToString();
Arrays
Array elements are specified using parentheses:
arrFruit(1) =
"Apple"
Array elements are specified using square brackets:
arrFruit[1] =
"Apple";
Methods
You can omit parentheses after method names if arguments
are omitted:
strX = objX.ToString
You must include parentheses after all methods:
strX = objX.ToString();
Statement termination
Statements are terminated by carriage return:
Response.Write("Hello")
Statements are terminated by the semicolon (;):
Response.Write("Hello");
Statement continuation
Statements are continued using the underscore (_):
intX = System.Math.Pi * _
intRadius
Statements continue until the semicolon (;) and can span multiple lines if needed:
intX = System.Math.PI *
intRadius;
String operator
Use the ampersand (&)
or plus sign (+) to join strings:
strFruit =
"Apples" & _ " Oranges"
Use the plus sign (+) to
join strings:
strFruit =
"Apples" + " Oranges";
Comparison operators
Use =, >, <, >=, <=, <> to compare values:
If intX >= 5 Then
Use ==, >, <, >=, <=, != to compare values:
if (intX >= 5)
Negation
Use the Not keyword to
express logical negation:
If Not IsPostBack Then
Use the ! operator to
express logical negation:
if (!IsPostBack)
Object comparison
Use the Is keyword to
compare object variables:
If objX Is objY Then
Use == to compare object
variables:
if (objX == objY)
Object existence
Use the Nothing keyword
or the IsNothing function to check if an
object exists:
If IsNothing(objX) Then
Use the null keyword to
check if an object exists:
if (objX == null)
|
|
|
|
Posted on Thursday, September 25, 2008 @ 07:55:30 UTC by BB
|
|
|
|
"Visual Basic to C# tricks, #Develop (SharpDevelop)" | Login/Create an Account | 0 comments |
| The comments are owned by the poster. We aren't responsible for their content. |
|
|
|
|
|
No Comments Allowed for Anonymous, please register |
|
|
|
|
|