C#       

Volver links de C#

Sintaxis de instrucciones de C# y su equivalente en VB.

Sintaxis C# Sintaxis VB
Declaración de varaibles
int x;
String s;
String s1, s2;
Object o;
Object obj= new Object();
public String name;
Dim x As Integer
Dim s As String
Dim s1, s2 As String
Dim o 'Implicity Object()
Public name As String
Instrucciones
Response.Write("foo");
Response.Write("foo")
Comentarios
//esto es un comentario
/*
esto es un bloque
de comentarios
*/
' esto es un comentario

' esto es un
' bloque de 
' comentarios
Acceder a métodos
String s = Request.QueryString["Name"];
String value = Request.Cookies["key"];
Dim s, value As String
s = Request.QueryString("Name")
value = Request.Cookies("Key").Value 

'Note that default non-indexed properties 
'must be explicitly named in VB
Declaración simple de métodos
public String name {

 get { 
 ... 
 return ...;
 }

 set {
 ... = value;
 }

}
Public Property Name As String

 Get 
 ... 
 Return ...;
 End Get

 Set
 ... = Value;
 End Set

End Property
Tablas/Matrices
String[] a = new String[3];
 a[0] = "1";
 a[1] = "2";
 a[2] = "3";

 String[][] a = new String[3][3];
 a[0][0] = "1";
 a[1][0] = "2";
 a[2][0] = "3";
 
 
 
 
 
 
 
 
 
 
 
 
Dim a(3) As String
 a(0) = "1"
 a(1) = "2"
 a(2) = "3"

 Dim a(3,3) As String
 a(0,0) = "1"
 a(1,0) = "2"
 a(2,0) = "3"

 ' Tableaux dynamiques (NA in C#)

 Dim a() As String
 a(0,0) = "1"
 a(1,0) = "2"
 a(2,0) = "3"

 Dim a(,) As String
 a(0,0) = "1"
 a(1,0) = "2"
 a(2,0) = "3"
Inicializaciones de variables
String s = "Hello World";
int i = 1
double[] a = { 3.00, 4.00, 5.00 };
Dim s As String = "Hello World"
Dim i As Integer = 1
Dim a() As Double = { 3.00, 4.00, 5.00 }
Instrucciones condicionales
if (Request.QueryString != null) {
 ...
}
If Not (Request.QueryString = Null)
 ...
End If
Instrucciones condicionales múltiples
switch (FirstName) {
 case "John" :
 ...
 break;
 case "Paul" :
 ...
 break;
 case "Ringo" :
 ...
 break;
}
Select (FirstName)
 case "John" :
 ...
 case "Paul" :
 ...
 case "Ringo" :
 ...
End Select
Bucles FOR
for (int i=0; i<3; i++)
 a(i) = "test";
 
 
Dim I As Integer
 For I = 0 To 2 
 a(I) = "test"
 Next
Bucles WHILE
int i = 0;
while (i<3) {
 Console.WriteLine(i.ToString());
 i += 1;
}
Dim I As Integer
I = 0
Do While I < 3
 Console.WriteLine(I.ToString())
 I = I + 1
Loop
Concatenación de strings
String s1;
String s2 = "hello";
s2 += " world";
s1 = s2 + " !!!";
Dim s1, s2 As String
s2 = "hello"
s2 &= " world"
s1 = s2 & " !!!"
Eventos
void MyButton_Click(Object sender, EventArgs E) {
...
}
Sub MyButton_Click(Sender As Object, E As EventArgs)
...
End Sub

' notez que ByVal est désormait 
' par defaut pour les paramètres en VB
Objetos, declaración
MyObject obj = (MyObject)Session["Some Value"];
IMyObject iObj = obj
Dim bj As MyObject
Dim iObj As IMyObject
obj = Session("Some Value")
iObj = CType(obj, IMyObject)
Conversiones explicitas
int i = 3;
String s = i.ToString();
double d = Double.Parse(s);
Dim i As Integer
Dim s As String
Dim d As Double

i = 3
s = i.ToString()
d = CDbl(s) 

' Voir aussi CDbl(...), CStr(...), ...
Definición de clases: Heréncia
using System; 

namespace MySpace {

 public class Foo : Bar {

 int x;

 public Foo() { x = 4; }
 public void Add(int x) { this.x += x; } 
 public int GetNum() { return x; } 
 }

}

// csc /out:librarycs.dll /t:library library.cs
Imports System

Namespace MySpace

 Public Class Foo : Inherits Bar

 Dim x As Integer

 Public Sub New()
 MyBase.New()
 x = 4
 End Sub

 Public Sub Add(x As Integer)
 Me.x = Me.x + x
 End Sub

 Public Function GetNum() As Integer
 Return x
 End Function

 End Class

End Namespace

' vbc /out:libraryvb.dll /t:library library.vb
Definición de clases: Método MAIN
using System;

public class ConsoleCS {

 public ConsoleCS() {
 Console.WriteLine("Object Created");
 }

 public static void Main (String[] args) {
 Console.WriteLine("Hello World");
 ConsoleCS ccs = new ConsoleCS();
 }

}

// csc /out:consolecs.exe /t:exe console.cs
Imports System

Public Class ConsoleVB

 Public Sub New()
 MyBase.New()
 Console.WriteLine("Object Created")
 End Sub

 Public Shared Sub Main()
 Console.WriteLine("Hello World")
 Dim cvb As ConsoleVB
 cvb = New ConsoleVB()
 End Sub

End Class

' vbc /out:consolevb.exe /t:exe console.vb
Módulos Estándard
using System;

public class Module {

public static void Main (String[] args) {
 Console.WriteLine("Hello World");
}

}
// csc /out:consolecs.exe /t:exe console.cs
Imports System

Public Module ConsoleVB

 Public Sub Main()
 Console.WriteLine("Hello World")
 End Sub

End Module

' vbc /out:consolevb.exe /t:exe console.vb

 

Hosted by www.Geocities.ws

1