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

前端 未结 10 2850
醉酒成梦
醉酒成梦 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:59

    This hasn't been fully tested, but here's a class to solve this problem:

    ''' 
    ''' Represents a  with a minimum
    ''' and maximum length.
    ''' 
    Public Class BoundedString
    
        Private mstrValue As String
    
        ''' 
        ''' The contents of this 
        ''' 
        Public Property Value() As String
            Get
                Return mstrValue
            End Get
    
            Set(value As String)
                If value.Length < MinLength Then
                    Throw New ArgumentException(String.Format("Provided string {0} of length {1} contains less " &
                                                              "characters than the minimum allowed length {2}.",
                                                              value, value.Length, MinLength))
                End If
    
                If value.Length > MaxLength Then
                    Throw New ArgumentException(String.Format("Provided string {0} of length {1} contains more " &
                                                              "characters than the maximum allowed length {2}.",
                                                              value, value.Length, MaxLength))
                End If
    
                If Not AllowNull AndAlso value Is Nothing Then
                    Throw New ArgumentNullException(String.Format("Provided string {0} is null, and null values " &
                                                                  "are not allowed.", value))
                End If
    
                mstrValue = value
            End Set
        End Property
    
        Private mintMinLength As Integer
        ''' 
        ''' The minimum number of characters in this .
        ''' 
        Public Property MinLength() As Integer
            Get
                Return mintMinLength
            End Get
    
            Private Set(value As Integer)
                mintMinLength = value
            End Set
    
        End Property
    
        Private mintMaxLength As Integer
        ''' 
        ''' The maximum number of characters in this .
        ''' 
        Public Property MaxLength As Integer
            Get
                Return mintMaxLength
            End Get
    
            Private Set(value As Integer)
                mintMaxLength = value
            End Set
        End Property
    
        Private mblnAllowNull As Boolean
        ''' 
        ''' Whether or not this  can represent a null value.
        ''' 
        Public Property AllowNull As Boolean
            Get
                Return mblnAllowNull
            End Get
    
            Private Set(value As Boolean)
                mblnAllowNull = value
            End Set
        End Property
    
        Public Sub New(ByVal strValue As String,
                       ByVal intMaxLength As Integer)
            MinLength = 0
            MaxLength = intMaxLength
            AllowNull = False
    
            Value = strValue
        End Sub
    
        Public Sub New(ByVal strValue As String,
                       ByVal intMinLength As Integer,
                       ByVal intMaxLength As Integer)
            MinLength = intMinLength
            MaxLength = intMaxLength
            AllowNull = False
    
            Value = strValue
        End Sub
    
        Public Sub New(ByVal strValue As String,
                       ByVal intMinLength As Integer,
                       ByVal intMaxLength As Integer,
                       ByVal blnAllowNull As Boolean)
            MinLength = intMinLength
            MaxLength = intMaxLength
            AllowNull = blnAllowNull
    
            Value = strValue
        End Sub
    End Class
    

提交回复
热议问题