Re: Please help: Numeric class properties
From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 03/05/04
- Next message: Bob O`Bob: "Re: Moving to XP"
- Previous message: Bob O`Bob: "Re: lunch application"
- In reply to: Paul: "Please help: Numeric class properties"
- Next in thread: Steve Gerrard: "Re: Please help: Numeric class properties"
- Reply: Steve Gerrard: "Re: Please help: Numeric class properties"
- Reply: Paul: "Re: Please help: Numeric class properties"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 5 Mar 2004 13:47:46 -0500
> Please can somebody advise on the following. I wish to develop a class
> that reflects a table in my database. Some of the fields in the
> database are numeric and my business rules state that some of them are
> required. I wish to validate the property values before writing them
> to the database from within my class. From a user perspective they
> enter a value in a text box and click the add button where upon I
> assign the values to the various properties of my class. However, if I
> try and assign a text value to a property of type double for example,
> I'll get an error before I've had a chance to call the method that
> validates the entries. I could add some validation code to the front
> end that notifies the user if they enter text instead of a number but
> then I have the possibility of the user receiving two error messages,
> one when they are entering the values and the second when they attempt
> to submit them. Ideally I'd like just one validation procedure.
> Instead of making numeric properties should I convert them all to
> string properties then check to see if they are numeric from within my
> class (e.g. using the IsNumeric function etc.). What is the common
> practice in situations like these.
I'm not sure I fully understand your problem. Can't you just validate the
TextBox entry BEFORE you attempt to assign it anywhere? If it is not
numeric, pop up your error message and, when the user dismisses it, throw
focus back to the TextBox; only do your assignment if the TextBox entry is
in proper form.
As for checking the entry using IsNumeric, consider the following previous
post of mine...
Rick - MVP
I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note at end of post):
ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")
Most people would not expect THAT to return True. IsNumeric has some "flaws"
in what it considers a proper number and what most programmers are looking
for.
I had a short tip published by Pinnacle Publishing in their Visual Basic
Developer magazine that covered some of these flaws. Originally, the tip was
free to view but is now viewable only by subscribers.. Basically, it said
that IsNumeric returned True for things like -- currency symbols being
located in front or in back of the number as shown in my example (also
applies to plus, minus and blanks too); numbers surrounded by parentheses as
shown in my example (some people use these to mark negative numbers);
numbers containing any number of commas before a decimal point as shown in
my example; numbers in scientific notation (a number followed by an upper or
lower case "D" or "E", followed by a number equal to or less than 305 -- the
maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for
Hexadecimal, &O or just & in front of the number for Octal).
NOTE:
======
In the above example and in the referenced tip, I refer to $ signs and
commas and dots -- these were meant to refer to your currency, thousands
separator and decimal point symbols as defined in your local settings --
substitute your local regional symbols for these if appropriate.
As for the question about checking numbers, here are two functions that I
have posted in the past for similar questions..... one is for digits only
and the other is for "regular" numbers:
Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Not Value Like "*[!0-9]*" And _
Len(Value) > 0
End Function
Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "."
End Function
- Next message: Bob O`Bob: "Re: Moving to XP"
- Previous message: Bob O`Bob: "Re: lunch application"
- In reply to: Paul: "Please help: Numeric class properties"
- Next in thread: Steve Gerrard: "Re: Please help: Numeric class properties"
- Reply: Steve Gerrard: "Re: Please help: Numeric class properties"
- Reply: Paul: "Re: Please help: Numeric class properties"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|