True or False - VB Constants

someone just told me that this:

Public Const TestConst = "TestValue"

is not good because it is a variant data type.  Does anyone know if that is true?

They said this is better:

Public Const TestConst As String = "TestValue"

Can anyone verify this?  This is what Microsoft Documentation says:

**************************
If you don't explicitly declare the constant type using As type, the constant has the data type that is most appropriate for expression.
**************************

Can anyone verify/clarify any of this?

Thanks

svfafelAsked:
Who is Participating?
 
amebaCommented:
Not True.
     MsgBox TypeName(TestConst)  ' shows String

You still have bad grading history and many open questions.  Do something!!
0
 
mlmccCommented:
I agree the documentation says exactly what you said.  It is possible the compiler still treats it as a variant.

good luck
mlmcc
0
 
Arthur_WoodCommented:
Technically both are true.  Since there is NO explicit Type in the declaration, the variable is in fact a VARIANT.  But because it is assigned a STRING value, VB, under the covers, does an IMPLICIT Type conversion to String, so that the TypeName(TestConst) returns String.  But the variable internally, in memory, is reserved as a Variant.  It is ALMOST nvere a good idea to NOT use explicit Type declarations.  ANd if you EVER intend to move up to VB.NET, get OUT of the Habit of using Varaints, as None of the .NET languages support VARIANT.

Arthur Wood
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
anthony_glenwrightCommented:
I have heard this one before (that constants are variants unless specified).  So it might be true...
0
 
inthedarkCommented:
To force a data type:

Public Const TestConst as String = "TestValue"

Public Const AcDc As Double = 1

0
 
inthedarkCommented:
I think loops work abaout 20% faster if using explicit declarations "Const Name As Type".



0
 
Anthony PerkinsCommented:
Ameba,

I agree with you that it is a string, however since
TypeName(x) <> "Variant"

There is no way of knowing, is there?

Anthony
0
 
amebaCommented:
Correct, acperkins, TypeName will not return "Variant".
My guess is that compiler will choose the right type at compile time.
There is a way to get ASM code, but I don't know enough to recognize any conversion inserted by compiler.

Maybe someone can compare performance - test string operation with constants: declared As String vs. unspecified type.  If the unspecified type is stored as variant internally, compiler will insert Cstr() behind the scene and it will be slower.
0
 
abelCommented:
> My guess is that compiler will choose the right type at compile time.
You are probably right. Consider these timings:
Const String = 12.973,098 msec
Const Var String = 12.991,451 msec
Const Variant (str content) = 28.523,670 msec
Const Long = 647,806 msec
Const Var Long = 668,040 msec
Const Variant (long content) = 1.755,480 msec

They are made with QueryPerformanceXXX functions, using the following code and no compiler-optimizations:


Private Sub TestConstTypes(iter As Long)
    Const cString As String = ""
    Const cVarString = ""
    Const cVariantStr As Variant = ""
    Const cLong As Long = 150&
    Const cVarLong = 150&
    Const cVariantLng As Variant = 150&
    Dim times(10) As String
    Dim testString(100) As String
    Dim testLong(100) As Long
    Dim i As Long, j As Long
    Dim tmr As New CTimingPC
   
    tmr.Reset
    For j = 0 To iter
        For i = 0 To 100
            testString(i) = cString
        Next i
    Next j
    times(0) = tmr.sElapsed
   
    tmr.Reset
    For j = 0 To iter
        For i = 0 To 100
            testString(i) = cVarString
        Next i
    Next j
    times(1) = tmr.sElapsed
   
    tmr.Reset
    For j = 0 To iter
        For i = 0 To 100
            testString(i) = cVariantStr
        Next i
    Next j
    times(2) = tmr.sElapsed
   
    tmr.Reset
    For j = 0 To iter
        For i = 0 To 100
            testLong(i) = cLong
        Next i
    Next j
    times(3) = tmr.sElapsed
   
    tmr.Reset
    For j = 0 To iter
        For i = 0 To 100
            testLong(i) = cVarLong
        Next i
    Next j
    times(4) = tmr.sElapsed
   
    tmr.Reset
    For j = 0 To iter
        For i = 0 To 100
            testLong(i) = cVariantLng
        Next i
    Next j
    times(5) = tmr.sElapsed
   
    txtResults.Text = "Const String = " & times(0) & vbCrLf & _
        "Const Var String = " & times(1) & vbCrLf & _
        "Const Variant (str content) = " & times(2) & vbCrLf & _
        "Const Long = " & times(3) & vbCrLf & _
        "Const Var Long = " & times(4) & vbCrLf & _
        "Const Variant (long content) = " & times(5)
   
   
End Sub
0
 
abelCommented:
CTimingPC is from VBSpeed, http://www.xbeat.net/vbspeed. I also tested with optimizations, which took away the difference between the results for the Const Long and the Const Var Long. Declared as Variant always appears to be the slowest.

Conclusion? VB6 chooses the type of Const variables at compile time. Which is quite logical, I guess, because they are Constants and hence the compiler won't have any problem optimizing for a specific data-type. Unless you specify it specifically, then the compiler won't change your declaration.

My guess is that the small difference between the two Longs is only due to system activity during the test...

Of course, we are talking constants here! For normal variables it still holds that a not specifically declared variable is a Variant for the compiler and thus much slower! But everybody knew that already, I guess ;)

Cheers,
Abel
0
 
BahnassCommented:

'not good ????
about speed  I think NO SIR TRY THIS CODE

Const Q = 10: Const QQ As Integer = 10
Dim QQQ: QQQ = 10       ' Variant
Dim I As Long, A As Long
Debug.Print Time,
For I = 1 To 50000000
    A = A + Q * 2 - Q
Next
Debug.Print Time

Debug.Print Time,
For I = 1 To 50000000
    A = A + QQ * 2 - QQ
Next
Debug.Print Time

Debug.Print Time,
For I = 1 To 50000000
    A = A + QQQ * 2 - QQQ
Next
Debug.Print Time

'Typical Result on PII 350 M M/C
'03:34:11     03:34:28     = 17 sec
'03:34:28     03:34:45     = 17 sec
'03:34:45     03:36:00     = 75 Sec   ( Variant )
0
 
abelCommented:
Just curious: how did you determine the optimized speed after compiling? Surely you didn't see the output from Debug.Print? I know that my numbers where completely different when run from the IDE! And that's not the performance that is interesting, is it?

And yet, the funny thing is that you have the same results/conclusions as I had ;-)

Cheers!
0
 
svfafelAuthor Commented:
Thank you everybody for the quick and thorough answers!!!! I wish point could go to all....thanks for the extra input, etc.

Ameba - thank you for the reminder about my open questions...i have got a little laxed (during the "bad" days of EE)....I will take care of the opens....

0
 
amebaCommented:
Thanks, svfafel.

Thanks also to abel for test code - I got similar results.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.