Improve company productivity with a Business Account.Sign Up

x
?
Solved

True or False - VB Constants

Posted on 2002-03-15
14
Medium Priority
?
235 Views
Last Modified: 2010-05-02
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

0
Comment
Question by:svfafel
  • 3
  • 3
  • 2
  • +6
14 Comments
 
LVL 15

Accepted Solution

by:
ameba earned 400 total points
ID: 6870169
Not True.
     MsgBox TypeName(TestConst)  ' shows String

You still have bad grading history and many open questions.  Do something!!
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 6870188
I agree the documentation says exactly what you said.  It is possible the compiler still treats it as a variant.

good luck
mlmcc
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6870414
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 6

Expert Comment

by:anthony_glenwright
ID: 6870529
I have heard this one before (that constants are variants unless specified).  So it might be true...
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6870591
To force a data type:

Public Const TestConst as String = "TestValue"

Public Const AcDc As Double = 1

0
 
LVL 17

Expert Comment

by:inthedark
ID: 6870595
I think loops work abaout 20% faster if using explicit declarations "Const Name As Type".



0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6870596
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
 
LVL 15

Expert Comment

by:ameba
ID: 6870666
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
 
LVL 39

Expert Comment

by:abel
ID: 6870735
> 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
 
LVL 39

Expert Comment

by:abel
ID: 6870742
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
 
LVL 3

Expert Comment

by:Bahnass
ID: 6870871

'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
 
LVL 39

Expert Comment

by:abel
ID: 6870914
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
 

Author Comment

by:svfafel
ID: 6870932
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
 
LVL 15

Expert Comment

by:ameba
ID: 6870999
Thanks, svfafel.

Thanks also to abel for test code - I got similar results.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question