Nothing as a char of a string

  • Thread starter Thread starter tommaso.gastaldi
  • Start date Start date
T

tommaso.gastaldi

I am extracting some field names from a table of a db (it's an
OledbSchemaguid table).

It occurs for some OleDbSchema driver that

NameDBField = cstr(DataRow.Item(3))

return a string that is composed by the characters of the field name,
for instance MYFIELD plus 1 Nothing. I know that the Nothing is there
because, since I was having a lot of problem with these strange strings
I have scanned them char by char.

This happens for any string extracted from any of the oledbschemaguid
tables using a certain OleDbSchema driver: there is always a char (the
last one ) equal to NOTHING.

Clearly, I would like to avoid the necessity to do the scan to remove
the strange Nothing char. Even the trimEnd instruction does not work,
and these strings cause general malfunnction of the program (for
instance prevent any string concatenation).

I cannot explain this strange thing. Does anybody have any idea how the
Nothing char can appear in the string and what is the best way to get
rid of it?

-t
 
I am extracting some field names from a table of a db (it's an
OledbSchemaguid table).

It occurs for some OleDbSchema driver that

NameDBField = cstr(DataRow.Item(3))

return a string that is composed by the characters of the field name,
for instance MYFIELD plus 1 Nothing. I know that the Nothing is there
because, since I was having a lot of problem with these strange strings
I have scanned them char by char.

This happens for any string extracted from any of the oledbschemaguid
tables using a certain OleDbSchema driver: there is always a char (the
last one ) equal to NOTHING.

Clearly, I would like to avoid the necessity to do the scan to remove
the strange Nothing char. Even the trimEnd instruction does not work,
and these strings cause general malfunnction of the program (for
instance prevent any string concatenation).

I cannot explain this strange thing. Does anybody have any idea how the
Nothing char can appear in the string and what is the best way to get
rid of it?

-t
That nothing character is the \0 from C++ and similar languages that
terminate a string with the NULL character. VB doesn't really
understand this character, and can't concatenate anything to it.
Try using this function for anything you get back from the OleDbSchema:

Private Function StripTerminator(ByVal strString As String) As String
'Strings returned from Windows end in chr(0). It needs to be
removed for VB.'

Dim intZeroPos As Integer

intZeroPos = InStr(strString, Chr$(0))
If intZeroPos > 0 Then
StripTerminator = Left$(strString, intZeroPos - 1)
Else
StripTerminator = strString
End If
End Function


Tom
 
Thank you Tom! :) that's very enlighting, and thank you very much for
the code you kindly provided, which is certainly faster than the one I
was using temporarily:

Dim Scanner As New System.Text.StringBuilder

Function RemoveNothing(ByVal Text As String) As String
Scanner.Length = 0
For Each u As Char In Text
If Not u = Nothing Then Scanner.Append(u)
Next u
Return Scanner.ToString
End Function

Actually I was afraid that some strange chars could be found in any
position. But your explanation has clarified that it can be found only
at the end (I hope!).

Actually, I do not understand why that character is there, while in all
the other OleDB provider I have tried (I tried almost all of them) I
never saw it.

In this case I am talking about MyOleDB, the one available for MYSQL
(it seems quite primitive, must say). It is strange that I did not hear
much complaint around: probably not many people are actually using it.

Is that a bug of the OLEDB provider or it is possible in principle
that, given the specifications to create OleDB Drivers, that strange
terminator chars could be found in the SchemaGuid tables?

Thank you VERY much!!

-tom




tomb ha scritto:
 
Thanks Dennis! :)

at this point I am really curious to get an idea of the respective
performance of the 3 solutions which
we have seen (although they are not really equivalent) ...

let's see ... here is the result of a quick and primitive test :

'TIME:
'RemoveNothing1 - around 10.000
'RemoveNothing2 - around 670
'RemoveNothing3 - around 1200

mmm .... actually I expected the replace to do better, as anyway
"InStr" id done through linear search (?) ...


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim t As Date = Now

Dim s As String = "khjlkjddsdvasdvfasbasdsd" & _
"sdfsdsdfgbsdfgsdfhlkhlhlhlkhj" _
& Chr(0) & Chr(0)

For i As Integer = 0 To 1000000
RemoveNothing3(s)
Next i

