Actualizado: 14/Jul/2004
Autor: Guillermo 'guille' Som
Volver al índice de Generar clases para acceder a una tabla o al de la librería
El código de la librería: CrearClase, CrearClaseOleDb, CrearClaseSQL, ConvLang
El código de Visual Basic .NET
'------------------------------------------------------------------------------ ' Clase para crear trozos de código en VB y C# (07/Jul/04) ' los métodos también generan el código de VB ' Inicialmente se usará código de VB para crear el de C# ' al menos en lo que a los tipos de datos, tipos de métodos, etc. ' ' ' ©Guillermo 'guille' Som, 2004 '------------------------------------------------------------------------------ Option Strict On Option Explicit On ' Imports System Imports Microsoft.VisualBasic Namespace elGuille.Data.CodeGenerator ' Public Enum eLenguaje eVBNET eCS End Enum Public Class ConvLang Public Shared Lang As eLenguaje ' las comprobaciones se harán sin tener en cuenta mayúsculas/minúsculas Private Shared tiposVB() As String = {"short", "integer", "long", "string", "object", "double", "single", "date", "decimal", "boolean", "byte", "char"} Private Shared tiposCS() As String = {"short", "int", "long", "string", "object", "double", "float", "DateTime", "decimal", "bool", "byte", "char"} Private Shared modificadoresVB() As String = {"public", "private", "friend", "protected", "shared", "overrides", "overridable", "shadows"} Private Shared modificadoresCS() As String = {"public", "private", "internal", "protected", "static", "override", "virtual", "new"} Private Shared compVB() As String = {"=", "<>"} Private Shared compCS() As String = {"==", "!="} Private Shared instrVB() As String = {".Rows(0)", "New ", ", Me", " Me.", " Me(", " Me,", "(Me,", "True", "False", " Nothing", " & ", Chr(34) & " " & Chr(34) & "c"} Private Shared instrCS() As String = {".Rows[0]", "new ", ", this", " this.", " this(", " this,", "(this,", "true", "false", " null", " + ", "' '"} ' Private Shared Function modificador(ByVal modif As String) As String ' el parámetro puede incluir más de uno (separado por espacio) ' por ejemplo: Public Shared If Lang = eLenguaje.eVBNET Then Return modif ' Dim s1 As String = "" Dim i As Integer Dim a() As String = modif.Split(" "c) ' For Each s As String In a If s <> "" Then i = Array.IndexOf(modificadoresVB, s.ToLower()) If i > -1 Then s1 &= " " & modificadoresCS(i) Else s1 &= " " & s End If End If Next Return s1.TrimStart() End Function ' Public Shared Function Comentario() As String If Lang = eLenguaje.eCS Then Return ""//" ElseIf Lang = eLenguaje.eVBNET Then Return "'" End If End Function ' esta sobrecarga la uso para indicar si se pone o no el comentario ' (para cuando las opciones son opcionales) Public Shared Function Comentario(ByVal poner As Boolean) As String If Lang = eLenguaje.eCS Then If poner Then Return ""//" Else Return "" End If ElseIf Lang = eLenguaje.eVBNET Then If poner Then Return "'" Else Return "" End If End If End Function Public Shared Function Comentario(ByVal coment As String) As String If Lang = eLenguaje.eCS Then Return ""//" & coment ElseIf Lang = eLenguaje.eVBNET Then Return "'" & coment End If End Function Public Shared Function [Imports](ByVal espacio As String) As String If Lang = eLenguaje.eCS Then Return "using " & espacio & ";" ElseIf Lang = eLenguaje.eVBNET Then Return "Imports " & espacio End If End Function Public Shared Function [Class](ByVal modif As String, ByVal nombre As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} class {1}{{", modificador(modif), nombre) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} Class {1}", modif, nombre) End If End Function Public Shared Function EndClass() As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "End Class" End If End Function ' Public Shared Function Constructor(ByVal nombreClase As String) As String Return Constructor("", nombreClase) End Function Public Shared Function Constructor(ByVal modif As String, ByVal nombreClase As String) As String If Lang = eLenguaje.eCS Then If modif = "" Then modif = "Friend" Return String.Format("{0} {1}(){{", modificador(modif), nombreClase) ElseIf Lang = eLenguaje.eVBNET Then If modif = "" Then Return "Friend Sub New()" Else Return String.Format("{0} Sub New()", modif) End If End If End Function Public Shared Function Constructor(ByVal modif As String, ByVal nombreClase As String, ByVal var As String, ByVal elTipo As String) As String If Lang = eLenguaje.eCS Then If modif = "" Then modif = "Friend" Return String.Format("{0} {1}({2}){{", modificador(modif), nombreClase, Variable(var, elTipo)) ElseIf Lang = eLenguaje.eVBNET Then If modif = "" Then Return "Friend Sub New()" Else Return String.Format("{0} Sub New({1})", modif, Variable(var, elTipo)) End If End If End Function ' Public Shared Function [Try]() As String If Lang = eLenguaje.eCS Then Return "try{" ElseIf Lang = eLenguaje.eVBNET Then Return "Try" End If End Function Public Shared Function EndTry() As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "End Try" End If End Function Public Shared Function [Catch]() As String If Lang = eLenguaje.eCS Then Return "}catch{" ElseIf Lang = eLenguaje.eVBNET Then Return "Catch" End If End Function Public Shared Function [Catch](ByVal var As String, ByVal elTipo As String) As String If Lang = eLenguaje.eCS Then Return String.Format("}}catch({0}){{", Variable(var, elTipo)) ElseIf Lang = eLenguaje.eVBNET Then Return "Catch " & Variable(var, elTipo) End If End Function Public Shared Function [Finally]() As String If Lang = eLenguaje.eCS Then Return "finally{" ElseIf Lang = eLenguaje.eVBNET Then Return "Finally" End If End Function ' Public Shared Function [Get]() As String If Lang = eLenguaje.eCS Then Return "get{" ElseIf Lang = eLenguaje.eVBNET Then Return "Get" End If End Function Public Shared Function EndGet() As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "End Get" End If End Function Public Shared Function [Set](ByVal elTipo As String) As String If Lang = eLenguaje.eCS Then Return "set{" ElseIf Lang = eLenguaje.eVBNET Then Return "Set(value As " & elTipo & ")" End If End Function Public Shared Function EndSet() As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "End Set" End If End Function ' Public Shared Function [Property](ByVal modif As String, ByVal elTipo As String, ByVal nombre As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} {1} {2}{{", modificador(modif), Tipo(elTipo), nombre) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} Property {1}() As {2}", modif, nombre, elTipo) End If End Function Public Shared Function [Property](ByVal modif As String, ByVal elTipo As String, ByVal index As String, ByVal tipoIndex As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} {1} this[{2} {3}]{{", modificador(modif), Tipo(elTipo), Tipo(tipoIndex), index) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} Default Property Item({1} As {2}) As {3}", modif, index, tipoIndex, elTipo) End If End Function Public Shared Function PropertyRead(ByVal modif As String, ByVal elTipo As String, ByVal nombre As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} {1} {2}{{", modificador(modif), Tipo(elTipo), nombre) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} ReadOnly Property {1}() As {2}", modif, nombre, elTipo) End If End Function Public Shared Function PropertyRead(ByVal modif As String, ByVal elTipo As String, ByVal index As String, ByVal tipoIndex As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} {1} this[{2} {3}]{{", modificador(modif), Tipo(elTipo), Tipo(tipoIndex), index) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} Default ReadOnly Property Item({1} As {2}) As {3}", modif, index, tipoIndex, elTipo) End If End Function Public Shared Function PropertyWrite(ByVal modif As String, ByVal elTipo As String, ByVal nombre As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} {1} {2}{{", modificador(modif), Tipo(elTipo), nombre) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} WriteOnly Property {1}() As {2}", modif, nombre, elTipo) End If End Function Public Shared Function PropertyWrite(ByVal modif As String, ByVal elTipo As String, ByVal index As String, ByVal tipoIndex As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} {1} this[{2} {3}]{{", modificador(modif), Tipo(elTipo), Tipo(tipoIndex), index) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} Default WriteOnly Property Item({1} As {2}) As {3}", modif, index, tipoIndex, elTipo) End If End Function Public Shared Function EndProperty() As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "End Property" End If End Function Public Shared Function [Sub](ByVal modif As String, ByVal nombre As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} void {1}(){{", modificador(modif), nombre) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} Sub {1}()", modif, nombre) End If End Function Public Shared Function [Sub](ByVal modif As String, ByVal nombre As String, _ ByVal vNombre As String, ByVal vTipo As String, _ ByVal ParamArray vars() As String) As String ' los parámetros opcionales (paramarray) se usará para indicar el nombre de la variable y el tipo separados por coma ' en caso de usar ByRef (ref en C#) indicarlo en el nombre de la variable: ... uno, String, ByRef dos, Integer Dim sb As New System.Text.StringBuilder ' sb.Append(Variable(vNombre, vTipo)) For i As Integer = 0 To vars.Length - 2 Step 2 sb.AppendFormat(", {0}", Variable(vars(i), vars(i + 1))) Next If Lang = eLenguaje.eCS Then Return String.Format("{0} void {1}({2}){{", modificador(modif), nombre, sb.ToString) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} Sub {1}({2})", modif, nombre, sb.ToString) End If End Function Public Shared Function EndSub() As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "End Sub" End If End Function Public Shared Function [Function](ByVal modif As String, ByVal nombre As String, ByVal elTipo As String) As String ' If elTipo = "" OrElse elTipo.ToLower = "void" OrElse elTipo.ToLower = "sub" Then Return [Sub](modif, nombre) End If ' If Lang = eLenguaje.eCS Then Return String.Format("{0} {1} {2}(){{", modificador(modif), Tipo(elTipo), nombre) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} Function {1}() As {2}", modif, nombre, elTipo) End If End Function Public Shared Function [Function](ByVal modif As String, ByVal nombre As String, ByVal elTipo As String, _ ByVal vNombre As String, ByVal vTipo As String, _ ByVal ParamArray vars() As String) As String ' If elTipo = "" OrElse elTipo.ToLower = "void" OrElse elTipo.ToLower = "sub" Then Return [Sub](modif, nombre, vNombre, vTipo, vars) End If ' Dim sb As New System.Text.StringBuilder ' sb.Append(Variable(vNombre, vTipo)) For i As Integer = 0 To vars.Length - 2 Step 2 sb.AppendFormat(", {0}", Variable(vars(i), vars(i + 1))) Next If Lang = eLenguaje.eCS Then Return String.Format("{0} {1} {2}({3}){{", modificador(modif), Tipo(elTipo), nombre, sb.ToString) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} Function {1}({2}) As {3}", modif, nombre, sb.ToString, elTipo) End If End Function Public Shared Function EndFunction() As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "End Function" End If End Function ' Public Shared Function [If](ByVal var As String, ByVal comp As String, ByVal valor As String) As String If Lang = eLenguaje.eCS Then Dim i As Integer = Array.IndexOf(compVB, comp) If i > -1 Then comp = compCS(i) Return String.Format("if({0} {1} {2}){{", comprobarParam(var), comp, comprobarParam(valor)) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("If {0} {1} {2} Then", var, comp, valor) End If End Function Public Shared Function [ElseIf](ByVal var As String, ByVal comp As String, ByVal valor As String) As String If Lang = eLenguaje.eCS Then Dim i As Integer = Array.IndexOf(compVB, comp) If i > -1 Then comp = compCS(i) Return String.Format("}}else if({0} {1} {2}){{", comprobarParam(var), comp, comprobarParam(valor)) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("ElseIf {0} {1} {2} Then", var, comp, valor) End If End Function Public Shared Function [Else]() As String If Lang = eLenguaje.eCS Then Return "}else{" ElseIf Lang = eLenguaje.eVBNET Then Return "Else" End If End Function Public Shared Function [EndIf]() As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "End If" End If End Function ' Public Shared Function [End](ByVal param As String) As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "End " & param End If End Function ' Public Shared Function [ForEach](ByVal var As String, ByVal elTipo As String, ByVal donde As String) As String If Lang = eLenguaje.eCS Then Return String.Format("foreach({0} {1} in {2}){{", Tipo(elTipo), var, donde) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("For Each {0} As {1} In {2}", var, elTipo, donde) End If End Function Public Shared Function [For](ByVal var As String, ByVal ini As String, ByVal fin As String) As String If Lang = eLenguaje.eCS Then Return String.Format("for({0} = {1}; {0} <= {2}; {0}++){{", var, ini, fin) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("For {0} = {1} To {2}", var, ini, fin) End If End Function Public Shared Function [For](ByVal var As String, ByVal ini As String, ByVal fin As String, ByVal incr As String) As String If Lang = eLenguaje.eCS Then Return String.Format("for({0} = {1}; {0} <= {2}; {0} + {3}){{", var, ini, fin, incr) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("For {0} = {1} To {2} Step {3}", var, ini, fin, incr) End If End Function Public Shared Function [Next]() As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "Next" End If End Function Public Shared Function [Next](ByVal param As String) As String If Lang = eLenguaje.eCS Then Return "}" ElseIf Lang = eLenguaje.eVBNET Then Return "Next " & param End If End Function ' Public Shared Function [Return](ByVal valor As String) As String If Lang = eLenguaje.eCS Then Return String.Format("return {0};", comprobarParam(valor)) ElseIf Lang = eLenguaje.eVBNET Then Return "Return " & valor End If End Function Public Shared Function [Exit](ByVal salirDe As String) As String If Lang = eLenguaje.eCS Then If "function sub property operator".IndexOf(salirDe.ToLower) > -1 Then Return "return;" Else Return "break;" End If ElseIf Lang = eLenguaje.eVBNET Then Return "Exit " & salirDe End If End Function ' Public Shared Function Asigna(ByVal var As String, ByVal valor As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} = {1};", comprobarParam(var), comprobarParam(valor)) ElseIf Lang = eLenguaje.eVBNET Then If valor.StartsWith(Chr(34)) = False Then Return String.Format("{0} = {1}", var, valor).Replace("[", "(").Replace("]", ")") Else Return String.Format("{0} = {1}", var, valor) End If End If End Function Public Shared Function AsignaNew(ByVal var As String, ByVal valor As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} = new {1};", comprobarParam(var), comprobarParam(valor)) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} = New {1}", var, valor) End If End Function Public Shared Function AsignaNew(ByVal var As String, ByVal valor As String, ByVal param As String) As String If Lang = eLenguaje.eCS Then Return String.Format("{0} = new {1}({2});", comprobarParam(var), comprobarParam(valor), comprobarParam(param)) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} = New {1}({2})", var, valor, param) End If End Function ' Public Shared Function Instruccion(ByVal cod As String) As String If Lang = eLenguaje.eCS Then Return comprobarParam(cod) & ";" ElseIf Lang = eLenguaje.eVBNET Then Return cod End If End Function Public Shared Function FinInstruccion() As String If Lang = eLenguaje.eCS Then Return ";" ElseIf Lang = eLenguaje.eVBNET Then Return "" End If End Function ' Public Shared Function Tipo(ByVal elTipo As String) As String If Lang = eLenguaje.eCS Then Dim i As Integer = Array.IndexOf(tiposVB, elTipo.ToLower) If i > -1 Then elTipo = tiposCS(i) End If End If Return elTipo End Function ' Public Shared Function Variable(ByVal nombre As String, ByVal elTipo As String) As String ' El tipo de datos lo daremos en formato VB If Lang = eLenguaje.eCS Then Return String.Format("{0} {1}", Tipo(elTipo), nombre) ElseIf Lang = eLenguaje.eVBNET Then Return String.Format("{0} As {1}", nombre, elTipo) End If End Function Public Shared Function DeclaraVariable(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String) As String Return Variable(modif, nombre, elTipo) End Function Public Shared Function Variable(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String) As String If Lang = eLenguaje.eCS Then If modif = "" OrElse modif.ToLower = "dim" Then Return String.Format("{0} {1};", Tipo(elTipo), nombre) Else Return String.Format("{0} {1} {2};", modificador(modif), Tipo(elTipo), nombre) End If ElseIf Lang = eLenguaje.eVBNET Then If modif = "" Then Return String.Format("{0} As {1}", nombre, elTipo) Else Return String.Format("{0} {1} As {2}", modif, nombre, elTipo) End If End If End Function Public Shared Function DeclaraVariable(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String, ByVal valor As String) As String Return Variable(modif, nombre, elTipo, valor) End Function Public Shared Function Variable(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String, ByVal valor As String) As String If Lang = eLenguaje.eCS Then If modif = "" OrElse modif.ToLower = "dim" Then Return String.Format("{0} {1} = {2};", Tipo(elTipo), nombre, comprobarParam(valor)) Else Return String.Format("{0} {1} {2} = {3};", modificador(modif), Tipo(elTipo), nombre, comprobarParam(valor)) End If ElseIf Lang = eLenguaje.eVBNET Then If modif = "" Then Return String.Format("{0} As {1} = {2}", nombre, elTipo, valor) Else Return String.Format("{0} {1} As {2} = {3}", modif, nombre, elTipo, valor) End If End If End Function Public Shared Function DeclaraVariableNew(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String) As String Return VariableNew(modif, nombre, elTipo) End Function Public Shared Function VariableNew(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String) As String If Lang = eLenguaje.eCS Then If modif = "" OrElse modif.ToLower = "dim" Then If elTipo.IndexOf("(") > -1 Then Return String.Format("{0} {1} = new {0};", Tipo(elTipo), nombre) Else Return String.Format("{0} {1} = new {0}();", Tipo(elTipo), nombre) End If Else If elTipo.IndexOf("(") > -1 Then Return String.Format("{0} {1} {2} = new {1};", modificador(modif), Tipo(elTipo), nombre) Else Return String.Format("{0} {1} {2} = new {1}();", modificador(modif), Tipo(elTipo), nombre) End If End If ElseIf Lang = eLenguaje.eVBNET Then If modif = "" Then Return String.Format("{0} As New {1}", nombre, elTipo) Else Return String.Format("{0} {1} As New {2}", modif, nombre, elTipo) End If End If End Function Public Shared Function DeclaraVariableNewParam(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String) As String Return VariableNewParam(modif, nombre, elTipo) End Function Public Shared Function VariableNewParam(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String) As String If Lang = eLenguaje.eCS Then If modif = "" OrElse modif.ToLower = "dim" Then Return String.Format("{0} {1} = new {0};", Tipo(elTipo), nombre) Else Return String.Format("{0} {1} {2} = new {1};", modificador(modif), Tipo(elTipo), nombre) End If ElseIf Lang = eLenguaje.eVBNET Then If modif = "" Then Return String.Format("{0} As New {1}", nombre, elTipo) Else Return String.Format("{0} {1} As New {2}", modif, nombre, elTipo) End If End If End Function Public Shared Function DeclaraVariableNewParam(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String, ByVal param As String) As String Return VariableNewParam(modif, nombre, elTipo, param) End Function Public Shared Function VariableNewParam(ByVal modif As String, ByVal nombre As String, ByVal elTipo As String, ByVal param As String) As String If Lang = eLenguaje.eCS Then If modif = "" OrElse modif.ToLower = "dim" Then Return String.Format("{0} {1} = new {0}({2});", Tipo(elTipo), nombre, comprobarParam(param)) Else Return String.Format("{0} {1} {2} = new {1}({3});", modificador(modif), Tipo(elTipo), nombre, comprobarParam(param)) End If ElseIf Lang = eLenguaje.eVBNET Then If modif = "" Then Return String.Format("{0} As New {1}({2})", nombre, elTipo, param) Else Return String.Format("{0} {1} As New {2}({3})", modif, nombre, elTipo, param) End If End If End Function ' ' comprobar si tiene New... Private Shared Function comprobarParam(ByVal var As String) As String ' por ejemplo, en un parámetro se puede indicar "New LosQueSea" If Lang = eLenguaje.eCS Then For i As Integer = 0 To instrVB.Length - 1 var = (" " & var).Replace(instrVB(i), instrCS(i)).Substring(1) Next ' Return var Else Return var End If End Function End Class End Namespace...
//------------------------------------------------------------------------------ // Clase para crear trozos de código en VB y C# (07/Jul/04) // los métodos también generan el código de VB // Inicialmente se usará código de VB para crear el de C# // al menos en lo que a los tipos de datos, tipos de métodos, etc. // // // ©Guillermo 'guille' Som, 2004 //------------------------------------------------------------------------------ // using System; namespace elGuille.Data.CodeGenerator{ // public enum eLenguaje{ eVBNET, eCS }; public class ConvLang{ public static eLenguaje Lang = eLenguaje.eVBNET; // las comprobaciones se harán sin tener en cuenta mayúsculas/minúsculas static string[] tiposVB = {"short", "integer", "long", "string", "object", "double", "single", "date", "decimal", "boolean", "byte", "char"}; static string[] tiposCS = {"short", "int", "long", "string", "object", "double", "float", "DateTime", "decimal", "bool", "byte", "char"}; static string[] modificadoresVB = {"public", "private", "friend", "protected", "shared", "overrides", "overridable", "shadows"}; static string[] modificadoresCS = {"public", "private", "internal", "protected", "static", "override", "virtual", "new"}; static string[] compVB = {"=", "<>"}; static string[] compCS = {"==", "!="}; static string[] instrVB = {".Rows(0)", "New ", ", Me", " Me.", " Me(", " Me,", "(Me,", "True", "False", " Nothing", " & ", "\" \"" + "c"}; static string[] instrCS = {".Rows[0]", "new ", ", this", " this.", " this(", " this,", "(this,", "true", "false", " null", " + ", "' '"}; // private static string modificador(string modif) { // el parámetro puede incluir más de uno (separado por espacio) // por ejemplo: Public Shared if( Lang == eLenguaje.eVBNET ) return modif; // string s1 = ""; int i; string[] a = modif.Split(' '); // foreach(string s in a){ if( s != "" ){ i = Array.IndexOf(modificadoresVB, s.ToLower()); if( i > -1 ){ s1 += " " + modificadoresCS[i]; }else{ s1 += " " + s; } } } return s1.TrimStart(); } // public static string Comentario() { if( Lang == eLenguaje.eCS ){ return ""//"; }else if( Lang == eLenguaje.eVBNET ){ return "'"; } return ""; } // esta sobrecarga la uso para indicar si se pone o no el comentario // (para cuando las opciones son opcionales) public static string Comentario(bool poner) { if( Lang == eLenguaje.eCS ){ if( poner ){ return ""//"; }else{ return ""; } }else if( Lang == eLenguaje.eVBNET ){ if( poner ){ return "'"; }else{ return ""; } } return ""; } public static string Comentario(string coment) { if( Lang == eLenguaje.eCS ){ return ""//" + coment; }else if( Lang == eLenguaje.eVBNET ){ return "'" + coment; } return ""; } public static string Imports(string espacio) { if( Lang == eLenguaje.eCS ){ return "using " + espacio + ";"; }else if( Lang == eLenguaje.eVBNET ){ return "Imports " + espacio; } return ""; } public static string Class(string modif, string nombre) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} class {1}{{", modificador(modif), nombre); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} Class {1}", modif, nombre); } return ""; } public static string EndClass() { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "End Class"; } return ""; } // public static string Constructor(string nombreClase) { return Constructor("", nombreClase); } public static string Constructor(string modif, string nombreClase) { if( Lang == eLenguaje.eCS ){ if( modif == "" ) modif = "Friend"; return String.Format("{0} {1}(){{", modificador(modif), nombreClase); }else if( Lang == eLenguaje.eVBNET ){ if( modif == "" ){ return "Friend Sub New()"; }else{ return String.Format("{0} Sub New()", modif); } } return ""; } public static string Constructor(string modif, string nombreClase, string var, string elTipo) { if( Lang == eLenguaje.eCS ){ if( modif == "" ) modif = "Friend"; return String.Format("{0} {1}({2}){{", modificador(modif), nombreClase, Variable(var, elTipo)); }else if( Lang == eLenguaje.eVBNET ){ if( modif == "" ){ return "Friend Sub New()"; }else{ return String.Format("{0} Sub New({1})", modif, Variable(var, elTipo)); } } return ""; } // public static string Try() { if( Lang == eLenguaje.eCS ){ return "try{"; }else if( Lang == eLenguaje.eVBNET ){ return "Try"; } return ""; } public static string EndTry() { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "End Try"; } return ""; } public static string Catch() { if( Lang == eLenguaje.eCS ){ return "}catch{"; }else if( Lang == eLenguaje.eVBNET ){ return "Catch"; } return ""; } public static string Catch(string var, string elTipo) { if( Lang == eLenguaje.eCS ){ return String.Format("}}catch({0}){{", Variable(var, elTipo)); }else if( Lang == eLenguaje.eVBNET ){ return "Catch " + Variable(var, elTipo); } return ""; } public static string Finally() { if( Lang == eLenguaje.eCS ){ return "finally{"; }else if( Lang == eLenguaje.eVBNET ){ return "Finally"; } return ""; } // public static string Get() { if( Lang == eLenguaje.eCS ){ return "get{"; }else if( Lang == eLenguaje.eVBNET ){ return "Get"; } return ""; } public static string EndGet() { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "End Get"; } return ""; } public static string Set(string elTipo) { if( Lang == eLenguaje.eCS ){ return "set{"; }else if( Lang == eLenguaje.eVBNET ){ return "Set(value As " + elTipo + ")"; } return ""; } public static string EndSet() { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "End Set"; } return ""; } // public static string Property(string modif, string elTipo, string nombre) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} {1} {2}{{", modificador(modif), Tipo(elTipo), nombre); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} Property {1}() As {2}", modif, nombre, elTipo); } return ""; } public static string Property(string modif, string elTipo, string index, string tipoIndex) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} {1} this[{2} {3}]{{", modificador(modif), Tipo(elTipo), Tipo(tipoIndex), index); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} Default Property Item({1} As {2}) As {3}", modif, index, tipoIndex, elTipo); } return ""; } public static string PropertyRead(string modif, string elTipo, string nombre) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} {1} {2}{{", modificador(modif), Tipo(elTipo), nombre); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} ReadOnly Property {1}() As {2}", modif, nombre, elTipo); } return ""; } public static string PropertyRead(string modif, string elTipo, string index, string tipoIndex) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} {1} this[{2} {3}]{{", modificador(modif), Tipo(elTipo), Tipo(tipoIndex), index); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} Default ReadOnly Property Item({1} As {2}) As {3}", modif, index, tipoIndex, elTipo); } return ""; } public static string PropertyWrite(string modif, string elTipo, string nombre) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} {1} {2}{{", modificador(modif), Tipo(elTipo), nombre); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} WriteOnly Property {1}() As {2}", modif, nombre, elTipo); } return ""; } public static string PropertyWrite(string modif, string elTipo, string index, string tipoIndex) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} {1} this[{2} {3}]{{", modificador(modif), Tipo(elTipo), Tipo(tipoIndex), index); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} Default WriteOnly Property Item({1} As {2}) As {3}", modif, index, tipoIndex, elTipo); } return ""; } public static string EndProperty() { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "End Property"; } return ""; } public static string Sub(string modif, string nombre) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} void {1}(){{", modificador(modif), nombre); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} Sub {1}()", modif, nombre); } return ""; } public static string Sub(string modif, string nombre, string vNombre, string vTipo, params string[] vars) { // los parámetros opcionales (paramarray) se usará para indicar el nombre de la variable y el tipo separados por coma // en caso de usar ByRef (ref en C#) indicarlo en el nombre de la variable: ... uno, String, ByRef dos, Integer System.Text.StringBuilder sb = new System.Text.StringBuilder(); // sb.Append(Variable(vNombre, vTipo)); for(int i= 0; i<= vars.Length - 2; i+= 2){ sb.AppendFormat(", {0}", Variable(vars[i], vars[i + 1])); } if( Lang == eLenguaje.eCS ){ return String.Format("{0} void {1}({2}){{", modificador(modif), nombre, sb.ToString()); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} Sub {1}({2})", modif, nombre, sb.ToString()); } return ""; } public static string EndSub() { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "End Sub"; } return ""; } public static string Function(string modif, string nombre, string elTipo) { // if( elTipo == "" || elTipo.ToLower() == "void" || elTipo.ToLower() == "sub" ){ return Sub(modif, nombre); } // if( Lang == eLenguaje.eCS ){ return String.Format("{0} {1} {2}(){{", modificador(modif), Tipo(elTipo), nombre); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} Function {1}() As {2}", modif, nombre, elTipo); } return ""; } public static string Function(string modif, string nombre, string elTipo, string vNombre, string vTipo, params string[] vars) { // if( elTipo == "" || elTipo.ToLower() == "void" || elTipo.ToLower() == "sub" ){ return Sub(modif, nombre, vNombre, vTipo, vars); } // System.Text.StringBuilder sb = new System.Text.StringBuilder(); // sb.Append(Variable(vNombre, vTipo)); for(int i= 0; i<= vars.Length - 2; i+= 2){ sb.AppendFormat(", {0}", Variable(vars[i], vars[i + 1])); } if( Lang == eLenguaje.eCS ){ return String.Format("{0} {1} {2}({3}){{", modificador(modif), Tipo(elTipo), nombre, sb.ToString()); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} Function {1}({2}) As {3}", modif, nombre, sb.ToString(), elTipo); } return ""; } public static string EndFunction() { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "End Function"; } return ""; } // public static string If(string var, string comp, string valor) { if( Lang == eLenguaje.eCS ){ int i = Array.IndexOf(compVB, comp); if( i > -1 ) comp = compCS[i]; return String.Format("if({0} {1} {2}){{", comprobarParam(var), comp, comprobarParam(valor)); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("If {0} {1} {2} Then", var, comp, valor); } return ""; } public static string ElseIf(string var, string comp, string valor) { if( Lang == eLenguaje.eCS ){ int i = Array.IndexOf(compVB, comp); if( i > -1 ) comp = compCS[i]; return String.Format("}}else if({0} {1} {2}){{", comprobarParam(var), comp, comprobarParam(valor)); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("ElseIf {0} {1} {2} Then", var, comp, valor); } return ""; } public static string Else() { if( Lang == eLenguaje.eCS ){ return "}else{"; }else if( Lang == eLenguaje.eVBNET ){ return "Else"; } return ""; } public static string EndIf() { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "End If"; } return ""; } // public static string End(string param) { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "End " + param; } return ""; } // public static string ForEach(string var, string elTipo, string donde) { if( Lang == eLenguaje.eCS ){ return String.Format("foreach({0} {1} in {2}){{", Tipo(elTipo), var, donde); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("For Each {0} As {1} In {2}", var, elTipo, donde); } return ""; } public static string For(string var, string ini, string fin) { if( Lang == eLenguaje.eCS ){ return String.Format("for({0} = {1}; {0} <= {2}; {0}++){{", var, ini, fin); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("For {0} = {1} To {2}", var, ini, fin); } return ""; } public static string For(string var, string ini, string fin, string incr) { if( Lang == eLenguaje.eCS ){ return String.Format("for({0} = {1}; {0} <= {2}; {0} + {3}){{", var, ini, fin, incr); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("For {0} = {1} To {2} Step {3}", var, ini, fin, incr); } return ""; } public static string Next() { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "Next"; } return ""; } public static string Next(string param) { if( Lang == eLenguaje.eCS ){ return "}"; }else if( Lang == eLenguaje.eVBNET ){ return "Next " + param; } return ""; } // public static string Return(string valor) { if( Lang == eLenguaje.eCS ){ return String.Format("return {0};", comprobarParam(valor)); }else if( Lang == eLenguaje.eVBNET ){ return "Return " + valor; } return ""; } public static string Exit(string salirDe) { if( Lang == eLenguaje.eCS ){ if( "function sub property operator".IndexOf(salirDe.ToLower()) > -1 ){ return "return;"; }else{ return "break;"; } }else if( Lang == eLenguaje.eVBNET ){ return "Exit " + salirDe; } return ""; } // public static string Asigna(string var, string valor) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} = {1};", comprobarParam(var), comprobarParam(valor)); }else if( Lang == eLenguaje.eVBNET ){ if( valor.StartsWith("\"") == false ){" return String.Format("{0} = {1}", var, valor).Replace("[", "(").Replace("]", ")"); }else{ return String.Format("{0} = {1}", var, valor); } } return ""; } public static string AsignaNew(string var, string valor) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} = new {1};", comprobarParam(var), comprobarParam(valor)); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} = New {1}", var, valor); } return ""; } public static string AsignaNew(string var, string valor, string param) { if( Lang == eLenguaje.eCS ){ return String.Format("{0} = new {1}({2});", comprobarParam(var), comprobarParam(valor), comprobarParam(param)); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} = New {1}({2})", var, valor, param); } return ""; } // public static string Instruccion(string cod) { if( Lang == eLenguaje.eCS ){ return comprobarParam(cod) + ";"; }else if( Lang == eLenguaje.eVBNET ){ return cod; } return ""; } public static string FinInstruccion() { if( Lang == eLenguaje.eCS ){ return ";"; }else if( Lang == eLenguaje.eVBNET ){ return ""; } return ""; } // public static string Tipo(string elTipo) { if( Lang == eLenguaje.eCS ){ int i = Array.IndexOf(tiposVB, elTipo.ToLower()); if( i > -1 ){ elTipo = tiposCS[i]; } } return elTipo; } // public static string Variable(string nombre, string elTipo) { // El tipo de datos lo daremos en formato VB if( Lang == eLenguaje.eCS ){ return String.Format("{0} {1}", Tipo(elTipo), nombre); }else if( Lang == eLenguaje.eVBNET ){ return String.Format("{0} As {1}", nombre, elTipo); } return ""; } public static string DeclaraVariable(string modif, string nombre, string elTipo) { return Variable(modif, nombre, elTipo); } public static string Variable(string modif, string nombre, string elTipo) { if( Lang == eLenguaje.eCS ){ if( modif == "" || modif.ToLower() == "dim" ){ return String.Format("{0} {1};", Tipo(elTipo), nombre); }else{ return String.Format("{0} {1} {2};", modificador(modif), Tipo(elTipo), nombre); } }else if( Lang == eLenguaje.eVBNET ){ if( modif == "" ){ return String.Format("{0} As {1}", nombre, elTipo); }else{ return String.Format("{0} {1} As {2}", modif, nombre, elTipo); } } return ""; } public static string DeclaraVariable(string modif, string nombre, string elTipo, string valor) { return Variable(modif, nombre, elTipo, valor); } public static string Variable(string modif, string nombre, string elTipo, string valor) { if( Lang == eLenguaje.eCS ){ if( modif == "" || modif.ToLower() == "dim" ){ return String.Format("{0} {1} = {2};", Tipo(elTipo), nombre, comprobarParam(valor)); }else{ return String.Format("{0} {1} {2} = {3};", modificador(modif), Tipo(elTipo), nombre, comprobarParam(valor)); } }else if( Lang == eLenguaje.eVBNET ){ if( modif == "" ){ return String.Format("{0} As {1} = {2}", nombre, elTipo, valor); }else{ return String.Format("{0} {1} As {2} = {3}", modif, nombre, elTipo, valor); } } return ""; } public static string DeclaraVariableNew(string modif, string nombre, string elTipo) { return VariableNew(modif, nombre, elTipo); } public static string VariableNew(string modif, string nombre, string elTipo) { if( Lang == eLenguaje.eCS ){ if( modif == "" || modif.ToLower() == "dim" ){ if( elTipo.IndexOf("(") > -1 ){ return String.Format("{0} {1} = new {0};", Tipo(elTipo), nombre); }else{ return String.Format("{0} {1} = new {0}();", Tipo(elTipo), nombre); } }else{ if( elTipo.IndexOf("(") > -1 ){ return String.Format("{0} {1} {2} = new {1};", modificador(modif), Tipo(elTipo), nombre); }else{ return String.Format("{0} {1} {2} = new {1}();", modificador(modif), Tipo(elTipo), nombre); } } }else if( Lang == eLenguaje.eVBNET ){ if( modif == "" ){ return String.Format("{0} As New {1}", nombre, elTipo); }else{ return String.Format("{0} {1} As New {2}", modif, nombre, elTipo); } } return ""; } public static string DeclaraVariableNewParam(string modif, string nombre, string elTipo) { return VariableNewParam(modif, nombre, elTipo); } public static string VariableNewParam(string modif, string nombre, string elTipo) { if( Lang == eLenguaje.eCS ){ if( modif == "" || modif.ToLower() == "dim" ){ return String.Format("{0} {1} = new {0};", Tipo(elTipo), nombre); }else{ return String.Format("{0} {1} {2} = new {1};", modificador(modif), Tipo(elTipo), nombre); } }else if( Lang == eLenguaje.eVBNET ){ if( modif == "" ){ return String.Format("{0} As New {1}", nombre, elTipo); }else{ return String.Format("{0} {1} As New {2}", modif, nombre, elTipo); } } return ""; } public static string DeclaraVariableNewParam(string modif, string nombre, string elTipo, string param) { return VariableNewParam(modif, nombre, elTipo, param); } public static string VariableNewParam(string modif, string nombre, string elTipo, string param) { if( Lang == eLenguaje.eCS ){ if( modif == "" || modif.ToLower() == "dim" ){ return String.Format("{0} {1} = new {0}({2});", Tipo(elTipo), nombre, comprobarParam(param)); }else{ return String.Format("{0} {1} {2} = new {1}({3});", modificador(modif), Tipo(elTipo), nombre, comprobarParam(param)); } }else if( Lang == eLenguaje.eVBNET ){ if( modif == "" ){ return String.Format("{0} As New {1}({2})", nombre, elTipo, param); }else{ return String.Format("{0} {1} As New {2}({3})", modif, nombre, elTipo, param); } } return ""; } // // comprobar si tiene New... private static string comprobarParam(string var) { // por ejemplo, en un parámetro se puede indicar "New LosQueSea" if( Lang == eLenguaje.eCS ){ for(int i= 0; i<= instrVB.Length - 1; i++){ var = (" " + var).Replace(instrVB[i], instrCS[i]).Substring(1); } // return var; }else{ return var; } } } }...