- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Yep, it gets worse if you're not careful
"2" + "2" = "22"
"2" + 2 = 4
Admin
VB has a Like operator?
The hell?
Admin
I'd rather jack than fleetwood mac
Admin
As a long time VB developer and instructor, I would certainly fault the coder for the MsgBox inside the validation routine. It would be better to pass error messages up through a parameter or return a non-empty string, describing the error, if there were validation problems. (instead of returning a Boolean value)
What I find curious is the lack of external validation. Maybe that follows this validation step. Is this OrderNumber in the database? Is this an active order number? Is this user allowed to see this order?
My validation credo is "The best validation is to prevent the user from entering wrong data." KeyPress events are a common place to prevent invalid characters or wrongly structured data. Although more difficult to work with, the masked edit control would help limit the user to numeric digits without doing any coding. If feasible, I would prefer showing eligible order numbers in a combobox.
I would generalize the suggested validation a bit, allowing for varying lengths of numeric strings. In the case of this WTF VerifyOrdNum function, the length would be 7.
Admin
I find it ironic that (from what I can tell) there is actually a typo in the description. " After a little while of going through and documenting the code, they were delighted to finally see some a comment..." some a comment? ;)
Admin
CInt = Convert a value to an Integer
CLng = Convert a value to a Long Integer
If the function is not just converting the data, but also modifying it, then it's not exactly a clean conversion is it? I understand that in the case of decimal numbers being converted to whole numbers, there has to be some kind of change. But why use the obscure bankers rounding?? Maybe change CInt/CLng to have an optional parameter to either round or truncate the number.
Admin
Don't know if anyone pointed this out earlier, but would it ever get to the error handling since the assignment (numOrd = xOrdNum) is after the On Error GoTo errorhandler
Admin
I did not use either and triggered rounding.
It might well be performing as designed, but I looked rather hard through the docs for information on how the rounding was done. I found nothing. Are you referring to the VB6 docs or something later?
Sincerely,
Gene Wirchenko
Admin
It feels kinda dirty to know this much about VB, but...
When you put in the "On Error" statement, it means from that point in the code until the end of the Sub or Function, don't crash if a run-time error occurs. It's most beneficial to put the statement at the top of your function, but you can put it in the middle as this guy did. It just means that any errors caused by code above this line will crash the program.
Admin
Don't feel dirty, sometimes it is good to know about the evil that is called VB. In fact, I would say that this makes sense, considering that VB is loosley based on a language that you had to specify the line numbers for each and every line of code and was considered good practice to use multiples of 10 so that if you wanted to add a line somewhere, you wouldn't have to re-number everything and wouldn't break existing "GoTo" statements.
The best trick that the devil ever played was to convince the world that he doesen't exist.
Admin
Ignorance is better?
No, it does not. It mean that that on error statement does not handle it. There can be other on error statements further up (in calling code).
Sincerely,
Gene Wirchenko
Admin
Ignorance is better than feeling kinda dirty? ummm, yeah! at least sometimes! I'm sure that if you though about it, you would agree.
<font color="#ff0000"></font>
Admin
Well, I do not. The languages can be useful. They might not be what you like or can use, but knowing them does not contaminate you. Not knowing them, but still whining about them makes you a whining ignoramus. There is a squaring effect there.
Sincerely,
Gene Wirchenko
Admin
Acctually, you are both right and wrong on this (Ahhh the dichotomy!). While the languages can be useful, knowing them can contaminate you. In my case, I had to go back and relearn a lot of foundation type stuff and unlearn some bad/lazy habits when I "upgraded" from VB to Java. If I had happen to learn Java, C++, or even Pascal (although pascal is pushing it), first, then I would not have been contaminated with that VB knowledge and life would have been measurably easier for me . Truth be told, I was doomed this pain from the start, being my first language at age 11 was "GW-Basic"... ahh, those were the days, line numbers, goto's, good stuff.....
Anyways, my original point was that "SOMETIMES" ignorance is better than feeling kinda dirty. Here is my proof: Your ignorance of what it tastes like to lick my rectum is certainly better than the dirty feeling that both you and I would have in the event that you were able to gain that knowledge.
As opposed to my original post, please do not think about this too much. Much more pleasant if you just simply agree.
Admin
If this works, I don't have a problem with it. In fact, it almost seems (dare I say it?) elegant.
As far as I can tell, the main risk in using "ON ERROR GOTO" here is that some whiz kid will find your code later on and make fun of it... which seems to be exactly what has happened.
Not knowing the nuances of VB6, I can't vouch for the proper performance of the code under every single situation, but this does seem like a potential way to ensure the input string consists solely of digits.
Many people would say it is bad form to use the error handling system for validation. There is more overhead associated with something like try/catch than there is with something like IsNumeric. But IsNumeric() won't work in this situation (a "numeric" is not really what you're checking for); and unless there's a function along the lines of "IsLong" or "IsSequenceOfDigits,", then there may not be a way to check validity without resorting to much more verbose code (e.g. looping through each character).
And I would not consider it worthwhile to search for "IsLong" or "IsSequenceOfDigits" without a really compelling reason (i.e. unless I could not reasonably make it work otherwise). Yes, there is probably a function somewhere in the relevant libraries that does what's necessary... but it is located and named in the manner of whatever programmer originally developed it, and it will take one quite a while to become as familiar with its name / location as I am with VB casting and error handling (which one would presumably already have mastered.)
I have worked on several projects where extensive object hierarchies, libraries, custom controls, etc. associated with validation were laboriously developed and promulgated. To me, that is the real WTF (i.e. "who the f_ck," as in "who the f_ck needs to use inheritance and polymorphism to check to see if an input string contains only digits?")
Is anyone going to flame me for saying this? I am sorry, but I have not spent the better part of my life learning computer programming in order to take its most advanced concepts and apply them to $hit-ass problems like validation and logging. To me, validation and logging fall into the category of "things that can be accomplished using if-then and windows.h."
Admin
Uhh.. doesn't this function always immediately return True, as soon as the assignment on the first line occurs?
Admin
Not necessarily. Attempting to assign a String to a Long value has a chance of triggering the Data Type Mismatch error. If the string is a number, eebil type casting will bypass this; if it isn't, the resulting exception will be caught by On Error Goto.
(Tho IIRC when I coded in VB6 SP6 on Option Explicit I needed to CInt/CDouble first before I could do this without getting kicked.)
Admin
This is the real WTF. The first line of the function makes it so that it always returns TRUE. The obscure code following this statement is never even executed.
Admin
Actually, it does something Isnumeric doesn't - it checks if it fits in a long. In particular,
?isnumeric("1234E99")
True
would be numeric and 7 digits, but would fail the minute you tried to put it in a long. My favorite tests were to put "1E99" and break all the developper's code here.