Variable de longitud fija

(1/1)

luis456:
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
Dim Name As String * 30  



pero ya no vale para net

tengo este codigo para hacerlo de longitud fija

Código
 Dim Results As IEnumerable(Of Integer) =
         (
             From Value As Integer
               In (Result).Distinct Where (Value <= MAX))
 
 
       ListBox6.Items.AddRange(Results.Cast(Of Object).ToArray)


Luis





seba123neo:
en .NET el equivalente es:

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

aca lo explican:

Preparing Your Visual Basic 6.0 Applications for the Upgrade to Visual Basic .NET

igualmente no lo recomiendan.

luis456:
 gracias seba123neo

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

luis

Eleкtro:
Si lo que quieres es limitar la cantidad de caracteres de un String, puedes utilizar el buffer de un StringBuilder:
Código
Dim sb As New StringBuilder(1, 5) ' máximo 5 caracteres.
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
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
Dim fixedStr As FixedLengthString
 
fixedStr = New FixedLengthString("", 10)
MessageBox.Show("""" & fixedStr.ValueFixed & """") ' Result: "          "
 
fixedStr.ValueUnfixed = "12345"
MessageBox.Show("""" & fixedStr.ValueFixed & """") ' Result: "1245      "
 
fixedStr.ValueUnfixed = "1234567890abc"
MessageBox.Show("""" & fixedStr.ValueFixed & """") ' Result: "1234567890"

Source:
Código
' ***********************************************************************
' Author   : Elektro
' Modified : 04-February-2015
' ***********************************************************************
' <copyright file="FixedLengthString.vb" company="Elektro Studios">
'     Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************
 
''' <summary>
''' Defines a <see cref="String"/> with a fixed length.
''' </summary>
Public NotInheritable Class FixedLengthString
 
#Region " Properties "
 
   ''' <summary>
   ''' Gets or sets the fixed string length.
   ''' </summary>
   ''' <value>The fixed string length.</value>
   Public Property FixedLength As Integer
       Get
           Return Me.fixedLength1
       End Get
       Set(ByVal value As Integer)
           Me.fixedLength1 = value
       End Set
   End Property
   ''' <summary>
   ''' The fixed string length.
   ''' </summary>
   Private fixedLength1 As Integer
 
   ''' <summary>
   ''' Gets or sets the padding character thath fills the string.
   ''' </summary>
   ''' <value>The padding character thath fills the string.</value>
   Public Property PaddingChar As Char
       Get
           Return Me.paddingChar1
       End Get
       Set(ByVal value As Char)
           Me.paddingChar1 = value
       End Set
   End Property
   ''' <summary>
   ''' The padding character thath fills the string.
   ''' </summary>
   Private paddingChar1 As Char
 
   ''' <summary>
   ''' Gets or sets the unmodified string.
   ''' </summary>
   ''' <value>The unmodified string.</value>
   Public Property ValueUnfixed As String
       Get
           Return Me.valueUnfixed1
       End Get
       Set(ByVal value As String)
           Me.valueUnfixed1 = value
       End Set
   End Property
   ''' <summary>
   ''' The unmodified string.
   ''' </summary>
   Private valueUnfixed1 As String
 
   ''' <summary>
   ''' Gets the fixed string.
   ''' </summary>
   ''' <value>The fixed string.</value>
   Public ReadOnly Property ValueFixed As String
       Get
           Return Me.FixLength(Me.valueUnfixed1, Me.fixedLength1, Me.paddingChar1)
       End Get
   End Property
 
#End Region
 
#Region " Constructors "
 
   ''' <summary>
   ''' Prevents a default instance of the <see cref="FixedLengthString" /> class from being created.
   ''' </summary>
   Private Sub New()
   End Sub
 
   ''' <summary>
   ''' Initializes a new instance of the <see cref="FixedLengthString" /> class.
   ''' </summary>
   ''' <param name="value">The string value.</param>
   ''' <param name="fixedLength">The fixed string length.</param>
   ''' <param name="paddingChar">The padding character thath fills the string.</param>
   Public Sub New(ByVal value As String,
                  ByVal fixedLength As Integer,
                  Optional ByVal paddingChar As Char = " "c)
 
       Me.valueUnfixed1 = value
       Me.fixedLength1 = fixedLength
       Me.paddingChar1 = paddingChar
 
   End Sub
 
#End Region
 
#Region " Public Methods "
 
   ''' <summary>
   ''' Returns a <see cref="System.String" /> that represents this instance.
   ''' </summary>
   ''' <returns>A <see cref="System.String" /> that represents this instance.</returns>
   Public Overrides Function ToString() As String
       Return Me.ValueFixed
   End Function
 
#End Region
 
#Region " Private Methods "
 
   ''' <summary>
   ''' Fixes the length of the specified string.
   ''' </summary>
   ''' <param name="value">The string value.</param>
   ''' <param name="fixedLength">The fixed string length.</param>
   ''' <param name="paddingChar">The padding character thath fills the string.</param>
   ''' <returns>System.String.</returns>
   Private Function FixLength(ByVal value As String,
                              ByVal fixedLength As Integer,
                              ByVal paddingChar As Char) As String
 
       If (value.Length > fixedLength) Then
           Return value.Substring(0, fixedLength)
       Else
           Return value.PadRight(fixedLength, paddingChar)
       End If
 
   End Function
 
#End Region
 
End Class

Navegación

[0] Índice de Mensajes