Professional Documents
Culture Documents
Clarionsharpcomparison
Clarionsharpcomparison
NET
This is a quick reference guide to highlight some key syntactical differences between C#, Clarion# and VB.NET (version 2).
NOTE: Clarion.NET and the Clarion# language are currently in alpha test, and various areas of the documentation are incomplete
and in a state of flux. Therefore, it’s very likely that some of the entries will change as new information becomes available.
Program Structure
C# Clarion# VB.NET
using System; PROGRAM Imports System
NAMESPACE(Hello)
namespace Hello { USING(System) Namespace Hello
public class HelloWorld { MAP Class HelloWorld
public static void Main(string[] args) { END Overloads Shared Sub Main(ByVal args() As String)
string name = "C#"; Dim name As String = "VB.NET"
Name &STRING
//See if an argument was passed from the command line 'See if an argument was passed from the command line
if (args.Length == 1) CODE If args.Length = 1 Then name = args(0)
name = args[0]; Name = 'Clarion#'
!See if an argument was passed from the command line Console.WriteLine("Hello, " & name & "!")
Console.WriteLine("Hello, " + name + "!"); IF COMMAND('1') <> '' End Sub
} Name = COMMAND('1') End Class
} END End Namespace
} Console.WriteLine('Hello, '& Name & '!')
Comments
C# Clarion# VB.NET
//Single line !Single line 'Single line only
REM Single line only
/* Multiple !~ Multiple
lines */ lines ~!
///<summary>XML comments on single line</summary> !!!<summary>XML comments on single line</summary> '''<summary>XML comments</summary>
Enumerations
C# Clarion# VB.NET
enum Action {Start, Stop, Rewind, Forward}; Action ENUM Action ENUM !Alternate syntax Enum Action
Start ITEM Start Start
Stop ITEM Stop [Stop] 'Stop is a reserved word
Rewind ITEM Rewind Rewind
Forward ITEM Forward Forward
END END End Enum
Operators
C# Clarion# VB.NET
// Comparison ! Comparison ' Comparison
== < > <= >= != = < > <= >= ~= <> &= = < > <= >= <>
// Note: && and || perform short-circuit logical evaluations ! Note: AND and OR perform short-circuit logical evaluations ' Note: AndAlso and OrElse perform short-circuit logical evaluations
Choices
C# Clarion# VB.NET
greeting = age < 20 ? "What's up?" : "Hello"; Greeting = CHOOSE(Age < 20, 'What''s up?', 'Hello') greeting = IIf(age < 20, "What's up?", "Hello")
if (age < 20) ! One line requires THEN (or ;) ' One line doesn't require End If
greeting = "What's up?"; IF Age < 20 THEN Greeting = 'What''s up?' END If age < 20 Then greeting = "What's up?"
else IF Age < 20; Greeting = 'What''s up?'ELSE Greeting = 'Hello' END If age < 20 Then greeting = "What's up?" Else greeting = "Hello"
greeting = "Hello";
// Semi-colon ";" is used to terminate each statement, ! Use semi-colon (;) to put two commands on same line. ' Use colon (:) to put two commands on same line
// so no line continuation character is necessary. ! A period (.) may replace END in single line constructs, If x <> 100 AndAlso y < 5 Then x *= 5 : y *= 2
! but it is discouraged for multi-line constructs.
IF X <> 100 AND Y < 5 THEN X *= 5; Y *= 2. !Period is OK here
// Multiple statements must be enclosed in {} ! Multi-line is more readable (THEN is optional on multi line) ' Multi-line is more readable
if (x != 100 && y < 5) { IF X <> 100 AND Y < 5 THEN IF X <> 100 AND Y < 5 If x <> 100 AndAlso y < 5 Then
x *= 5; X *= 5 X *= 5 x *= 5
y *= 2; Y *= 2 Y *= 2 y *= 2
} END . !Period is hard to see here End If
! To break up any long single line use | (pipe) ' To break up any long single line use _
IF WhenYouHaveAReally < LongLine | If whenYouHaveAReally < longLine AndAlso _
AND ItNeedsToBeBrokenInto2 > Lines itNeedsToBeBrokenInto2 > Lines Then _
UseThePipe(CharToBreakItUp) UseTheUnderscore(charToBreakItUp)
END
CASE Value
OF 0.00 TO 9.99; RangeName = 'Ones'
OF 10.00 TO 99.99; RangeName = 'Tens'
OF 100.00 TO 999.99; RangeName = 'Hundreds'
!etc.
ELSE ; RangeName = 'Zillions'
END
Loops
C# Clarion# VB.NET
// Pre-test Loops ! Pre-test Loops ' Pre-test Loops
while (c < 10) LOOP WHILE C < 10 LOOP UNTIL C = 10 While c < 10 Do Until c = 10
c++; C += 1 C += 1 c += 1 c += 1
END END End While Loop
// no "until" keyword
LOOP C = 2 TO 10 BY 2 Do While c < 10 For c = 2 To 10 Step 2
for (c = 2; c <= 10; c += 2) Console.WriteLine(C) c += 1 Console.WriteLine(c)
Console.WriteLine(c); END Loop Next
// Breaking out of loops ! Breaking out of loops ' Breaking out of loops
int i = 0; I SHORT(0) Dim i As Integer = 0
while (true) { CODE While (True)
if (i == 5) LOOP If (i = 5) Then Exit While
break; IF I = 5 THEN BREAK. i += 1
i++; I += 1 End While
} END
// Continue to next iteration ! Continue to next iteration ' Continue to next iteration
for (i = 0; i < 5; i++) { LOOP I = 0 TO 4 For i = 0 To 4
if (i < 4) IF I < 4 THEN CYCLE. If i < 4 Then Continue For
continue; Console.WriteLine(I) Console.WriteLine(i) 'Only prints 4
Console.WriteLine(i); //Only prints 4 END Next
}
Arrays
C# Clarion# VB.NET
int[] nums = {1, 2, 3}; Nums SIGNED,DIM(3) Dim nums() As Integer = {1, 2, 3}
for (int i = 0; i < nums.Length; i++) I SIGNED For i As Integer = 0 To nums.Length - 1
Console.WriteLine(nums[i]); CODE Console.WriteLine(nums(i))
Nums[1] = 1; Nums[2] = 2; Nums[3] = 3 Next
LOOP I = 1 TO Nums.Length
Console.WriteLine(Nums[I])
END
// 5 is the size of the array ! 5 is the size of the array ' 4 is the index of the last element, so it holds 5 elements
string[] names = new string[5]; Names &STRING,DIM(5) Dim names(4) As String
names[0] = "David"; CODE names(0) = "David"
names[5] = "Bobby"; //Throws System.IndexOutOfRangeException Names[1] := 'David' names(5) = "Bobby" 'Throws System.IndexOutOfRangeException
Names[6] := 'Bobby' !Caught by compiler
I = 6
Names[I] := 'Bobby' !Throws System.IndexOutOfRangeException
// C# can't dynamically resize arrays, so copy into new array ! Clarion# can't dynamically resize arrays, so copy into new array ' Resize the array, keeping existing values (Preserve is optional)
string[] names2 = new string[7]; Names2 &STRING[] ReDim Preserve names(6)
Array.Copy(names, names2, names.Length); CODE
// or Names2 := NEW STRING[7]
names.CopyTo(names2, 0); Array.Copy(Names, Names2, Names.Length) !or
Names.CopyTo(Names2, 0)
float[,] twoD = new float[rows, cols]; TwoD SREAL[,] Dim twoD(rows-1, cols-1) As Single
twoD[2,0] = 4.5f; CODE twoD(2, 0) = 4.5
TwoD := NEW SREAL[Rows, Cols]
TwoD[3,1] = 4.5
// Jagged arrays ! Jagged arrays unsupported ' Jagged arrays
int[][] jagged = new int[3][] { Dim jagged()() As Integer = { _
new int[5], new int[2], new int[3] }; New Integer(4) {}, New Integer(1) {}, New Integer(2) {} }
jagged[0][4] = 5; jagged(0)(4) = 5
Functions
C# Clarion# VB.NET
// Pass by value(in,default), reference(in/out), and reference(out) ! Pass by value(in,default), reference(in/out), and reference(out) ' Pass by value(in,default), reference(in/out), and reference(out)
void TestFunc(int x, ref int y, out int z) { TestFunc PROCEDURE(SIGNED X, *SIGNED Y, *SIGNED Z) Sub TestFunc(ByVal x As Integer, ByRef y As Integer, _
x++; CODE ByRef z As Integer)
y++; X += 1 x += 1
z = 5; Y += 1 y += 1
} Z = 5 z = 5
RETURN !Optional, if not returning a value End Sub
int a = 1, b = 1, c; //c doesn't need initializing A UNSIGNED(1) Dim a = 1, b = 1, c As Integer 'c set to zero by default
TestFunc(a, ref b, out c); B UNSIGNED(1) TestFunc(a, b, c)
Console.WriteLine("{0} {1} {2}", a, b, c); //1 2 5 C UNSIGNED !C doesn’t need initializing Console.WriteLine("{0} {1} {2}", a, b, c) '1 2 5
CODE
TestFunc(A, B, C)
Console.WriteLine('{{0} {{1} {{2}', A, B, C) !1 2 5
// Accept variable number of arguments ! Accept variable number of arguments ' Accept variable number of arguments
int Sum(params int[] nums) { Sum PROCEDURE(PARAMS UNSIGNED[] Nums),UNSIGNED Function Sum(ByVal ParamArray nums As Integer()) As Integer
int sum = 0; Result UNSIGNED(0) Sum = 0
foreach (int i in nums) I UNSIGNED For Each i As Integer In nums
sum += i; CODE Sum += i
return sum; FOREACH I IN Nums Next
} Result += I End Function 'Or use Return statement like C#
END
int total = Sum(4, 3, 2, 1); //returns 10 RETURN Result Dim total As Integer = Sum(4, 3, 2, 1) 'returns 10
/* C# doesn't support optional arguments/parameters. ! Optional parameters without default value ' Optional parameters must be listed last and have a default value
Just create two different versions of the same function. */ ! (When omitted, value parameters default to 0 or an empty string) Sub SayHello(ByVal name As String, Optional ByVal prefix As String = "")
void SayHello(string name, string prefix) { ! (Use OMITTED to detect the omission) Console.WriteLine("Greetings, " & prefix & " " & name)
Console.WriteLine("Greetings, " + prefix + " " + name); SayHello PROCEDURE(STRING Name, <STRING Prefix>) End Sub
}
void SayHello(string name) { ! Optional parameters with default value SayHello("Strangelove", "Dr.")
SayHello(name, ""); SayHello("Madonna")
! (Valid only on simple numeric types)
}
! (OMITTED will not detect the omission — the default is passed)
SayHello PROCEDURE(STRING Name, BYTE Age = 20)
Strings
C# Clarion# VB.NET
// Escape sequences ! Special Characters ' Special Character Constants
\r //carriage-return <13> !carriage-return vbCrLf, vbCr, vbLf, vbNewLine
\n //line-feed <10> !line-feed vbNullString
\t //tab <9> !tab vbTab
\\ //backslash vbBack
<n> !character with the ASCII value=n (see above)
\" //quote vbFormFeed
<< !less-than vbVerticalTab
{{ !left-curly-brace ""
'' !single-quote
{n} !Repeat previous character "n" times
// My birthday: Sep 3, 1964 ! My birthday: Sep 3, 1964 ' My birthday: Sep 3, 1964
DateTime dt = new DateTime(1964, 9, 3); DT DateTime Dim dt As New DateTime(1964, 9, 3)
string s = "My birthday: " + dt.ToString("MMM dd, yyyy"); S &STRING Dim s As String = "My birthday: " & dt.ToString("MMM dd, yyyy")
CODE
DT = NEW DateTime(1964, 9, 3)
S = 'My birthday: '& DT.ToString('MMM dd, yyyy')
Exception Handling
C# Clarion# VB.NET
// Throw an exception ! Throw an exception ' Throw an exception
Exception ex = new Exception("Something is really wrong."); Ex &Exception('Something is really wrong.') Dim ex As New Exception("Something is really wrong.")
throw ex; CODE Throw ex
THROW Ex
// Catch an exception ! Catch an exception ' Catch an exception
try { TRY Try
y = 0; y = 0; y = 0
x = 10 / y; x = 10 / y; x = 10 / y
} CATCH(Exception Ex) !Argument is optional, no "When" keyword Catch ex As Exception When y = 0 'Argument and When is optional
catch (Exception ex) { //Argument is optional, no "When" keyword Console.WriteLine(Ex.Message); Console.WriteLine(ex.Message)
Console.WriteLine(ex.Message); FINALLY Finally
} BEEP Beep()
finally { END End Try
//Requires reference to the Microsoft.VisualBasic.dll
//assembly (pre .NET Framework v2.0) ' Deprecated unstructured error handling
Microsoft.VisualBasic.Interaction.Beep(); On Error GoTo MyErrorHandler
} ...
MyErrorHandler: Console.WriteLine(Err.Description)
Namespaces
C# Clarion# VB.NET
namespace Harding.Compsci.Graphics { NAMESPACE(Harding.Compsci.Graphics) Namespace Harding.Compsci.Graphics
... ...
} End Namespace
// or ' or
Classes / Interfaces
C# Clarion# VB.NET
// Accessibility keywords ! Accessibility keywords ' Accessibility keywords
public PUBLIC Public
private PRIVATE Private
internal INTERNAL Friend
protected PROTECTED Protected
protected internal PROTECTED INTERNAL Protected Friend
static STATIC Shared
Constructors / Destructors
C# Clarion# VB.NET
class SuperHero { SuperHero CLASS Class SuperHero
private int _powerLevel; _PowerLevel UNSIGNED,PRIVATE Private _powerLevel As Integer
Construct PROCEDURE,PUBLIC
public SuperHero() { Construct PROCEDURE(UNSIGNED PowerLevel),PUBLIC Public Sub New()
_powerLevel = 0; Destruct PROCEDURE _powerLevel = 0
} END End Sub
public SuperHero(int powerLevel) { SuperHero.Construct PROCEDURE Public Sub New(ByVal powerLevel As Integer)
this._powerLevel= powerLevel; CODE Me._powerLevel = powerLevel
} SELF._PowerLevel = 0 End Sub
Using Objects
C# Clarion# VB.NET
SuperHero hero = new SuperHero(); Hero &SuperHero Dim hero As SuperHero = New SuperHero
Hero2 &SuperHero 'or
Obj &Object Dim hero As New SuperHero
CODE
Hero &= NEW SuperHero
SuperHero hero2 = hero; //Both reference the same object Hero2 &= Hero !Both reference the same object Dim hero2 As SuperHero = hero 'Both reference the same object
hero2.Name = "WormWoman"; Hero2.Name = 'WormWoman' hero2.Name = "WormWoman"
Console.WriteLine(hero.Name); //Prints WormWoman Console.WriteLine(Hero.Name) !Prints "WormWoman" Console.WriteLine(hero.Name) 'Prints WormWoman
hero = null ; //Free the object Hero &= NULL !Free the object hero = Nothing 'Free the object
Object obj = new SuperHero(); Obj &= NEW SuperHero(); Dim obj As Object = New SuperHero
if (obj is SuperHero) IF Obj IS SuperHero If TypeOf obj Is SuperHero Then _
Console.WriteLine("Is a SuperHero object."); Console.WriteLine('Is a SuperHero object.') Console.WriteLine("Is a SuperHero object.")
END
// Mark object for quick disposal ! No equivalent to USING(Resource) to mark for quick disposal ' Mark object for quick disposal
using (StreamReader reader = File.OpenText("test.txt")) { Using reader As StreamReader = File.OpenText("test.txt")
string line; Dim line As String = reader.ReadLine()
while ((line = reader.ReadLine()) != null) While Not line Is Nothing
Console.WriteLine(line); Console.WriteLine(line)
} line = reader.ReadLine()
End While
End Using
Structs
C# Clarion# VB.NET
struct StudentRecord { StudentRecord STRUCT Structure StudentRecord
public string name; Name &STRING,PUBLIC Public name As String
public float gpa; GPA SREAL,PUBLIC Public gpa As Single
Construct PROCEDURE(STRING Name,SREAL GPA),PUBLIC
public StudentRecord(string name, float gpa) { INLINE Public Sub New(ByVal name As String, ByVal gpa As Single)
this.name = name; CODE Me.name = name
this.gpa = gpa; SELF.Name = Name Me.gpa = gpa
} SELF.GPA = GPA End Sub
} END End Structure
END
StudentRecord stu = new StudentRecord("Bob", 3.5f); Stu StudentRecord('Bob', 3.5) Dim stu As StudentRecord = New StudentRecord("Bob", 3.5)
StudentRecord stu2 = stu; Stu2 StudentRecord Dim stu2 As StudentRecord = stu
CODE
stu2.name = "Sue"; Stu2.Name = 'Sue' stu2.name = "Sue"
Console.WriteLine(stu.name); //Prints Bob Console.WriteLine(Stu.Name) !Prints Bob Console.WriteLine(stu.name) 'Prints Bob
Console.WriteLine(stu2.name); //Prints Sue Console.WriteLine(Stu2.Name) !Prints Sue Console.WriteLine(stu2.name) 'Prints Sue
Properties
C# Clarion# VB.NET
private int _size; _Size UNSIGNED,PRIVATE Private _size As Integer
Size PROPERTY,UNSIGNED,PUBLIC
public int Size { INLINE Public Property Size() As Integer
get { GETTER Get
return _size; CODE Return _size
} RETURN SELF._Size End Get
set { //parameter is always "value" SETTER !parameter is always "Value" Set (ByVal Value As Integer)
if (value < 0) CODE If Value < 0 Then
_size = 0; IF Value < 0 _size = 0
else SELF._Size = 0 Else
_size = value; ELSE _size = Value
} SELF._Size = Value End If
} END End Set
END End Property
Delegates / Events
C# Clarion# VB.NET
MAP
delegate void MsgArrivedEventHandler(string message); MsgArrivedEventHandler PROCEDURE(STRING Message),DELEGATE Delegate Sub MsgArrivedEventHandler(ByVal message As String)
END
// Events must use explicitly-defined delegates in C# ! Events must use explicitly-defined delegates in Clarion# ' or to define an event which declares a delegate implicitly
Event MsgArrivedEvent(ByVal message As String)
private void MyButton_Click(object sender, EventArgs e) { MyForm.MyButton_Click PROCEDURE(Object Sender, EventArgs E) Private Sub MyButton_Click(ByVal sender As Object, _
MessageBox.Show("Button was clicked", "Info", CODE ByVal e As EventArgs) Handles MyButton.Click
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show('Button was clicked', 'Info', | MessageBox.Show(Me, "Button was clicked", "Info", _
} MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBoxButtons.OK, MessageBoxIcon.Information)
} End Sub
End Class
Console I/O
C# Clarion# VB.NET
Name &STRING
Age UNSIGNED
C UNSIGNED
CODE
Console.Write("What's your name? "); Console.Write('What's your name? ') Console.Write("What's your name? ")
string name = Console.ReadLine(); Name = Console.ReadLine() Dim name As String = Console.ReadLine()
Console.Write("How old are you? "); Console.Write('How old are you? ') Console.Write("How old are you? ")
int age = Convert.ToInt32(Console.ReadLine()); Age = Convert.ToInt32(Console.ReadLine()) Dim age As Integer = Val(Console.ReadLine())
Console.WriteLine("{0} is {1} years old.", name, age); Console.WriteLine("{{0} is {{1} years old.", Name, Age); Console.WriteLine("{0} is {1} years old.", name, age)
// or ! or ' or
Console.WriteLine(name + " is " + age + " years old."); Console.WriteLine(Name + 'is '+ age + 'years old.'); Console.WriteLine(name & " is " & age & " years old.")
Dim c As Integer
int c = Console.Read(); //Read single char C = Console.Read() !Read single char c = Console.Read() 'Read single char
Console.WriteLine(c); //Prints 65 if user enters "A" Console.WriteLine(C) !Prints 65 if user enters "A" 0 Console.WriteLine(c) 'Prints 65 if user enters "A"
File I/O
C# Clarion# VB.NET
using System.IO; USING(System.IO) Imports System.IO
// Write out to text file ! Write out to text file ' Write out to text file
StreamWriter writer = File.CreateText("c:\\myfile.txt"); Writer &StreamWriter Dim writer As StreamWriter = File.CreateText("c:\myfile.txt")
writer.WriteLine("Out to file."); CODE writer.WriteLine("Out to file.")
writer.Close(); Writer &= File.CreateText('c:\myfile.txt') writer.Close()
Writer.WriteLine('Out to file.')
Writer.Close()
// Read all lines from text file ! Read all lines from text file ' Read all lines from text file
StreamReader reader = File.OpenText("c:\\myfile.txt"); Reader &StreamReader Dim reader As StreamReader = File.OpenText("c:\myfile.txt")
string line = reader.ReadLine(); Line &STRING Dim line As String = reader.ReadLine()
while (line != null) { CODE While Not line Is Nothing
Console.WriteLine(line); Reader &= File.OpenText('c:\myfile.txt') Console.WriteLine(line)
line = reader.ReadLine(); Line = Reader.ReadLine() line = reader.ReadLine()
} LOOP WHILE NOT Line &= NULL End While
reader.Close(); Console.WriteLine(Line) reader.Close()
Line = Reader.ReadLine()
END
Reader.Close()
// Write out to binary file ! Write out to binary file ' Write out to binary file
string str = "Text data"; Str &STRING Dim str As String = "Text data"
int num = 123; Num SIGNED(123) Dim num As Integer = 123
BinaryWriter binWriter = BinWriter &BinaryWriter Dim binWriter As New BinaryWriter(File.OpenWrite("c:\myfile.dat"))
new BinaryWriter(File.OpenWrite("c:\\myfile.dat")); CODE binWriter.Write(str)
binWriter.Write(str); Str = 'Text data' binWriter.Write(num)
binWriter.Write(num); BinWriter &= NEW BinaryWriter(File.OpenWrite('c:\myfile.dat')) binWriter.Close()
binWriter.Close(); BinWriter.Write(Str)
BinWriter.Write(Num)
BinWriter.Close()
// Read from binary file ! Read from binary file ' Read from binary file
BinaryReader binReader = BinReader &BinaryReader Dim binReader As New BinaryReader(File.OpenRead("c:\myfile.dat"))
new BinaryReader(File.OpenRead("c:\\myfile.dat")); CODE str = binReader.ReadString()
str = binReader.ReadString(); BinReader &= NEW BinaryReader(File.OpenRead('c:\myfile.dat')) num = binReader.ReadInt32()
num = binReader.ReadInt32(); Str = BinReader.ReadString() binReader.Close()
binReader.Close(); Num = BinReader.ReadInt32()
BinReader.Close()
Based upon adocument by Frank McCown (www.harding.edu/fmccown/vbnet_csharp_comparison.html). Licensed under a Creative Commons License (creativecommons.org/licenses/by-sa/2.0).
Clarion# examples provided by Mike Hanson (www.boxsoft.net). Last updated December 17, 2007.