Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

First off have I corrupted a string value in this example:

VB
 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

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
Quote:
Error 1 Property 'Chars' is 'ReadOnly'.
with a blue wiggly line, however it compiles this version

VB
 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

Works with a little green wiggly line under CommandB(BCount) as the following
Quote:
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!
Posted

Use the StringBuilder class like this:
VB
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
 
Share this answer
 
Comments
glennPattonWork3 31-Jan-14 6:28am    
I wanted to use StringBuilder (I wanted to rewrite it in C# but wasn't allowed) so I might do that... or a mix or your & Pete's answer.
Okay, you can fix the second version by doing:
VB
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:
VB
Dim CommandB As StringBuilder = New StringBuilder()
Do Until RectString(ICount) = EofCharacter
  ICount += 1
  If RecString(ICount) = EofCharacter Break
  CommandB.Append(RecString(ICount))
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.
 
Share this answer
 
Comments
glennPattonWork3 31-Jan-14 6:26am    
Thanks, I will have a go at that
This looks like some perverted form of incremental string splitting on a EOF character.
If I am wrong please ignore the rest of this.
VB
'*** 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
 
Share this answer
 
The reason why it says "Chars is ReadOnly" is because, in .NET, once created, a String object is immutable. You cannot change it the contents of the string.

In your case, the StringBuilder suggestions would be the better way to go.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900