Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: luis456 en 4 Febrero 2015, 16:51 pm



Título: Variable de longitud fija
Publicado por: luis456 en 4 Febrero 2015, 16:51 pm
En una variable de este tipo como puedo forzar que tenga solo la cantidad de numeros que yo quiera,

 era antes mas o menos

Código
  1. Dim Name As String * 30  



pero ya no vale para net

tengo este codigo para hacerlo de longitud fija

Código
  1.  Dim Results As IEnumerable(Of Integer) =
  2.          (
  3.              From Value As Integer
  4.                In (Result).Distinct Where (Value <= MAX))
  5.  
  6.  
  7.        ListBox6.Items.AddRange(Results.Cast(Of Object).ToArray)


Luis







Título: Re: Variable de longitud fija
Publicado por: seba123neo en 4 Febrero 2015, 19:25 pm
en .NET el equivalente es:

Código
  1. <VBFixedString(30)> Dim vVariable As String
  2.  
  3.    Dim MyFixedLengthString As New VB6.FixedLengthString(30)

aca lo explican:

Preparing Your Visual Basic 6.0 Applications for the Upgrade to Visual Basic .NET (https://msdn.microsoft.com/en-us/library/aa260644%28VS.60%29.aspx#vb6tovbdotnet_arrays)

igualmente no lo recomiendan.



Título: Re: Variable de longitud fija
Publicado por: luis456 en 4 Febrero 2015, 19:45 pm
 gracias seba123neo

Lo mirare y si no me vale tratare en la funcion que me restrinja las cantidades :)

luis


Título: Re: Variable de longitud fija
Publicado por: Eleкtro en 4 Febrero 2015, 21:33 pm
Si lo que quieres es limitar la cantidad de caracteres de un String, puedes utilizar el buffer de un StringBuilder:
Código
  1. Dim sb As New StringBuilder(1, 5) ' máximo 5 caracteres.
  2. sb.Append("123456") ' 6 caracteres, dará error por sobrepasar el límite.

Si lo que quieres es un String de longitud variable pero que en principio contenga un número específico de caracteres, puedes usar el constructor del datatype String:
Código
  1. Dim str As New String(" "c, 10)

Si lo que quieres es crear un string de longitud fija, siempre puedes crear tu propia extensión de método, o type:

