Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Val Grind (the wrong kind)

Val Grind (the wrong kind)

Scheduled Pinned Locked Moved The Weird and The Wonderful
23 Posts 12 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Chris Quinn

    That is a programmer's trick to handle null values - it converts a null value to an empty string, which then evaluates to zero. The use of the ampersand (&) makes this work - if the plus sign (+) is used to concatenate, an invalid use of null error will be thrown, as any variable concatenated using + will be nulled if any of the concatenated values are null It is equivalent to using functions such as IsNull, NZ etc

    ==================================== Transvestites - Roberts in Disguise! ====================================

    R Offline
    R Offline
    Rob Grainger
    wrote on last edited by
    #6

    I realize that, but that still doesn't excuse conceding from a long to a string, then back to a long again. That's just sloppy coding. Regardless, it's an appalling way to do the conversion.

    C 1 Reply Last reply
    0
    • R Rob Grainger

      I realize that, but that still doesn't excuse conceding from a long to a string, then back to a long again. That's just sloppy coding. Regardless, it's an appalling way to do the conversion.

      C Offline
      C Offline
      Chris Quinn
      wrote on last edited by
      #7

      It's not pretty, admittedly, but VB6 did not include the NZ function that later versions of VBA had. The alternative would be to create an NZ function and use that e.g.

      Function Nz(ByVal V As Variant, Optional ByVal ValueIfNull As Variant) As Variant
      If Not IsNull(V) Then
      Nz = V
      Else
      If IsMissing(ValueIfNull) Then
      If VarType(V) = vbString Then
      Nz = ""
      Else
      Nz = 0
      End If
      Else
      Nz = ValueIfNull
      End If
      End If
      End Function

      ==================================== Transvestites - Roberts in Disguise! ====================================

      R 1 Reply Last reply
      0
      • C Chris Quinn

        It's not pretty, admittedly, but VB6 did not include the NZ function that later versions of VBA had. The alternative would be to create an NZ function and use that e.g.

        Function Nz(ByVal V As Variant, Optional ByVal ValueIfNull As Variant) As Variant
        If Not IsNull(V) Then
        Nz = V
        Else
        If IsMissing(ValueIfNull) Then
        If VarType(V) = vbString Then
        Nz = ""
        Else
        Nz = 0
        End If
        Else
        Nz = ValueIfNull
        End If
        End If
        End Function

        ==================================== Transvestites - Roberts in Disguise! ====================================

        R Offline
        R Offline
        Rob Grainger
        wrote on last edited by
        #8

        You've missed the point again. I know exactly what the trick is doing. I'll spell it out slowly... 1. The first line declares PolNumb As Long. 2. Consequently, we know PolNumb is always a number. 3. The line If Val("" & PolNumb) > 0 Then effectively converts PolNumb to a string and appends it to an empty string, simply to convert it back to a number using Val, and finally checks if the result is greater than 0. That line could be replaced with If PolNumb > 0 Then and be more efficient and more correct (as conceivably PolNumbcould be zero). The issue is not the use of the Val("" & variant) trick, but the misuse of it applied to something we already know to be a number.

        M 1 Reply Last reply
        0
        • N Nagy Vilmos

          I've seen plenty of those "safe cast" functions.


          Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

          B Offline
          B Offline
          Brady Kelly
          wrote on last edited by
          #9

          Often those functions easily, if not totally safely, help a developer out of the mire that is VB6.

          N 1 Reply Last reply
          0
          • B Brady Kelly

            Often those functions easily, if not totally safely, help a developer out of the mire that is VB6.

            N Offline
            N Offline
            Nagy Vilmos
            wrote on last edited by
            #10

            ... and that, dear friend, is where I've seen them. ;P


            Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

            1 Reply Last reply
            0
            • R Rob Grainger

              From some legacy VB6 code I'm in the process of making redundant...

              Private Sub Command7_Click()
              Dim PolNumb As Long

              If Val("" & txtOpenPolicyNo) > 0 Then
                  PolNumb = Val("" & txtOpenPolicyNo)
              Else
                  PolNumb = Val("" & InputBox("Policy ID "))
                  If Val("" & PolNumb) > 0 Then
                     PolNumb = GetPolicyNumberfromID(PolNumb)
                  End If
              End If
              
              If Val("" & PolNumb) > 0 Then
                  FindPolicy GetPolicyIDfromNumber(PolNumb)
              End If
              

              End Sub

              I particularly like the lines...

              If Val("" & PolNumb) > 0 Then

              which effectively convert a number to a string, prepend an empty string, then convert it back again... just for good measure.

              R Offline
              R Offline
              RafagaX
              wrote on last edited by
              #11

              I've always told it: It's better to be in the safe side... :)

              CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

              1 Reply Last reply
              0
              • R Rob Grainger

                From some legacy VB6 code I'm in the process of making redundant...

                Private Sub Command7_Click()
                Dim PolNumb As Long

                If Val("" & txtOpenPolicyNo) > 0 Then
                    PolNumb = Val("" & txtOpenPolicyNo)
                Else
                    PolNumb = Val("" & InputBox("Policy ID "))
                    If Val("" & PolNumb) > 0 Then
                       PolNumb = GetPolicyNumberfromID(PolNumb)
                    End If
                End If
                
                If Val("" & PolNumb) > 0 Then
                    FindPolicy GetPolicyIDfromNumber(PolNumb)
                End If
                

                End Sub

                I particularly like the lines...

                If Val("" & PolNumb) > 0 Then

                which effectively convert a number to a string, prepend an empty string, then convert it back again... just for good measure.

                D Offline
                D Offline
                dshillito
                wrote on last edited by
                #12

                This is a standard VB6 idiom for dealing with data that might contain a Null value.

                R 1 Reply Last reply
                0
                • R Rob Grainger

                  You've missed the point again. I know exactly what the trick is doing. I'll spell it out slowly... 1. The first line declares PolNumb As Long. 2. Consequently, we know PolNumb is always a number. 3. The line If Val("" & PolNumb) > 0 Then effectively converts PolNumb to a string and appends it to an empty string, simply to convert it back to a number using Val, and finally checks if the result is greater than 0. That line could be replaced with If PolNumb > 0 Then and be more efficient and more correct (as conceivably PolNumbcould be zero). The issue is not the use of the Val("" & variant) trick, but the misuse of it applied to something we already know to be a number.

                  M Offline
                  M Offline
                  Mark Hurd
                  wrote on last edited by
                  #13

                  You can see from most of the replies, cargo cult programming is common in VB6. I had to deal with this sort of thing in code written for VB.NET but with Option Strict Off :-(

                  Regards, Mark Hurd, B.Sc.(Ma.) (Hons.)

                  R 1 Reply Last reply
                  0
                  • Sander RosselS Sander Rossel

                    At my current job we've got a lot of legacy code like that. Just put + "" to the end of every object you can imagine. It will effectively convert the object which you already had to a string and from there you can convert it back to whatever you want (but Val() is indeed very popular!). My company even had its own Val() function which returned 0 if an Exception was thrown :laugh:

                    It's an OO world.

                    public class Naerling : Lazy<Person>{
                    public void DoWork(){ throw new NotImplementedException(); }
                    }

                    L Offline
                    L Offline
                    LesF
                    wrote on last edited by
                    #14

                    Is that irony? "It's an OO world. public class Naerling : Lazy<Person>{" Now, a Person with type specifier "Lazy" is something I can understand, but a "Lazy" with type specifier "Person" does not seem to be good OO design to me. (just jokes)

                    R Sander RosselS 2 Replies Last reply
                    0
                    • M Mark Hurd

                      You can see from most of the replies, cargo cult programming is common in VB6. I had to deal with this sort of thing in code written for VB.NET but with Option Strict Off :-(

                      Regards, Mark Hurd, B.Sc.(Ma.) (Hons.)

                      R Offline
                      R Offline
                      Rob Grainger
                      wrote on last edited by
                      #15

                      Yes, I'm particularly surprised that in spite of writing explicitly why it is inappropriate in this case (to convert long->string->long), that people still post to indicate "this is a common idiom in VB6". I'd rephrase that as "this is a common idiocy in VB6". Cargo-cult programming sums it well too. I see why VB6 has such as bad reputation - and its not the language for the most part, and why programming languages aimed at mainstream use should take steps to protect programmers from themselves (strong typing, correct scoping, eliminate global variables, and make bad-idioms a struggle to use). I wonder if there's a cargo-cult approach on message boards too, where if you keep repeating the same wrong reply it will magically begin to work. "Insanity: Doing the same thing over and over again and expecting different results". Albert Einstein.

                      M 1 Reply Last reply
                      0
                      • D dshillito

                        This is a standard VB6 idiom for dealing with data that might contain a Null value.

                        R Offline
                        R Offline
                        Rob Grainger
                        wrote on last edited by
                        #16

                        ...and in my replies above I've already indicated why it is unnecessary to convert long->string->long.

                        C 1 Reply Last reply
                        0
                        • L LesF

                          Is that irony? "It's an OO world. public class Naerling : Lazy<Person>{" Now, a Person with type specifier "Lazy" is something I can understand, but a "Lazy" with type specifier "Person" does not seem to be good OO design to me. (just jokes)

                          R Offline
                          R Offline
                          Rob Grainger
                          wrote on last edited by
                          #17

                          Those aren't type specifiers, at least in any language I'm familiar with (C++, C#, Java).

                          1 Reply Last reply
                          0
                          • L LesF

                            Is that irony? "It's an OO world. public class Naerling : Lazy<Person>{" Now, a Person with type specifier "Lazy" is something I can understand, but a "Lazy" with type specifier "Person" does not seem to be good OO design to me. (just jokes)

                            Sander RosselS Offline
                            Sander RosselS Offline
                            Sander Rossel
                            wrote on last edited by
                            #18

                            Lazy<Something> and what's lazy? The Person. Read it out loud and it makes perfect sense :) Besides, I was more worried with signature design than OO design ;)

                            It's an OO world.

                            public class Naerling : Lazy<Person>{
                            public void DoWork(){ throw new NotImplementedException(); }
                            }

                            1 Reply Last reply
                            0
                            • R Rob Grainger

                              Yes, I'm particularly surprised that in spite of writing explicitly why it is inappropriate in this case (to convert long->string->long), that people still post to indicate "this is a common idiom in VB6". I'd rephrase that as "this is a common idiocy in VB6". Cargo-cult programming sums it well too. I see why VB6 has such as bad reputation - and its not the language for the most part, and why programming languages aimed at mainstream use should take steps to protect programmers from themselves (strong typing, correct scoping, eliminate global variables, and make bad-idioms a struggle to use). I wonder if there's a cargo-cult approach on message boards too, where if you keep repeating the same wrong reply it will magically begin to work. "Insanity: Doing the same thing over and over again and expecting different results". Albert Einstein.

                              M Offline
                              M Offline
                              Mark Hurd
                              wrote on last edited by
                              #19

                              Slightly in defence of the VB.NET Option Strict Off equivalent, I did see it provides a more user friendly error message. (Programmatically it loses information, because the exception type is always the same, but the error message actually displays the invalid "value".) This is NOT a defence of the current Long->String->Long scenario though.

                              Regards, Mark Hurd, B.Sc.(Ma.) (Hons.)

                              1 Reply Last reply
                              0
                              • Sander RosselS Sander Rossel

                                At my current job we've got a lot of legacy code like that. Just put + "" to the end of every object you can imagine. It will effectively convert the object which you already had to a string and from there you can convert it back to whatever you want (but Val() is indeed very popular!). My company even had its own Val() function which returned 0 if an Exception was thrown :laugh:

                                It's an OO world.

                                public class Naerling : Lazy<Person>{
                                public void DoWork(){ throw new NotImplementedException(); }
                                }

                                M Offline
                                M Offline
                                Member 4608898
                                wrote on last edited by
                                #20

                                I've seen code like x = 0 - y; which, apparently was a workaround for a compiler bug which sometimes generated the wrong code for x = -y; I've also seen lots of legacy code of the type you've described but not in VB. The thing is that the concatenation operator is & so think about why they've used + before you change it. Sometimes there is a reason for using the +. Just make sure the item on the right is a string: it may not always be a string. When it isn't, what is happening and what are they doing?

                                Sander RosselS 1 Reply Last reply
                                0
                                • M Member 4608898

                                  I've seen code like x = 0 - y; which, apparently was a workaround for a compiler bug which sometimes generated the wrong code for x = -y; I've also seen lots of legacy code of the type you've described but not in VB. The thing is that the concatenation operator is & so think about why they've used + before you change it. Sometimes there is a reason for using the +. Just make sure the item on the right is a string: it may not always be a string. When it isn't, what is happening and what are they doing?

                                  Sander RosselS Offline
                                  Sander RosselS Offline
                                  Sander Rossel
                                  wrote on last edited by
                                  #21

                                  Member 4608898 wrote:

                                  what is happening and what are they doing?

                                  No one really knows... There's lots of obscure bugs in code like that :) Luckily, one of the other 'magical solutions for all your problems' is the wonderful On Error Resume Next command. Really, if it was allowed people would've used On Error Resume Next + "" :laugh:

                                  It's an OO world.

                                  public class Naerling : Lazy<Person>{
                                  public void DoWork(){ throw new NotImplementedException(); }
                                  }

                                  1 Reply Last reply
                                  0
                                  • R Rob Grainger

                                    ...and in my replies above I've already indicated why it is unnecessary to convert long->string->long.

                                    C Offline
                                    C Offline
                                    cpkilekofp
                                    wrote on last edited by
                                    #22

                                    I'm still boggled a bit by the number of people who responded "this is a common idiom" without actually analyzing what was written. It's not that I'm not familiar with why it happens, it's just that so many who reply this way are being paid good money to manage code bases.

                                    "Seize the day" - Horace "It's not what he doesn't know that scares me; it's what he knows for sure that just ain't so!" - Will Rogers, said by him about Herbert Hoover

                                    R 1 Reply Last reply
                                    0
                                    • C cpkilekofp

                                      I'm still boggled a bit by the number of people who responded "this is a common idiom" without actually analyzing what was written. It's not that I'm not familiar with why it happens, it's just that so many who reply this way are being paid good money to manage code bases.

                                      "Seize the day" - Horace "It's not what he doesn't know that scares me; it's what he knows for sure that just ain't so!" - Will Rogers, said by him about Herbert Hoover

                                      R Offline
                                      R Offline
                                      Rob Grainger
                                      wrote on last edited by
                                      #23

                                      cpkilekofp wrote:

                                      I'm still boggled a bit by the number of people who responded "this is a common idiom" without actually analyzing what was written.

                                      Good, it's not just me then.

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups