How declare a fix length field?

danyeungw

Well-known member
Joined
Aug 30, 2005
Messages
73
Programming Experience
10+
For example, dim var1 as string, to create a variable. How to make the variable a fix length?

Thanks.
DanYeung
 
Fixed length strings no longer exist in .NET.... a string is a string is a string regardless of length.

-tg
 
If you just want to limit the length of a text file you could do something like:

VB.NET:
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object,  _
ByVal e As System.EventArgs) Handles [/SIZE][SIZE=1]TextBox1[/SIZE][SIZE=1].TextChanged
        Dim Strvar As String = [/SIZE][SIZE=1]TextBox1[/SIZE][SIZE=1].Text
        Dim StringLength As Integer = 10
        Dim StrCharArray() As Char = Strvar.ToCharArray()
        Dim a As Integer
        If Strvar.Length > 10 Then
            MsgBox("String longer than 10")
            Strvar = ""
            For a = 0 To StringLength - 1
                Strvar &= StrCharArray(a)
            Next
            [/SIZE][SIZE=1]TextBox1[/SIZE][SIZE=1].Text = Strvar


        End If
    End Sub
 
Even easier than that is to se the MaxLength property of the text box.... but the question was bout strings. so that doesn't help much.

-tg
 
you could always make your own class consisting of 1 variable with 1 property and everytime a new value is stored check it's length and handle padding/trimming there

it'd still be a string but with it being inside a class you'd use it as a fixed length string
 
don't forget to truncate it if it reaches that length.....

-tg
 
Back
Top