Modo de empleo:
Código
  1. Dim fixedStr As FixedLengthString
  2.  
  3. fixedStr = New FixedLengthString("", 10)
  4. MessageBox.Show("""" & fixedStr.ValueFixed & """") ' Result: "          "
  5.  
  6. fixedStr.ValueUnfixed = "12345"
  7. MessageBox.Show("""" & fixedStr.ValueFixed & """") ' Result: "1245      "
  8.  
  9. fixedStr.ValueUnfixed = "1234567890abc"
  10. MessageBox.Show("""" & fixedStr.ValueFixed & """") ' Result: "1234567890"

Source:
Código
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 04-February-2015
  4. ' ***********************************************************************
  5. ' <copyright file="FixedLengthString.vb" company="Elektro Studios">
  6. '     Copyright (c) Elektro Studios. All rights reserved.
  7. ' </copyright>
  8. ' ***********************************************************************
  9.  
  10. ''' <summary>
  11. ''' Defines a <see cref="String"/> with a fixed length.
  12. ''' </summary>
  13. Public NotInheritable Class FixedLengthString
  14.  
  15. #Region " Properties "
  16.  
  17.    ''' <summary>
  18.    ''' Gets or sets the fixed string length.
  19.    ''' </summary>
  20.    ''' <value>The fixed string length.</value>
  21.    Public Property FixedLength As Integer
  22.        Get
  23.            Return Me.fixedLength1
  24.        End Get
  25.        Set(ByVal value As Integer)
  26.            Me.fixedLength1 = value
  27.        End Set
  28.    End Property
  29.    ''' <summary>
  30.    ''' The fixed string length.
  31.    ''' </summary>
  32.    Private fixedLength1 As Integer
  33.  
  34.    ''' <summary>
  35.    ''' Gets or sets the padding character thath fills the string.
  36.    ''' </summary>
  37.    ''' <value>The padding character thath fills the string.</value>
  38.    Public Property PaddingChar As Char
  39.        Get
  40.            Return Me.paddingChar1
  41.        End Get
  42.        Set(ByVal value As Char)
  43.            Me.paddingChar1 = value
  44.        End Set
  45.    End Property
  46.    ''' <summary>
  47.    ''' The padding character thath fills the string.
  48.    ''' </summary>
  49.    Private paddingChar1 As Char
  50.  
  51.    ''' <summary>
  52.    ''' Gets or sets the unmodified string.
  53.    ''' </summary>
  54.    ''' <value>The unmodified string.</value>
  55.    Public Property ValueUnfixed As String
  56.        Get
  57.            Return Me.valueUnfixed1
  58.        End Get
  59.        Set(ByVal value As String)
  60.            Me.valueUnfixed1 = value
  61.        End Set
  62.    End Property
  63.    ''' <summary>
  64.    ''' The unmodified string.
  65.    ''' </summary>
  66.    Private valueUnfixed1 As String
  67.  
  68.    ''' <summary>
  69.    ''' Gets the fixed string.
  70.    ''' </summary>
  71.    ''' <value>The fixed string.</value>
  72.    Public ReadOnly Property ValueFixed As String
  73.        Get
  74.            Return Me.FixLength(Me.valueUnfixed1, Me.fixedLength1, Me.paddingChar1)
  75.        End Get
  76.    End Property
  77.  
  78. #End Region
  79.  
  80. #Region " Constructors "
  81.  
  82.    ''' <summary>
  83.    ''' Prevents a default instance of the <see cref="FixedLengthString" /> class from being created.
  84.    ''' </summary>
  85.    Private Sub New()
  86.    End Sub
  87.  
  88.    ''' <summary>
  89.    ''' Initializes a new instance of the <see cref="FixedLengthString" /> class.
  90.    ''' </summary>
  91.    ''' <param name="value">The string value.</param>
  92.    ''' <param name="fixedLength">The fixed string length.</param>
  93.    ''' <param name="paddingChar">The padding character thath fills the string.</param>
  94.    Public Sub New(ByVal value As String,
  95.                   ByVal fixedLength As Integer,
  96.                   Optional ByVal paddingChar As Char = " "c)
  97.  
  98.        Me.valueUnfixed1 = value
  99.        Me.fixedLength1 = fixedLength
  100.        Me.paddingChar1 = paddingChar
  101.  
  102.    End Sub
  103.  
  104. #End Region
  105.  
  106. #Region " Public Methods "
  107.  
  108.    ''' <summary>
  109.    ''' Returns a <see cref="System.String" /> that represents this instance.
  110.    ''' </summary>
  111.    ''' <returns>A <see cref="System.String" /> that represents this instance.</returns>
  112.    Public Overrides Function ToString() As String
  113.        Return Me.ValueFixed
  114.    End Function
  115.  
  116. #End Region
  117.  
  118. #Region " Private Methods "
  119.  
  120.    ''' <summary>
  121.    ''' Fixes the length of the specified string.
  122.    ''' </summary>
  123.    ''' <param name="value">The string value.</param>
  124.    ''' <param name="fixedLength">The fixed string length.</param>
  125.    ''' <param name="paddingChar">The padding character thath fills the string.</param>
  126.    ''' <returns>System.String.</returns>
  127.    Private Function FixLength(ByVal value As String,
  128.                               ByVal fixedLength As Integer,
  129.                               ByVal paddingChar As Char) As String
  130.  
  131.        If (value.Length > fixedLength) Then
  132.            Return value.Substring(0, fixedLength)
  133.        Else
  134.            Return value.PadRight(fixedLength, paddingChar)
  135.        End If
  136.  
  137.    End Function
  138.  
  139. #End Region
  140.  
  141. End Class