VB.Net vs C# parte 1

30 Octubre 2007

Una comparación de sintaxis bien basica:

Estructura de una clase en Vb.Net

Namespace gente

Public Class Persona

    Private _nombre As String

    ‘Propiedades

    Public Property nombre()

      Get

        Return _nombre

      End Get

      Set(ByVal value)

       _nombre = value

      End Set

    End Property

 

    ‘Constructor

    Public Sub Persona(ByVal nombre As String)

       Me.nombre = nombre

    End Sub

End Class

End Namespace

Estructura de una clase en C#

namespace gente

{

    public class Persona

    {

        private string _nombre;

        //Propiedad

        public string nombre

        {

            get { return this._nombre;}

            set { this._nombre = value;}

        }

 

        //Constructor de la clase

        public Persona(string nombre)

        {

            this._nombre = nombre;

        }

    }

}

Operadores en VB.Net

Comparacion
=  <  >  <=  >=  <>

Aritmeticos
+  -  *  /  Mod  \(division de enteros)

Logicos
AndAlso  OrElse  And  Or  Xor  Not
AndAlso y OrElse evaluan en circuito corto
Asignacion
=    +=    -=    *=    /=   \=    &=   <<=   >>=

Concatenacion
+    &

Operadores en C#

Comparacion
==  <  >  <=  >=  !=

Aritmeticos
+  -  *  /  %(Mod)

Logicos
&&  ||  &  |  ^  !
&& y || evaluan en circuito corto
Asignacion
=    +=    -=    *=    /=  %=    &=   <<=   >>=   ++  –
Concatenacion
+
Estructuras de control en Vb.Net

‘If comun

If (nombre = “Juan”) Then

    ‘…

Else

    ‘…

End If

 

‘If con varios else

If (nombre = “Juan”) Then

    ‘…

ElseIf (nombre = “Daniel”) Then

    ‘…

Else

    ‘…

End If

 

‘IIF

‘Setea apellido a “Hernandez” si nombre = “Juan”, sino a “Gonzalez”

apellido = IIf(nombre = “Juan”, “Hernandez”, “Gonzalez”)

 

‘Select Case

Select Case nombre

    Case “Juan”

        ‘…

    Case “Daniel”,”Ismael”

        ‘…

    Case Else

        ‘…

    End Select

Estructuras de control en C#

//If comun

if (nombre == “Juan”)

    {/*…*/}

else

    {/*…*/}

 

//If con varios else

if (nombre == “Juan”)

    {/*…*/}

else if (nombre == “Daniel”)

    {/*…*/}

else

    {/*…*/}

 

//?

//Setea apellido a “Hernandez” si nombre = “Juan”, sino a “Gonzalez”

apellido = nombre == “Juan” ? “Hernandez” : “Gonzalez”;

 

//Switch

//La variable a evalar debe ser int o string

//El break; va si o si al finalizar cada case.

switch (nombre)

{

    case “Juan” :

        {/*…*/;break;}

    case “Daniel” :

        {/*…*/;break;}

    default :

        {/*…*/;break;}

}

Estructuras de iteración en VB.net

Dim contador As Integer

 

‘While

While (contador <= 100)

    ‘…

End While

 

‘Do Until … Loop

Do Until (contador = 100)

    ‘…

Loop

 

‘Do While … Loop

Do While (contador <= 100)

    ‘…

Loop

 

‘Do … Loop While

Do

    ‘…

Loop While (contador <= 100)

 

‘Do … Loop Until

Do

    ‘…

Loop Until (contador = 100)

 

‘For … Next

‘Step puede ser negativo para ir de un maximo a un minimo

For contador = 1 To 100 Step 2

    ‘…

Next

 

‘Recorrer una coleccion

Dim listaDeNombres As List(Of String) = New List(Of String)

For Each nombre As String In listaDeNombres

    ‘…

Next

Estructuras de iteración en C#

int contador  = 0;

//While

while (contador < 100)

{

    { /*…*/}

}

 

//For

for (contador = 0; contador <= 100; contador += 1)

{

    { /*…*/}

}

 

//Do … While

do

    {/*…*/}

while (contador == 100);

 

//foreach

List<string> listaDeNombres = new List<string>();

foreach (string nombre in listaDeNombres)

{

    { /*…*/}

}

Leave a Reply