|
9401
|
Programación / .NET (C#, VB.NET, ASP) / Re: Ayuda en ejercicio basico
|
en: 16 Abril 2013, 20:20 pm
|
Te está diciendo que intentas usar una variable que no tiene ningún valor asignado. Asígnale un valor, y listo: float primernumero = 0; float segundonumero = 0; float resultado = 0; string operacion = null;
El ¿porque?, pues me imagino que porque C# es así de restrictivo, no sé, no manejo C#...  Saludos!
|
|
|
9402
|
Programación / .NET (C#, VB.NET, ASP) / Re: Problema accediendo a un objecto en un Form !!
|
en: 16 Abril 2013, 20:12 pm
|
Reference to a non-shared member requires an object reference.
este es el error que me atormenta
Pues no tienes que atormentarte xD, simplemente declara las cosas como compartidas (Shared), y listo. Ejemplo: Public Class Class1 Public Shared SharedVar As String = "Test" ' Esta la podrás leer Public Var As String = "Test"' Esta no la podrás leer End Class
Imports WindowsApplication1.Class1 Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load MsgBox(SharedVar) ' String: "Test" MsgBox(Var) ' Exception: Reference to a non-shared member requires an object reference End Sub End Class
Saludos!
|
|
|
9403
|
Programación / .NET (C#, VB.NET, ASP) / Re: Librería de Snippets !! (Posteen aquí sus snippets)
|
en: 16 Abril 2013, 16:38 pm
|
· Devuelve el valor de un nombre de un Enum #Region " Get Enum Value " ' [ Get Enum Value Function ] ' ' Examples : ' MsgBox(Get_Enum_Value(DayOfWeek.Sunday)) ' Result: 0 ' MsgBox(Get_Enum_Value(DayOfWeek.Monday)) ' Result: 1 Function Get_Enum_Value(Of T)(Byval ValueName As T) As Int32 Return Convert.ToInt32(ValueName) End Function #End Region
· Devuelve el nombre de un valor de un Enum #Region " Get Enum Name " ' [ Get Enum ValueName Function ] ' ' Examples : ' MsgBox(Get_Enum_Name(Of DayOfWeek)(0)) ' Result: Sunday ' MsgBox(Get_Enum_Name(Of DayOfWeek)(1)) ' Result: Monday Private Function Get_Enum_Name(Of T)(EnumValue As Integer) As String Return [Enum].GetName(GetType(T), EnumValue) End Function #End Region
· Comparar dos archivos: #Region " Compare Files " ' [ Compare Files Function ] ' ' Examples : ' MsgBox(Compare_Files("C:\File1.txt", "C:\File2.txt")) Private Function Compare_Files(ByVal File1 As String, ByVal File2 As String) As Boolean ' Set to true if the files are equal; false otherwise Dim FilesAreEqual As Boolean = False With My.Computer.FileSystem ' Ensure that the files are the same length before comparing them line by line. If .GetFileInfo(File1).Length = .GetFileInfo(File2).Length Then Using file1Reader As New FileStream(File1, FileMode.Open), _ file2Reader As New FileStream(File2, FileMode.Open) Dim byte1 As Integer = file1Reader.ReadByte() Dim byte2 As Integer = file2Reader.ReadByte() ' If byte1 or byte2 is a negative value, we have reached the end of the file. While byte1 >= 0 AndAlso byte2 >= 0 If (byte1 <> byte2) Then FilesAreEqual = False Exit While Else FilesAreEqual = True End If ' Read the next byte. byte1 = file1Reader.ReadByte() byte2 = file2Reader.ReadByte() End While End Using End If End With Return FilesAreEqual End Function #End Region
|
|
|
9404
|
Programación / .NET (C#, VB.NET, ASP) / Re: Librería de Snippets !! (Posteen aquí sus snippets)
|
en: 16 Abril 2013, 15:31 pm
|
· Un AppActivate distinto, en mi opinión mejor, se usa por el nombre del proceso, con posibilidad de seleccionar el proceso por el título de la ventana de dicho proceso: #Region " Activate APP " ' [ Activate APP Function ] ' ' // By Elektro H@cker ' ' Examples : ' ActivateAPP("notepad.exe") ' ActivateAPP("notepad.exe", "Notepad Sub-Window Title") ' MsgBox(ActivateAPP("notepad")) Private Function ActivateAPP(ByVal ProcessName As String, _ Optional ByVal WindowTitle As String = Nothing) As Boolean If ProcessName.ToLower.EndsWith(".exe") Then ProcessName = ProcessName.Substring(0, ProcessName.Length - 4) Dim ProcessTitle As String = Nothing Dim ProcessArray = Process.GetProcessesByName(ProcessName) If ProcessArray.Length = 0 Then : Return False ' ProcessName not found ElseIf ProcessArray.Length > 1 AndAlso Not WindowTitle Is Nothing Then For Each Title In ProcessArray If Title.MainWindowTitle.Contains(WindowTitle) Then _ ProcessTitle = Title.MainWindowTitle Next Else : ProcessTitle = ProcessArray(0).MainWindowTitle End If AppActivate(ProcessTitle) Return True ' Window activated End Function #End Region
· Escribe texto en un Log #Region " Write Log " ' [ Write Log Function ] ' ' // By Elektro H@cker ' ' Examples : ' WriteLog("Application started", InfoType.Information) ' WriteLog("Application got mad", InfoType.Critical) Dim LogFile = CurDir() & "\" & System.Reflection.Assembly.GetExecutingAssembly.GetName().Name & ".log" Public Enum InfoType Information Exception Critical None End Enum Private Function WriteLog(ByVal Message As String, ByVal InfoType As InfoType) As Boolean Dim LocalDate As String = My.Computer.Clock.LocalTime.ToString.Split(" ").First Dim LocalTime As String = My.Computer.Clock.LocalTime.ToString.Split(" ").Last Dim LogDate As String = "[ " & LocalDate & " ] " & " [ " & LocalTime & " ] " Dim MessageType As String = Nothing Select Case InfoType Case InfoType.Information : MessageType = "Information: " Case InfoType.Exception : MessageType = "Error: " Case InfoType.Critical : MessageType = "Critical: " Case InfoType.None : MessageType = "" End Select Try My.Computer.FileSystem.WriteAllText(LogFile, vbNewLine & LogDate & MessageType & Message & vbNewLine, True) Return True Catch ex As Exception 'Return False Throw New Exception(ex.Message) End Try End Function #End Region
· Cierra un proceso (No lo mata) #Region " Close Process Function " ' [ Close Process Function ] ' ' Examples : ' ' Close_Process(Application.ExecutablePath) ' Close_Process("notepad.exe") ' Close_Process("notepad", False) Private Function Close_Process(ByRef Process_Name As String, _ Optional ByVal OnlyFirstFound As Boolean = True) As Boolean If Process_Name.ToLower.EndsWith(".exe") Then Process_Name = Process_Name.Substring(0, Process_Name.Length - 4) Dim proc() As Process = Process.GetProcessesByName(Process_Name) If Not OnlyFirstFound Then For proc_num As Integer = 0 To proc.Length - 1 Try : proc(proc_num).CloseMainWindow() _ : Catch : Return False : End Try ' One of the processes can't be closed Next Return True Else Try : proc(0).CloseMainWindow() : Return True ' Close message sent to the process Catch : Return False : End Try ' Can't close the process End If Return Nothing ' ProcessName not found End Function #End Region
· Buscar coincidencias de texto usando expresiones regulares #Region " Find RegEx " ' [ Find RegEx Function ] ' ' // By Elektro H@cker ' ' Examples : ' If Find_RegEx("abcdef", "^[A-Z]+$") Then MsgBox("Yes") Else MsgBox("No") ' Result: No ' If Find_RegEx("abcdef", "^[A-Z]+$", True) Then MsgBox("Yes") Else MsgBox("No") ' Result: Yes Private Function Find_RegEx(ByVal str As String, ByVal Pattern As String, _ Optional ByVal Ignorecase As Boolean = False) As Boolean Dim RegExCase As System.Text.RegularExpressions.RegexOptions If Ignorecase Then _ RegExCase = System.Text.RegularExpressions.RegexOptions.IgnoreCase _ Else RegExCase = System.Text.RegularExpressions.RegexOptions.None Dim RegEx As New System.Text.RegularExpressions.Regex(Pattern, RegExCase) Return RegEx.IsMatch(str) End Function #End Region
· Leer un texto línea por línea (For each line...) con posibilidad de saltar líneas en blanco. #Region " Read TextFile Libe By Line " ' [ Read TextFile Libe By Line ] ' ' // By Elektro H@cker ' ' Examples : ' Read_TextFile_Libe_By_Line("C:\Test.txt") ' Read_TextFile_Libe_By_Line("C:\Test.txt", True) Private Sub Read_TextFile_Libe_By_Line(ByVal TextFile As String, _ Optional ByVal Read_Blank_Lines As Boolean = False) Dim Line As String = Nothing Dim Text As IO. StreamReader = IO. File. OpenText(TextFile ) Dim RegEx As New System.Text.RegularExpressions.Regex("^\s+$") Do Until Text.EndOfStream Line = Text.ReadLine() If (Not Read_Blank_Lines _ AndAlso _ (Not Line = "" _ And Not RegEx.IsMatch(Line))) _ OrElse Read_Blank_Lines Then ' Do things here... MsgBox(Line) End If Loop Text.Close() : Text.Dispose() End Sub #End Region
|
|
|
9406
|
Programación / .NET (C#, VB.NET, ASP) / Re: Librería de Snippets !! (Posteen aquí sus snippets)
|
en: 16 Abril 2013, 12:06 pm
|
La función de convertir un string a Case, mejorada y mucho más ampliada: #Region " String to Case " ' [ String to Case Function ] ' ' // By Elektro H@cker ' ' Examples : ' MsgBox(String_To_Case("THiS is a TeST", StringCase.Titlecase)) ' MsgBox(String_To_Case("THiS is a TeST", StringCase.DelimitedCase_Lower, ";")) ' Var = String_To_WordCase(Var, StringCase.LowerCase) Public Enum StringCase LowerCase UpperCase Titlecase WordCase CamelCase_First_Lower CamelCase_First_Upper MixedCase_First_Lower MixedCase_First_Upper MixedCase_Word_Lower MixedCase_Word_Upper DelimitedCase_Lower DelimitedCase_Mixed_Word_Lower DelimitedCase_Mixed_Word_Upper DelimitedCase_Title DelimitedCase_Upper DelimitedCase_Word End Enum Private Function String_To_Case(ByVal str As String, _ ByVal StringCase As StringCase, _ Optional ByVal Delimiter As String = "-") As String Select Case StringCase Case StringCase.LowerCase Return str.ToLower Case StringCase.UpperCase Return str.ToUpper Case StringCase.Titlecase Return Char.ToUpper(str(0)) + StrConv(str.Substring(1), VbStrConv.Lowercase) Case StringCase.WordCase Return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str) Case StringCase.CamelCase_First_Lower Return Char.ToLower(str(0)) & _ System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str).Replace(" ", "").Substring(1) Case StringCase.CamelCase_First_Upper Return Char.ToUpper(str(0)) & _ System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str).Replace(" ", "").Substring(1) Case StringCase.MixedCase_First_Lower Dim MixedString As String = Nothing For X As Integer = 0 To str.Length - 1 Dim c As Char = str(X) If (X / 2).ToString.Contains(",") Then _ MixedString += c.ToString.ToUpper _ Else MixedString += c.ToString.ToLower Next Return MixedString Case StringCase.MixedCase_First_Upper Dim MixedString As String = Nothing For X As Integer = 0 To str.Length - 1 Dim c As Char = str(X) If (X / 2).ToString.Contains(",") Then _ MixedString += c.ToString.ToLower _ Else MixedString += c.ToString.ToUpper Next Return MixedString Case StringCase.MixedCase_Word_Lower Dim MixedString As String = Nothing Dim Count As Integer = 1 For X As Integer = 0 To str.Length - 1 Dim c As Char = str(X) If Not c = " " Then Count += 1 Else Count = 1 If (Count / 2).ToString.Contains(",") Then _ MixedString += c.ToString.ToUpper _ Else MixedString += c.ToString.ToLower Next Return MixedString Case StringCase.MixedCase_Word_Upper Dim MixedString As String = Nothing Dim Count As Integer = 1 For X As Integer = 0 To str.Length - 1 Dim c As Char = str(X) If Not c = " " Then Count += 1 Else Count = 1 If (Count / 2).ToString.Contains(",") Then _ MixedString += c.ToString.ToLower _ Else MixedString += c.ToString.ToUpper Next Return MixedString Case StringCase.DelimitedCase_Lower Dim rgx As New System.Text.RegularExpressions.Regex("\s+") Return rgx.Replace(str.ToLower, Delimiter) Case StringCase.DelimitedCase_Upper Dim rgx As New System.Text.RegularExpressions.Regex("\s+") Return rgx.Replace(str.ToUpper, Delimiter) Case StringCase.DelimitedCase_Title Dim rgx As New System.Text.RegularExpressions.Regex("\s+") Return rgx.Replace(Char.ToUpper(str(0)) + StrConv(str.Substring(1), VbStrConv.Lowercase), Delimiter) Case StringCase.DelimitedCase_Word Dim rgx As New System.Text.RegularExpressions.Regex("\s+") Return rgx.Replace(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str), Delimiter) Case StringCase.DelimitedCase_Mixed_Word_Lower Dim MixedString As String = Nothing Dim Count As Integer = 1 For X As Integer = 0 To str.Length - 1 Dim c As Char = str(X) If Not c = " " Then Count += 1 Else Count = 1 If (Count / 2).ToString.Contains(",") Then _ MixedString += c.ToString.ToUpper _ Else MixedString += c.ToString.ToLower Next Dim rgx As New System.Text.RegularExpressions.Regex("\s+") Return rgx.Replace(MixedString, Delimiter) Case StringCase.DelimitedCase_Mixed_Word_Upper Dim MixedString As String = Nothing Dim Count As Integer = 1 For X As Integer = 0 To str.Length - 1 Dim c As Char = str(X) If Not c = " " Then Count += 1 Else Count = 1 If (Count / 2).ToString.Contains(",") Then _ MixedString += c.ToString.ToLower _ Else MixedString += c.ToString.ToUpper Next Dim rgx As New System.Text.RegularExpressions.Regex("\s+") Return rgx.Replace(MixedString, Delimiter) Case Else Return Nothing End Select End Function #End Region
|
|
|
9407
|
Programación / .NET (C#, VB.NET, ASP) / Re: Consulta sobre respuestas aleatorias.
|
en: 16 Abril 2013, 09:24 am
|
Se me ocurre que puedes almacenar tus variables en un Dictionary o un Hashtable, y de ahi sacar el índice aleatório y su valor correspondiente: Public Class Form1 Dim Misvariables As New Hashtable Dim Rando As New Random Dim MisVariablesLength As Int32 = 5 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Add values Misvariables.Add("Var1", True) Misvariables.Add("Var2", False) Misvariables.Add("Var3", 10) Misvariables.Add("Var4", 20) Misvariables.Add("Var5", "Test") End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' Get Random Index name Dim VariableKey As String = Misvariables.Keys(Rando.Next(0, MisVariablesLength)) ' Get Index Value MsgBox("Variable: " & VariableKey & vbNewLine & _ "Value: " & Misvariables.Item(VariableKey)) ' Get Random Value name ' MsgBox(Misvariables.Keys(Rando.Next(0, MisVariablesLength))) ' Get Random Value ' MsgBox(Misvariables.Item(Misvariables.Keys(Rando.Next(0, MisVariablesLength)))) End Sub End Class
Creo que de otra forma (es decir, con las variables de toda la vida en el código (Dim var as...)) tienes que usar reflection y es u trabajo duro, yo intenté algo parecido y al final lo hice usando un diccionario como te he comentado. Saludos!
|
|
|
9408
|
Programación / .NET (C#, VB.NET, ASP) / Re: mensaje de error c#
|
en: 16 Abril 2013, 09:05 am
|
El nombre txtId no existe en el conexto actual El nombre txtNombre no existe en el contexto actual
no entiendo por qué me da esos errores, esta es la primera vez que programo en c# if (txtId.Text == "" || txtNombre.Text == "") Eso equivale a: if (Objeto1.PropiedadText = vacío || Objeto2.PropiedadText = vacío) ¿Lo vas pillando  ?, en el formulário falta que agreges un control que tenga la propiedad " Text" (Por ejemplo un Textbox) con el nombre " txtId", y luego agregas otro control (otro textbox) y le cambias el nombre a: " txtNombre", puedes agregar los controles en el código o arrastrándolos desde el Designer. Saludos!
|
|
|
9409
|
Programación / Scripting / Re: [BATCH] [APORTE] USB MON (Roba USB)
|
en: 16 Abril 2013, 08:51 am
|
hola sabendonde puedo descargar este programa
No se descarga, es un código, el código que ves en el comentario principal lo copias en el bloc de notas y lo guardas como "archivo.BAT" PD: Aunque por otro lado tengo pensado trasladar el código a una aplicación de escritório en VB.NET... pero tiempo al tiempo xD. Saludos!
|
|
|
9410
|
Programación / .NET (C#, VB.NET, ASP) / Re: Librería de Snippets !! (Posteen aquí sus snippets)
|
en: 15 Abril 2013, 18:12 pm
|
· Convierte un string a LowerCase/Titlecase/UpperCase/WordCase #Region " String to Case " ' [ String to Case Function ] ' ' // By Elektro H@cker ' ' Examples : ' MsgBox(String_To_Case("THiS is a TeST", StringCase.Titlecase)) ' Var = String_To_WordCase(Var, StringCase.LowerCase) Public Enum StringCase LowerCase Titlecase UpperCase WordCase End Enum Private Function String_To_Case(ByVal str As String, ByVal StringCase As StringCase) As String Select Case StringCase Case Form1.StringCase.LowerCase : Return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToLower(str) Case Form1.StringCase.Titlecase : Return Char.ToUpper(str(0)) + StrConv(str.Substring(1), VbStrConv.Lowercase) Case Form1.StringCase.UpperCase : Return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToUpper(str) Case Form1.StringCase.WordCase : Return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str) Case Else : Return Nothing End Select End Function #End Region
|
|
|
|
|
|
|