How to declare a fixed-length string in VB.NET?

前端 未结 10 2965
醉酒成梦
醉酒成梦 2020-12-10 04:27

How do i Declare a string like this:

Dim strBuff As String * 256

in VB.NET?

10条回答
  •  自闭症患者
    2020-12-10 04:54

    This object can be defined as a structure with one constructor and two properties.

    Public Structure FixedLengthString
         Dim mValue As String
         Dim mSize As Short
    
         Public Sub New(Size As Integer)
             mSize = Size
             mValue = New String(" ", mSize)
         End Sub
    
         Public Property Value As String
             Get
                 Value = mValue
             End Get
    
             Set(value As String)
                 If value.Length < mSize Then
                     mValue = value & New String(" ", mSize - value.Length)
                 Else
                     mValue = value.Substring(0, mSize)
                 End If
             End Set
         End Property
     End Structure
    

    https://jdiazo.wordpress.com/2012/01/12/getting-rid-of-vb6-compatibility-references/

提交回复
热议问题