MsgBox(Now.Subtract(t).TotalMilliseconds)

End Sub

Dim Scanner As New System.Text.StringBuilder
Function RemoveNothing1(ByVal Text As String) As String
Scanner.Length = 0
For Each u As Char In Text
If Not u = Nothing Then Scanner.Append(u)
Next u
Return Scanner.ToString
End Function

Private Function RemoveNothing2(ByVal strString As String) As
String
'Strings returned from Windows end in chr(0). It needs to be
'removed for VB.'

Dim intZeroPos As Integer

intZeroPos = InStr(strString, Chr(0))
If intZeroPos > 0 Then
Return Microsoft.VisualBasic.Left(strString, intZeroPos -
1)
Else
Return strString
End If

End Function

Private Function RemoveNothing3(ByVal mystring As String) As String
Return mystring.Replace(Chr(0), "")
End Function
 
Thanks for sharing your timing test...it's as I suspected it would be.
However, if you have only a chr(0) at the end of the string, you might try
mystring=mystring.trimend(chr(0)). This might be faster. Also, if there are
several embedded chr(0)'s in the string, other than at the end, then I
suspect method 3 with the .replace method will be faster than 2.
 
I tried it.
Under same conditions, it's around 600.

Yes, method 3 is more general, as it replaces vbNullChar everywhere.
The last method proposed is probably the one which sufficies in this
specific
situation ("bug" in MyOleDB driver).

The key thing has been noticing (Tomb) that the "nothing" value is
caused by the terminator
vbNullchar (\0) ...

-tom
 
I tried it.
Under same conditions, it's around 600.

Yes, method 3 is more general, as it replaces vbNullChar everywhere.
The last method proposed is probably the one which sufficies in this
specific
situation ("bug" in MyOleDB driver).

The key thing has been noticing (Tomb) that the "nothing" value is
caused by the terminator
vbNullchar (\0) ...

-tom
Actually, this is not a bug at all. The C related environments all use
the chr(0) as a string terminator, as does the Windows OS. If you ever
used the Windows dll call to read a registry entry, it is returned from
the OS with that very terminator. MySql obviously was written in that
kind of language. VB actually has that terminator, it's just that we
don't see it - it is hidden fom us by the development environment. VB
uses a string descriptor rather than the actual array of characters
holding the string. I'm sure the originators of VB had a very good
reason for doing this, but it can get in the way of playing nice with
the other guys.
Anyway, these were some interesting approaches to the situation. It was
a learning experience for me also. And thanks for the recognition.

Tom
 
Hi Tom :)

I understood that. What I meant talking about "bug" is the following.

If you extract (through the command
OleDbConnection.GetOleDbSchemaTable) the SchemaTables using ANY other
OleDb driver (Oracle, Sybase, VFP, SQL server, jet, etc...) you will
get a clean list of items.

When you use MyOleDB OleDbDriver for MYSQL, * and only for this one * ,
each string in the schema shows that problem (a vbNullChar appended to
each string).

You can make a trial yourself (the driver is here:
http://sourceforge.net/projects/myoledb/ ). I use something like:

Try
Using SchemaGuidColumns As DataTable =
..OleDbConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns,
New Object() {})
'...

The connection string for MySQL is like that:

Me.ConnectionString = "Provider=MySQLProv" & _
";User ID=" & UserId & _
";Password=" & Password & _
";Location=" &
Me.ServiceNameServerNameLocation & _
";Data Source=" &
Me.DatabaseCatalog

Since this happens only for MyOLEDB driver, I suspect that this
situation may be defined as "bug", even though I do not know the reason
why this happens (perhaps the programmer of the driver has used 2
terminators (??) )

Any idea?

-tommaso
 
I haven't encountered any of this. It is only my C++ experience that
enabled me to decypher what that ending character is. But I appreciate
you sharing the work you've done with this, it will certainly be useful
in the very near future, as I am beginning a project that uses MySql as
the back-end.

Tom
 
Good. When you find out problems on that area, let's share them (just
drop an e-mail or post here), I am very interested on these (or
actually, on their solution :) and, for some, I already have some
solutions... )

for instance, another thing I have noticed is that MyOledb does not
seem to like at all table aliases ...

-tom

tomb ha scritto:
 
Back
Top