Test Foro de elhacker.net SMF 2.1

Programación => Programación General => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Lain0x en 21 Octubre 2012, 04:47 AM

Título: Función Mid de String
Publicado por: Lain0x en 21 Octubre 2012, 04:47 AM
Hola estoy haciendo un programa que muestra la fecha, y después captura el día, mes y año y los imprime en textbox por separado al momento de presionar un botón.
El programa hace lo que quiero, pero muestra algunas excepciones abajo.

Código del botón:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     
        Dim c As String
        c = Date.Today
        T1.Text = c
        T2.Text = Mid(c, 1, Len(c) - 8)
        T3.Text = Mid(c, 4, Len(c) - 8)
        T4.Text = Mid(c, 7, Len(c) - 6)
       
    End Sub


Se ejecuta perfecto pero muestra esto abajo:

A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Título: Re: Función Mid de String
Publicado por: raul338 en 21 Octubre 2012, 17:20 PM
Ese codigo es de visual basic .net

Fijate bien que tengas bien en cuenta el tamaño del string, en lugar de usar Len, pon el numero exacto.

Aunque tiene mejores formas de hacer eso
Título: Re: Función Mid de String
Publicado por: $Edu$ en 21 Octubre 2012, 18:49 PM
Fijate en que formato te da la fecha, si es del tipo "06/02/12" solo tienes que usar la funcion Split(), busca sobre ella.
Título: Re: Función Mid de String
Publicado por: Keyen Night en 21 Octubre 2012, 20:27 PM
Mid es una vieja función de vb6, en su lugar en .Net tienes String.SubString, si quieres trabajar con cadenas, pero si el fin es unicamente mostrar en un TextBox día, mes y año, están esas mismas propiedades en Today, Today.Month, Today.Year, Today.Day.

También se puede hacer con expresiones regulares pero ya eso es otro tema más extenso.
Título: Re: Función Mid de String
Publicado por: Lain0x en 21 Octubre 2012, 21:51 PM
Como puede haberme complicado pensando como capturar la fecha, si bastaba con ocupar un par de funciones como today.day. HAHA.