.NET Framework Bookmark and Share   
 index > .NET Base Class Library > A weird problem with string variables
 

A weird problem with string variables

In my project I need to to simple string operations like a & b, replace and instr

the problem I came up with is for some weird reason it refuses to merge two string variables!

example:

dim A as string="<data>"
dim V as string="some text"
dim B as string="</data>"

dim Result as string=A & V & B

seems simple?

but at the end Result= "<data>sometext"

"</data>" not there

I tried to use + instead of &

even tried to put these strings into an array and then join it. nothing helps, the last TAG always not in its place.

In other projects the same code works well, but in one that I need it to use it with it doesn't...

I have no idea what can affect the code in such weird way.

If anyone met with such or similar prob please advice what can be done.

thanks
Xfolder

It reminds me of something I have seen several times before. When you get the string out of the array of bytes, the trailing binary zeroes make it to that string, acting as a imbedded end-of string. When you them append to that string, this end-of-string is still there, so that the appended one is not visible.

Try to do the GetString on a subarray of only bytesRecieved characters.

Not sure this will help, but it's worth trying.

Libor

Libor V
Use the other overload of ASCIIEncoding.GetString():

Dim dataReceived As String = System.Text.Encoding.ASCII.GetString(received, 0, bytesReceived-1)

Note that the string actually got appended, you just can't see it in the debugger because it is not able to display past an embedded zero. One thing you'll notice is that the end double-quote is missing.
nobugz
Yup, that's mighty weird. Unsurprisingly, that problem doesn't reproduce when I try it. There is actually an explanation for it. Search all the source files of your project for the "declare" keyword or DllImport attribute. If you find one, use StringBuilder instead of String. If you don't, I have no clue what is going on.
nobugz
right, I've localized the problem

I use the following code to get string data out of socket connection

Dim received(512) As Byte
Dim bytesReceived As Integer = MyWsk.Receive(received, received.Length, 0)
Dim dataReceived As String = System.Text.Encoding.ASCII.GetString(received)

and when I use this dataReceived string something weird going on BUT when I do

Dim dataReceived As String = "some data"

fixed string value it works!

Am I doing something wrong when I read the data out of socket?
Xfolder
This code doesn't really qualify. It doesn't violate the immutability of a string. One possibility is that MyWsk.Receive() simply overruns its buffer, destroying data in the heap. That's only likely if MyWsk is an instance of an ActiveX component instead of Windows.Net.Socket, I can't tell from your code. Forgetting to account for the zero terminator is pretty classic, try passing received.Length-1.
nobugz

It reminds me of something I have seen several times before. When you get the string out of the array of bytes, the trailing binary zeroes make it to that string, acting as a imbedded end-of string. When you them append to that string, this end-of-string is still there, so that the appended one is not visible.

Try to do the GetString on a subarray of only bytesRecieved characters.

Not sure this will help, but it's worth trying.

Libor

Libor V
yes, that was the problem, found out it too Smile

is there a nicer way to avoid use of sub arrays? I see no way how to create byte array to the exact size of passed bytes.

Dim received(512) As Byte
Dim bytesReceived As Integer = MyWsk.Receive(received, received.Length, 0)

Dim XD As New List(Of Byte)

For Each I As Byte In received
If I > 0 Then XD.Add(I)
Next

Dim A() As Byte = XD.ToArray

Dim dataReceived As String = System.Text.Encoding.ASCII.GetString(A)




Xfolder
Use the other overload of ASCIIEncoding.GetString():

Dim dataReceived As String = System.Text.Encoding.ASCII.GetString(received, 0, bytesReceived-1)

Note that the string actually got appended, you just can't see it in the debugger because it is not able to display past an embedded zero. One thing you'll notice is that the end double-quote is missing.
nobugz
thank you Smile
Xfolder

Hi . That problem doesn't reproduce when I try it. But try to use following code & it may work will : -

Code Snippet

Dim Result As String = Convert.ToString(A + V + B)

Or Use StringBuilder Instead of String type .

Pr.Wael

You can use google to search for other answers

Custom Search

More Threads

• Clarification on TraceSource/Trace
• Serial Number using WMI under .NET 2.0
• class library
• Reflect private field of System.Web.UI.Page
• Thread safe event design (to avoid deadlocks)
• Menu Items and ControlBox Buttons/Items all gone...
• RSH Failure when using System.Diagnostics.Process
• Can anybody explain the reasoning behind this 260 chars limitation?
• How Apply *.theme files to Program Visual Style By Programming
• How to get the calling Assemblies