Results 1 to 9 of 9

Thread: [RESOLVED] Using Len() Function - VB.NET

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Location
    Howell, MI
    Posts
    134

    Resolved [RESOLVED] Using Len() Function - VB.NET

    Hi,

    Is there a better way of getting the length of a variable pulled from a SQL DB other than the LEN() function? I'm printing the output from my query and when I step through the code, I get very strange results. If the variable is a DATE such as "12/1/2007", the LEN() function returns 8 and other weird things when I can see what the value is and count the characters, I get results that I don't understand. I am using the results in SPACE() for proper spacing and it isn't working out too well.

    Any ideas?

    PS - I was just looking at the data types that I used in my DB. I used smallmoney, smalldatetime, and money. These are the variables that I get funny LEN() results for. The other types are varchar(50) and they give the proper length. Hope this helps.

    PPS - Should this be in the Database section of questions?


    Thanks.
    Last edited by tomroth; Oct 22nd, 2007 at 08:30 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Using Len() Function - VB.NET

    There's nothing weird about it. All that shows is that you don't know how Len works. People assume that the Len gives the length of a string because that's how it almost always gets used. That's not its specific purpose though, as the documentation would tell you.

    The purpose of the Len function is to tell you size, in bytes, of the object when it gets saved to disk. For strings that equated to the number of characters, as each ASCII character occupies one byte, so that's what it returns for strings. For objects other than strings it doesn't, and why would it? Given that a Date object is a number, how can the number of characters it contains have any meaning? The number a Date contains is 8 bytes long, which is why Len returns 8 for any and all Date objects.

    Now, you say this:
    If the variable is a DATE such as "12/1/2007"
    but that's not a Date object. That's a string representation of a date. If you were to do this:
    vb.net Code:
    1. Len(myDate.ToString("dd/MM/yyyy"))
    then you'd get a value of 10, because there are 10 characters in the string you created to represent the date.

    Having said all that, you shouldn't use Len anyway. If you want the length of a string then you should use the Length property of that String object. For types other than String length doesn't really mean anything.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Location
    Howell, MI
    Posts
    134

    Re: Using Len() Function - VB.NET

    Why doesn't length really mean anything if I am trying to format printed output? I need the length, in characters, to get the spacing correct. This is getting SQL quickly but... if I do this:

    Code:
    Length = Len(row("Finishdate").ToString)
    I get 21 (the actual value is "12/3/2006"). There isn't a Length property for this. I made it a STRING though, right?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Using Len() Function - VB.NET

    No, the actual value is NOT "12/3/2006". Do this:
    vb.net Code:
    1. MessageBox.Show(row("Finishdate").ToString)
    and you'll see what the actual value is.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Location
    Howell, MI
    Posts
    134

    Re: Using Len() Function - VB.NET

    That is why you are the master and I am the humble student trying to learn. I guess the only thing to do is to chop off the not needed part.

    Thanks. One thing I don't understand is that the first two in my code give the correct result as far as length goes, as they are strings, but when I use Space(expression) for spacing, they are still off.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Using Len() Function - VB.NET

    Assuming that row is a datarow, something like this ought to be close, though I haven't tested it:

    length = (CDate(row("FinishDate")).ToString).Length
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Location
    Howell, MI
    Posts
    134

    Re: Using Len() Function - VB.NET

    That gives the same 21 as the plain old .ToString. I should be able to parse out what I don't need.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Using Len() Function - VB.NET

    Do the messagebox thing if you don't know what that extra stuff is. I'm pretty sure it will be time, which reminds me that the Date object has a whole bunch of To....String methods. I suspect that rather than .ToString, I should have used .ToShortDateString
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Location
    Howell, MI
    Posts
    134

    Re: Using Len() Function - VB.NET

    That did it. How long have you been doing this stuff? I've only been at it about 9 months now but The amount of stuff you know is staggering. I've wondered how anyone could ever know all of the methods in VB. I'm sure you don't know them all, but your knowledge is amazing!

    Thank you very much. This post is resolved as far as I'm concerned. I should be able to figure out the Space() thing.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width