(VB).Net question

  • I found a sort of WTF in VB.Net just now.

    Len(chr(1) & chr(0) & chr(0) & chr(1)) = 2             ...   WTF?

    It seems that somehow VB.Net eats the chr(0)'s. Is this also the case in C#? Is there any way around it? Anybody?


  • I don't use VB.net (I use c#) but the help says the Len function returns the size in bytes for types except string which returns the string's length.

    I noticed the & operator widens to string if needed but since the second character is 0 the rest will probably be ignored and that leaves you with only a 1 character. Basically (no pun intended) that'll leave you with a char (2 byte unicode) so maybe in this situation Len is giving you the size of a char.

    Ofcourse this is all guesswork [*-)]

    Put the &-ed result in a string variable first and see what VB makes of that. Len might return the right result.

    Anyway, the nicest way to get the length of a string is using the Length property of your string (.NET). See if that works.

  • I did some of the tests you mentioned. Thanks for taking the time to mention them.

    The problem only occurs in immediate mode (not sure C# has this). In my program all seems to be well. If I ask for the length of my program-string it reutns the proper value. However, if I print my string in immediate mode, it stops after the first chr(0). It also doesn't show the final double quote.

    I think the immediate mode cannot handle chr(0) in strings. Luckily I don't need to use immediate mode anymore, and this whole thing has made me code what I wanted to do much more efficiently. Maybe that's why the made it so WTF. So people would get all upset and think of better ways to do what they want [:P]


  • <FONT style="BACKGROUND-COLOR: #efefef">Just curious, why on earth would you use char(0) ;-)? </FONT><FONT style="BACKGROUND-COLOR: #efefef">Char(0) = NUL = string terminiator ... I would expect screwey results when you use that.</FONT>

    <FONT style="BACKGROUND-COLOR: #efefef">If you're looking for a good delim, use some of the other chars < 13. BEL works wonders for me ... look at an ascii or unicode chart, depending on the encoding you need.</FONT>

  • The reason for having a char(0) in the string is in itself a bit of a WTF, but let me explain.

    I'm building a .Net webservice into which you can logon. I decided to use rijndael encryption for the password. Then someone tells me that the is a VB6 app that will also have to log on there, so I hunt for a free VB6 rijndael encryptor/decryptor. I find one. Yippee. Only it won't decode any coded test messages.

    I found out why by stepping through it (yay! for source code). It seems to expect a character count as the forst four bytes as a sort of check. It's supposed to be a Long value turned into 4 bytes and then reversed.

    That's where the char(0)s come from. 4L = char(4) + char(0) + char(0) + char(0).

    I figured adding that to my string before turning into the byte array for encrypting would be easier than expanding the byte array that I get from my string by 4, and adding the length (to the front...).

    Anyway I ended up doing it the second way, which IMHO turned out to be more elegant than the first way to my surpsrise [:)]

    But tat's the reason for the char(0)


  • Another wtf is the CompareTo method of the Version object

    Public Overridable NotOverridable Function CompareTo...



  • Drak, you must be looking at VB6.

    VB.NET (1.1) defines it as:

    Public Overridable Function CompareTo( _
    version As Object _
    ) As Integer Implements IComparable.CompareTo

  • <FONT size=2>Public Overridable Function CompareTo( _ ByVal version As </FONT><FONT size=2>Object</FONT><FONT size=2> _ ) As </FONT><FONT size=2>Integer</FONT><FONT size=2> Implements IComparable.CompareTo</FONT>

    <FONT size=2>True Blue, but that's not what my intellisense says. It truly says for almost all methods in Version that they are Overridable NotOverridable. See the screenshot I made here.</FONT>

    <FONT size=2>Drak</FONT>

  • Gotcha, so therefore it's an IntelliSense WTF rather than a VB.NET WTF. 🙂

  • I love that docs bug. If you create a base class with an overridable method, and then mark the method in the ineherited class notoverridable, you get a "overridable notoverridable" method.

    I'm sure this will be fixed in 2005.

  • @Blue said:

    Gotcha, so therefore it's an IntelliSense WTF rather than a VB.NET WTF. 🙂

    Point taken [:D]


  • Len(chr(1)
    & chr(0) & chr(0) & chr(1)) =
    ...   WTF?

    Nope, that seems very logical to me. You're concatenating four string values and two of them happens to contain 1 character while the other two only contain the string-terminator character (char(0)) which means their length is 0. 1+0+0+1 equals two.

    Makes perfect sense to me... [:P]

  • Except that it doesn't work that way in compiled code[;)]

    <FONT style="BACKGROUND-COLOR: #efefef">Drak</FONT>

  • I know, but this is the .NET version. Delphi does the same, btw.
    Storing information about the string (or a dynamic array) at some
    negative offset. Still, some functions that accept strings will
    consider a chr(0) as a string terminator and I think this is what's
    happening somewhere. (Btw, as I understood it, the executable displayed
    it correctly but the immediate mode in VB seems to do things slightly
    different, and thus displays the wrong result.

    It's weird though. Even some Windows API's use the chr(0) as a
    delimiter vor string values. Especially with the shell functions. So
    why does MS mess things up in this case?

  • Could be a unicode issue. As you don tknow what the fuck is going on inside VB it could very well be using unicode strings?

    Anyway afaik unicode \0 is twice as wide as a char \0.

Log in to reply

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.