如何在VB.NET中声明固定长度字符串?

如何在VB.NET中声明固定长度字符串?,vb.net,vb6-migration,Vb.net,Vb6 Migration,如何声明这样的字符串: Dim strBuff As String * 256 在VB.NET中?您尝试过吗 Dim strBuff as String 也看到 本教程介绍如何 使用VB.NET在.NET中表示字符串 以及如何与他们一起工作 NET类库类的帮助 使用VBFixedString属性。请参阅MSDN信息 Dim strBuff作为字符串 要编写此VB 6代码: Dim strBuff As String * 256 在VB.Net中,您可以使用以下内容: Dim strBuff

如何声明这样的字符串:

Dim strBuff As String * 256
在VB.NET中?

您尝试过吗

Dim strBuff as String
也看到

本教程介绍如何 使用VB.NET在.NET中表示字符串 以及如何与他们一起工作 NET类库类的帮助


使用VBFixedString属性。请参阅MSDN信息

Dim strBuff作为字符串

要编写此VB 6代码:

Dim strBuff As String * 256
在VB.Net中,您可以使用以下内容:

Dim strBuff(256) As Char

这取决于您打算将字符串用于什么。如果将其用于文件输入和输出,则可能需要使用字节数组来避免编码问题。在vb.net中,256个字符的字符串可能超过256个字节

Dim strBuff(256) as byte
可以使用编码将字节转换为字符串

Dim s As String
Dim b(256) As Byte
Dim enc As New System.Text.UTF8Encoding
...
s = enc.GetString(b)
如果需要使用字符串接收数据,可以将256个单字节字符分配给字符串,但在vb.net中传递的参数可能与vb6中的不同

s = New String(" ", 256)
此外,还可以使用vbFixedString。但是,我不确定这到底是做什么的,因为当您将不同长度的字符串指定给以这种方式声明的变量时,它将成为新的长度

<VBFixedString(6)> Public s As String
s = "1234567890" ' len(s) is now 10
Public s作为字符串
s=“1234567890”len现在是10

使用stringbuilder

'Declaration   
Dim S As New System.Text.StringBuilder(256, 256)
'Adding text
S.append("abc")
'Reading text
S.tostring
试试这个:

    Dim strbuf As New String("A", 80)
创建一个80个字符的字符串,其中填充“AAA…”

在这里,我从二进制文件中读取了一个80个字符的字符串:

    FileGet(1,strbuf)

将80个字符读入strbuf…

您可以使用
Microsoft.VisualBasic.Compatibility

Imports Microsoft.VisualBasic.Compatibility

Dim strBuff As New VB6.FixedLengthString(256)
但是它被标记为,所以请编写您自己的代码来复制该功能,即在设置长值时截断,并用空格填充短值。它还将“未初始化”值(如上所述)设置为null

LinqPad中的示例代码(我不能允许
导入Microsoft.VisualBasic.Compatibility
,我认为这是因为它被标记为过时,但我没有证据):

其中有以下输出:

“\0\0\0\0\0\0”
“测试”
“睾丸”
“测试”
“睾丸”


这尚未经过全面测试,但有一个类可以解决此问题:

''' <summary>
''' Represents a <see cref="String" /> with a minimum
''' and maximum length.
''' </summary>
Public Class BoundedString

    Private mstrValue As String

    ''' <summary>
    ''' The contents of this <see cref="BoundedString" />
    ''' </summary>
    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
    ''' <summary>
    ''' The minimum number of characters in this <see cref="BoundedString" />.
    ''' </summary>
    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
    ''' <summary>
    ''' The maximum number of characters in this <see cref="BoundedString" />.
    ''' </summary>
    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
    ''' <summary>
    ''' Whether or not this <see cref="BoundedString" /> can represent a null value.
    ''' </summary>
    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
“”
''表示具有最小值的
''和最大长度。
''' 
公共类BoundedString
私有mstrValue作为字符串
''' 
“这本书的内容是什么
''' 
作为字符串的公共属性值()
得到
返回mstrValue
结束
设置(值为字符串)
如果value.LengthMaxLength,则
抛出新ArgumentException(String.Format(“提供的长度为{1}的字符串{0}包含更多内容”)&
“超过允许的最大长度{2}的字符。”,
值,值。长度,最大长度)
如果结束
如果不AllowFull和Also值为Nothing,则
抛出新ArgumentNullException(String.Format)(“提供的字符串{0}为null,且值为null”&
“不允许。”,值)
如果结束
mstrValue=value
端集
端属性
私有最小长度为整数
''' 
''此字段中的最小字符数。
''' 
公共属性MinLength()为整数
得到
返回最小长度
结束
私有集(值为整数)
mintMinLength=值
端集
端属性
私有mintMaxLength为整数
''' 
''此文件中的最大字符数。
''' 
公共属性MaxLength为整数
得到
返回mintMaxLength
结束
私有集(值为整数)
mintMaxLength=值
端集
端属性
私有mblnAllowNull为布尔值
''' 
''这是否可以表示空值。
''' 
公共属性AllowFull为布尔值
得到
返回mblnAllowNull
结束
私有集(值为布尔值)
mblnAllowNull=值
端集
端属性
Public Sub New(ByVal strValue作为字符串,
ByVal intMaxLength(整数形式)
最小长度=0
MaxLength=intMaxLength
AllowNull=False
值=标准值
端接头
Public Sub New(ByVal strValue作为字符串,
ByVal intMinLength为整数,
ByVal intMaxLength(整数形式)
MinLength=intMinLength
MaxLength=intMaxLength
AllowNull=False
值=标准值
端接头
Public Sub New(ByVal strValue作为字符串,
ByVal intMinLength为整数,
ByVal intMaxLength为整数,
ByVal blnAllowNull(作为布尔值)
MinLength=intMinLength
MaxLength=intMaxLength
AllowNull=blnalownull
值=标准值
端接头
末级

此对象可以定义为具有一个构造函数和两个属性的结构

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
公共结构固定长度字符串
作为字符串的Dim mValue
变短
公共子新建(大小为整数)
mSize=大小
mValue=新字符串(“,mSize)
端接头
作为字符串的公共属性值
得到
值=mValue
结束
设置(值为字符串)
如果value.Length

但是那里没有答案
Imports Microsoft.VisualBasic.Compatibility

Dim U As New VB6.FixedLengthString(5)
Dim S As New VB6.FixedLengthString(5, "Test")
Dim L As New VB6.FixedLengthString(5, "Testing")
Dim p0 As Func(Of String, String) = Function(st) """" & st.Replace(ChrW(0), "\0") & """"
p0(U.Value).Dump()
p0(S.Value).Dump()
p0(L.Value).Dump()
U.Value = "Test"
p0(U.Value).Dump()
U.Value = "Testing"
p0(U.Value).Dump()
''' <summary>
''' Represents a <see cref="String" /> with a minimum
''' and maximum length.
''' </summary>
Public Class BoundedString

    Private mstrValue As String

    ''' <summary>
    ''' The contents of this <see cref="BoundedString" />
    ''' </summary>
    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
    ''' <summary>
    ''' The minimum number of characters in this <see cref="BoundedString" />.
    ''' </summary>
    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
    ''' <summary>
    ''' The maximum number of characters in this <see cref="BoundedString" />.
    ''' </summary>
    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
    ''' <summary>
    ''' Whether or not this <see cref="BoundedString" /> can represent a null value.
    ''' </summary>
    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
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