Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Zeroql en 11 Febrero 2010, 15:12 pm



Título: Crear estructuras con limite en variables
Publicado por: Zeroql en 11 Febrero 2010, 15:12 pm
Buenas
bueno resulta que necesito crear una estructura pero necesito darle un tamaño a las variables string como se hacia en vb 6.0
algo asi
Código
  1. public type Datos
  2.   nombre as string *20
  3.   Apellido as string *20
  4. end type
  5.  

He buscado como hacer esto en vb.net y no logro dar con nada

Si alguien sabe porfavor que me diga..
gracias


Título: Re: Crear estructuras con limite en variables
Publicado por: raul338 en 11 Febrero 2010, 15:23 pm
Utiliza la clase StringBuilder del namespace System.Text que su constructor tiene el numero maximo de caracteres que puede aceptar. Y te convendria usar clases en lugar de tipos (tipos en vb6 = structuras en .net)


Título: Re: Crear estructuras con limite en variables
Publicado por: seba123neo en 12 Febrero 2010, 01:34 am
importas
Código
  1. Imports System.Runtime.InteropServices

y despues haces:

Código
  1. Private Structure Datos
  2.        Dim Nombre As String
  3.        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=20)> _
  4.        Dim Apellido As String
  5.       <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=20)> _
  6. End Structure
  7.  

saludos.


Título: Re: Crear estructuras con limite en variables
Publicado por: MANULOMM en 12 Febrero 2010, 03:41 am
Nunca pero NUNCA hagas esto si no es absolutamente necesario.
importas
Código
  1. Imports System.Runtime.InteropServices

y despues haces:

Código
  1. Private Structure Datos
  2.        Dim Nombre As String
  3.        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=20)> _
  4.        Dim Apellido As String
  5.       <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=20)> _
  6. End Structure
  7.  

saludos.

Lo del StringBuilder esta bueno y ademas consume menos memoria.

Atentamente,

Juan Manuel Lombana
Medellín - Colombia


Título: Re: Crear estructuras con limite en variables
Publicado por: raul338 en 12 Febrero 2010, 03:48 am
Jamas vi eso, pero.... no me convence

El StringBuilder es nativo del framework. Y para obtener su valor final es ".ToString()" ;-)

MANULOMM se te olvido cerrar la cita  :silbar:  :xD


Título: Re: Crear estructuras con limite en variables
Publicado por: Zeroql en 13 Febrero 2010, 15:20 pm
Muchachos muchas gracias por la ayuda...
me sirvio mucho lo del StringBuilder


Título: Re: Crear estructuras con limite en variables
Publicado por: MANULOMM en 14 Febrero 2010, 01:15 am
Corregido!!!  :xD


Atentamente,

Juan Manuel Lombana
Medellín - Colombia