Come dichiaro una stringa come questa:Come dichiarare una stringa di lunghezza fissa in VB.NET?
Dim strBuff As String * 256
in VB.NET?
Come dichiaro una stringa come questa:Come dichiarare una stringa di lunghezza fissa in VB.NET?
Dim strBuff As String * 256
in VB.NET?
Hai provato
Dim strBuff as String
anche vedere Working with Strings in .NET using VB.NET
Questo tutorial spiega come rappresentare stringhe in .NET utilizzando VB.NET e come lavorare con loro con l'aiuto di Classi di libreria di classi .NET.
utilizzare l'attributo VBFixedString. Vedere le informazioni MSDN here
<VBFixedString(256)>Dim strBuff As String
L'annotazione stessa è grossolanamente insufficiente. L'impostazione di 'strBuff' dall'esempio precedente a una stringa vuota (" ") in VB6 produrrebbe comunque una stringa con 256 spazi. Questo equivalente .NET non lo fa. L'annotazione sembra essere principalmente informativa e nella maggior parte dei casi non applica * molto alla lunghezza delle stringhe (credo che FilePut usi le informazioni). – ChristopheD
Vedi ulteriori discussioni qui: http://stackoverflow.com/questions/16996971/how-to-declare-a-fixed-length-string-in-vb-net-without-increasing-the-length-dur – DaveInCaz
di scrivere questo codice VB 6:
Dim strBuff As String * 256
In VB.Net è possibile usare qualcosa come:
Dim strBuff(256) As Char
Dipende da ciò che si intende usa la stringa per. Se lo si utilizza per l'input e l'output dei file, è possibile utilizzare un array di byte per evitare problemi di codifica. In vb.net, una stringa di 256 caratteri può essere superiore a 256 byte.
Dim strBuff(256) as byte
È possibile utilizzare la codifica di trasferire da byte in una stringa
Dim s As String
Dim b(256) As Byte
Dim enc As New System.Text.UTF8Encoding
...
s = enc.GetString(b)
È possibile assegnare 256 caratteri singolo byte in una stringa se è necessario utilizzare per ricevere i dati, ma il passaggio di parametri potrebbe essere diverso in vb.net rispetto a vb6.
s = New String(" ", 256)
Inoltre, è possibile utilizzare vbFixedString. Non sono sicuro di cosa faccia, tuttavia, perché quando assegni una stringa di lunghezza diversa a una variabile dichiarata in questo modo, diventa la nuova lunghezza.
<VBFixedString(6)> Public s As String
s = "1234567890" ' len(s) is now 10
Dim a as string
a = ...
If a.length > theLength then
a = Mid(a, 1, theLength)
End If
This isn 'una stringa di lunghezza fissa. Cosa impedisce alla stringa di crescere nel resto del codice? –
Usa stringbuilder
'Declaration
Dim S As New System.Text.StringBuilder(256, 256)
'Adding text
S.append("abc")
'Reading text
S.tostring
Prova questo:
Dim strbuf As New String("A", 80)
crea una stringa di 80 caratteri pieni di "AAA ...." 's
Qui mi r ead una stringa di 80 caratteri da un file binario:
FileGet(1,strbuf)
legge 80 caratteri in strbuf ...
È possibile utilizzare Microsoft.VisualBasic.Compatibility
:
Imports Microsoft.VisualBasic.Compatibility
Dim strBuff As New VB6.FixedLengthString(256)
ma è contrassegnato come obsolete and specifically not supported for 64-bit processes, in modo da scrivere il proprio che replica la funzionalità, che è quello di troncare i sull'impostazione dei valori di lunghezza e imbottitura destra con spazi per i valori brevi. Imposta anche un valore "non inizializzato", come sopra, su null.
codice di esempio da LINQPad (che non posso ottenere per consentire Imports Microsoft.VisualBasic.Compatibility
penso perché è contrassegnato obsoleto, ma non ho alcuna prova di questo):
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()
che ha questa uscita:
"\ 0 \ 0 \ 0 \ 0 \ 0"
"test"
"Testi"
"test"
"Testi"
Questo non è stato testato, ma qui è una classe per risolvere questo problema:
''' <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
Tale scopo può essere definito come una struttura con un costruttore e due proprietà.
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/
Ma c'è is'nt una risposta ci – Rachel
, naturalmente, ma questo non è quello che mi serve. ho bisogno che lui lo dichiari come è stato dichiarato in vb.6 – Rachel