Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is Number

3 views
Skip to first unread message

Aoli

unread,
Feb 28, 2022, 10:30:04 AM2/28/22
to
What is the fastest way to determine if a variant contains a whole number ?

This seems too complex
If VarType(vThing) = vbInteger or VarType(vThing) = vbLong or
VarType(vThing) = vbDecimal or VarType(vThing) = vbByte Then

Anyway testing floored me because this code told me the result is a
double ??? Why not decimal or even currency???

Changed Val() to CLng() or CDec() and that was OK.
Avoinding CCur because an unwanted decimal point might show up.
I think Val() did not grow up(mature) since GWBasic.

Also testing told me that my NumericTrue function is lightning fast.

cQP uses QueryPerformanceCounter

Gives a result of loop time 0.0056 Sec per loop or ~ 1,77 million per
sec. On my PC you know nothing about. lol Old Toshiba laptop.


' ==============================================

Private Sub cmdNbrTest_Click()

Dim lX As Long
Dim vThing As Variant
Dim bIsNbr As Boolean

cQP.TimerStartCount
For lX = 1 To 10000
bIsNbr = NumericTrue(CVar(Val("12345678")))

Next lX
Msg cQP.ElapsedReport("Nbr Test", True, 10000, 6)

End Sub

Private Function NumericTrue(vThing As Variant) As Boolean

NumericTrue = ((VarType(vThing) = vbInteger) Or _
(VarType(vThing) = vbLong) Or _
(VarType(vThing) = vbDecimal) Or _
(VarType(vThing) = vbByte))

Debug.Print "VarType(vThing)=", VarType(vThing)
' = 5 or vbDouble

End Function ' NumericTrue

' ==============================================

ObiWan

unread,
Feb 28, 2022, 12:12:49 PM2/28/22
to
:: On Mon, 28 Feb 2022 07:29:58 -0800
:: (microsoft.public.vb.general.discussion)
:: <svippo$5bc$1...@gioia.aioe.org>
:: Aoli <Ao...@Aoli.com> wrote:

> What is the fastest way to determine if a variant contains a whole
> number ?

bottom line, variants are inefficient if you need speed, use them
properly but use typed variables for your other purposes; do not
replace all your variables with variants.

Anyhow, try this


Dim lX As Long
Dim vThing As Variant
Dim bIsNbr As Boolean

cQP.TimerStartCount
vThing = CVar(Val("12345678"))
For lX = 1 To 10000
bIsNbr = IsNumeric(vThing)

Aoli

unread,
Feb 28, 2022, 1:54:55 PM2/28/22
to

I only use variants when necessary.
For the missing capability of VB6.

e.g.
Calling a method.
Public Function Add(vKey as Variant, vItem as variant)

I allow a key to be any alphanumeric. e.g. 123A
IsNumeric(12.3) says numeric but cannot be used as a index (whole
numbers only) but could be a key. A whole number is an index.

vKey can be a whole number index into my DB or a string Key into my DB.
vItem can be any type including an Array(1,2,3) of subitems.
I can also initialize the DB with multiple vItems.

I have this all working and provides me a class that is very powerful.
And best, the data is held in a multidimensional array so saving and
loading is fast and easy. The size is limited by memory and is adequate
for my purposes and I have total control. No databases for me for this
type of thing. My persisting a collection is now not needed.

I insert and remove intermediate items in the sorted array using
CopyMemory so adjusting a sorted array is fast.
I use BinarySearch to locate an item by the key.

ObiWan

unread,
Mar 1, 2022, 3:19:43 AM3/1/22
to
:: On Mon, 28 Feb 2022 10:54:50 -0800
:: (microsoft.public.vb.general.discussion)
:: <svj5ps$o51$1...@gioia.aioe.org>
:: Aoli <Ao...@Aoli.com> wrote:

> IsNumeric(12.3) says numeric but cannot be used as a index (whole
> numbers only)

well, you may always use something like (air code)

Function IsNumberOk(vData As Variant) As Boolean
IsNumberOk = ((IsNumeric(vData)) And (Val(vData) = CLng(vData)))
End Function


ObiWan

unread,
Mar 1, 2022, 3:25:52 AM3/1/22
to
:: On Tue, 1 Mar 2022 09:19:40 +0100
:: (microsoft.public.vb.general.discussion)
:: <20220301091...@mvps.org>
or either

Function IsInteger(vData As Variant) As Boolean
IsInteger = ((IsNumeric(vData)) And (Instr(vData, ".")=0))
End Function


0 new messages