我在代码示例中损坏了字符串吗? [英] Have I corrupted the string in the code example?
问题描述
大家好,
首先我在此示例中损坏了字符串值:
Hi All,
First off have I corrupted a string value in this example:
Dim CommandB As String = ""
'do stuff not related
' a lot of stuff not related
Do Until RecString(ICount) = "EOF"
ICount += 1
If RecString(ICount) = "EOF" Then GoTo EndofCommandLoop 'Yes that's a Goto No I'm not responcible!
CommandB(BCount) = RecString(ICount)
BCount += 1
Loop
EndofCommandLoop:
'Take actions
啊,我吐在布偶上涂鸦(不写)这个烂摊子
VB里面无限智慧决定不编译这个,它返回
Ah, I spit on the muppet that doodled (not wrote) this mess
VB in it infinite wisdom has decided not to compile this, it returns
错误1属性'Chars'是'ReadOnly'。
Error 1 Property 'Chars' is 'ReadOnly'.
带有蓝色摇摆线,但是它编译了这个版本
with a blue wiggly line, however it compiles this version
Dim CommandB As String
'do stuff not related
' a lot of stuff not related
Do Until RecString(ICount) = "EOF"
ICount += 1
If RecString(ICount) = "EOF" Then GoTo EndofCommandLoop
CommandB(BCount) = RecString(ICount)
BCount += 1
Loop
EndofCommandLoop:
'Take actions
使用CommandB(BCount)下的绿色小摆线作为以下内容
Works with a little green wiggly line under CommandB(BCount) as the following
警告BC42104:变量'CommandB'在被赋值之前使用。在运行时可能会产生空引用异常。
warning BC42104: Variable 'CommandB' is used before it has been assigned a value. A null reference exception could result at runtime.
为什么,任何人都可以帮忙!
Why, can anyone help!
推荐答案
像这样使用StringBuilder类:
Use the StringBuilder class like this:
Dim sb As New System.Text.StringBuilder
'do stuff not related
' a lot of stuff not related
Do Until RecString(ICount) = "EOF"
ICount += 1
If RecString(ICount) = "EOF" Then GoTo EndofCommandLoop
sb.Append(RecString(ICount))
BCount += 1
Loop
EndofCommandLoop:
'Take actions
Dim CommandB As String = sb.ToString
好的,你可以通过这样做来修复第二个版本:
Okay, you can fix the second version by doing:
Dim CommandB As String = String.Empty
但是,看起来你只想在那里增加一个字符串,所以尝试将其更改为:
However, it looks like you are just tring to increment a string there so try changing it to this:
Dim CommandB As StringBuilder = New StringBuilder()
Do Until RectString(ICount) = EofCharacter
ICount += 1
If RecString(ICount) = EofCharacter Break
CommandB.Append(RecString(ICount))
在您的示例中,您将字符与字符串进行比较EOF是一个字符串,因此您需要将EOF标记转换为字符。
In your example, you are comparing a character to a string "EOF" is a string, so you need to convert the EOF marker into a character.
这看起来像EOF字符上的一些变换形式的增量字符串拆分。
如果我错了,请忽略其余部分。
This looks like some perverted form of incremental string splitting on a EOF character.
If I am wrong please ignore the rest of this.
'*** assuming these are defined outside of the splitting method
Const EOF As Char = Chr(26)
Dim RecString As String = "sequence 1" & EOF & "sequence 2" & EOF & "sequence 3" & EOF
Dim RecPosition As Int32 = 0 ' This was your ICount variable
'***
' the grunt work of find EOF marker and grabbing a substring
Dim EOFposition As Int32
Dim CommandB As String
If RecPosition < RecString.Length Then
EOFposition = RecString.IndexOf(value:=EOF, startIndex:=RecPosition)
If EOFposition <> -1 Then
If RecPosition = EOFposition Then
CommandB = String.Empty
Else
CommandB = RecString.Substring(startIndex:=RecPosition, length:=(EOFposition - RecPosition))
End If
Else
CommandB = RecString.Substring(startIndex:=RecPosition)
End If
RecPosition += CommandB.Length + 1 ' start of next sequence
End If
这篇关于我在代码示例中损坏了字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!