How do i Declare a string like this:
Dim strBuff As String * 256
in VB.NET?
This hasn't been fully tested, but here's a class to solve this problem:
''' <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
Use the VBFixedString attribute. See the MSDN info here
<VBFixedString(256)>Dim strBuff As String
Have you tried
Dim strBuff as String
Also see Working with Strings in .NET using VB.NET
This tutorial explains how to represent strings in .NET using VB.NET and how to work with them with the help of .NET class library classes.
You can use Microsoft.VisualBasic.Compatibility
:
Imports Microsoft.VisualBasic.Compatibility
Dim strBuff As New VB6.FixedLengthString(256)
But it's marked as obsolete and specifically not supported for 64-bit processes, so write your own that replicates the functionality, which is to truncate on setting long values and padding right with spaces for short values. It also sets an "uninitialised" value, like above, to nulls.
Sample code from LinqPad (which I can't get to allow Imports Microsoft.VisualBasic.Compatibility
I think because it is marked obsolete, but I have no proof of that):
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()
which has this output:
"\0\0\0\0\0"
"Test "
"Testi"
"Test "
"Testi"