Mari berbagi

Translate kode VB.NET ke C#

May 9, 2008 · Leave a Comment

Setiap bahasa pada .NET dapat ditranslate dengan mudah antara satu bahasa dengan bahasa lain (Visual Basic ke C# misalnya). Proses translasi dapat menggunakan tools seperti .NET Refactor atau menggunakan layanan gratis online seperti Developer Fusion.

Sebenarnya, setiap bahasa .NET disusun agar dapat mengakses objek yang sama. Dengan demikian, konsekuensinya terjadi penyeragaman struktur bahasa dan tipe data. Dengan mempelajari satu bahasa .NET saja, biasanya bahasa lain akan mudah dipelajari. Langkah awal dalam mempelajari bahasa lain ketika satu bahasa .NET telah dikuasai adalah dengan melihat cheatsheet perbandingan bahasa.

Berikut cheatsheet perbandingan bahasa VB.NET dengan C# (Selengkapnya dapat dilihat pada situs berikut)

Komentar
VB.NET

‘Single line only

Rem Single line only

C#

// Single line

/* Multiple

line */

/// XML comments on single line

/** XML comments on multiple lines */

Struktur kode program
VB.NET

Imports System

Namespace MyNameSpace

Class HelloWorld

‘Entry point which delegates to C-style main

Private Function

Public Overloads Shared Sub Main()

Main(System.Environment.GetCommandLineArgs())

End Sub

Overloads Shared Sub Main(args() As String)

System.Console.WriteLine(“Hello World”)

End Sub ‘Main

End Class ‘HelloWorld End Namespace ‘MyNameSpace

C#

using System

Namespace MyNameSpace

{

class HelloWorld

{

static void Main(string[] args)

{

System.Console.WriteLine(“Hello

World”)

}

}

Tipe Data
VB.NET

‘Value Types

Boolean

Byte

Char (example: “A”)

Short, Integer, Long

Single, Double

Decimal

Date

‘Reference Types

Object

String

Dim x As Integer

System.Console.WriteLine(x.GetType())

System.Console.WriteLine(TypeName(x))

‘Type conversion

Dim d As Single = 3.5

Dim i As Integer = CType (d, Integer)

i = CInt (d)

i = Int(d)

C#

//Value Types

bool

byte, sbyte

char (example: ‘A’)

short, ushort, int, uint, long, ulong

float, double

decimal

DateTime

//Reference Types

object

string

int x;

Console.WriteLine(x.GetType())

Console.WriteLine(typeof(int))

//Type conversion

float d = 3.5;

int i = (int) d

Konstanta
VB.NET

Const MAX_AUTHORS As Integer = 25

ReadOnly MIN_RANK As Single = 5.00

C#

const int MAX_AUTHORS = 25;

readonly float MIN_RANKING = 5.00;

Enumerasi
VB.NET

Enum Action

Start

‘Stop is a reserved word

[Stop]

Rewind

Forward

End Enum

Enum Status

Flunk = 50

Pass = 70

Excel = 90

End Enum

Dim a As Action = Action.Stop

If a <> Action.Start Then _

‘Prints “Stop is 1″

System.Console.WriteLine(a.ToString & ” is ” &

a)

‘Prints 70

System.Console.WriteLine(Status.Pass)

‘Prints Pass

System.Console.WriteLine(Status.Pass.ToString())

Enum Weekdays

Saturday

Sunday

Monday

Tuesday

Wednesday

Thursday

Friday

End Enum ‘Weekdays

C#

enum Action {Start, Stop, Rewind, Forward};

enum Status {Flunk = 50, Pass = 70, Excel =

90};

Action a = Action.Stop;

if (a != Action.Start)

//Prints “Stop is 1″

System.Console.WriteLine(a + ” is ” +

(int) a);

// Prints 70

System.Console.WriteLine((int)

Status.Pass);

// Prints Pass

System.Console.WriteLine(Status.Pass);

enum Weekdays

{

Saturday, Sunday, Monday, Tuesday,

Wednesday, Thursday, Friday

}

Operator
VB.NET

‘Comparison

= < > <= >= <>

‘Arithmetic

+ – * /

Mod

\ (integer division)

^ (raise to a power)

‘Assignment

= += -= *= /= \= ^= <<= >>= &=

‘Bitwise

And AndAlso Or OrElse Not << >>

‘Logical

And AndAlso Or OrElse Not

‘String Concatenation

&

C#

//Comparison

== < > <= >= !=

//Arithmetic

+ – * /

% (mod)

/ (integer division if both operands are

ints)

Math.Pow(x, y)

//Assignment

= += -= *= /= %= &= |= ^= <<=

>>= ++ –

//Bitwise

& | ^ ~ << >>

//Logical

&& || !

//

Categories: Programming
Tagged: , , ,

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

You must be logged in to post a comment.