Limit String size defination

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

I have a structure that contains an array of type string.
public structure MyStruc
dim MyString as string
end structure

in vb 6 I would have done something like this
public type MyStruc
Dim MyString as string *6
end type.

how can i limit the MyString to just 6 characters.
 
I'll need to limit the size of the string as it will be filled by an in
coming telegram data
 
Brian,

As Patrice says, there is no support for fixed-length strings.

However, you may be able to use this construct:

Dim MyString(5) As Char

Give it a try and see if this meets your needs.

Dick

--
Richard Grier, Consultant, Hard & Software 12962 West Louisiana Avenue
Lakewood, CO 80228 303-986-2179 (voice) Homepage: www.hardandsoftware.net
Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004, Revised July
2006.
 
augh.. forgot i did this a couple of years ago... <vbfixedstring(40)>Dim
MyString as string
 
I'll need to limit the size of the string as it will be filled by an in
coming telegram data

Just testing the length and truncating the string if needed is not enough ?
 
Brian said:
in vb 6 I would have done something like this
public type MyStruc
Dim MyString as string *6
end type.

how can i limit the MyString to just 6 characters.

Unless you have a really, /really/ Good Reason to keep this /as/ a
Structure, I would recommend ditching the Structure, using a full-blown
Class and a Property to manage the restrictions on this field.

Public Class Struc1

Public Sub New( byval incomingData as TelegramData )
. . .
Me.String1 = SomeFunction( incomingData )
. . .
End Sub

Public Property String1() as String
Get
Return m_string1
End Get
Set( value as String )
If ( value.Length > 6 ) Then
value = value.Subtring( 0, 6 )
End If

m_string1 = value
End Set
End Property

Private m_string1 as string = Nothing

End Class

HTH,
Phill W.
 
Back
Top