View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default reduce public variables

Have tested UDT element versus variable and luckily there doesn't seem any
performance difference at all:

Option Explicit

Private Type UDT_Test
str1 As String
str2 As String
str3 As String
str4 As String
str5 As String
str6 As String
str7 As String
str8 As String
str9 As String
str10 As String
str11 As String
str12 As String
str13 As String
str14 As String
str15 As String
str16 As String
str17 As String
str18 As String
str19 As String
str20 As String
str21 As String
str22 As String
str23 As String
str24 As String
str25 As String
str26 As String
str27 As String
str28 As String
str30 As String
End Type

Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private lStartTime As Long
Private lEndTime As Long

Sub StartSW()
lStartTime = timeGetTime()
End Sub

Sub StopSW(Optional ByRef strMessage As Variant = "")
lEndTime = timeGetTime()
MsgBox "Done in " & lEndTime - lStartTime & " msecs", , strMessage
End Sub

Sub UDTTester()

Dim i As Long
Dim uTest As UDT_Test
Dim strTest As String
Dim strVar As String

uTest.str25 = "testing"
strVar = "testing"

StartSW

For i = 1 To 10000000
'strTest = strVar
strTest = uTest.str25
Next

StopSW

End Sub


RBS

"RB Smissaert" wrote in message
...
Nice to know that. How about though say 1000 public variables?
I can see one potential drawback now with using a UDT and that is if it is
used
in a long loop. Haven't tested but I suppose repeatedly doing UDT.Element
could
have some performance penalty. So that would mean I would need to make a
local
variable and use that in the loop.

RBS

"Bob Phillips" wrote in message
...
Don't know Bart, I see no problem with them. They don't even make the
code
more difficult to understand IMO, if you use proper commenting and
sensible
names they are just as 'easy' to understand as classes, UDTs, or any
other
variant.

--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)

"RB Smissaert" wrote in message
...
Bob,

Didn't know that one, thanks for the tip.
Not sure if it helped that much in this particular situation as it means

to
take advantage of that I would have
to rename for example all my .ini file related variables. For example

start
with bINI, strINI etc.
I think the UDT is the best option.

What is actually so bad about public variables, apart from the fact that

it
can make the code more difficult to understand?

RBS


"Bob Phillips" wrote in message
...
Bart,

Don't forget with any variables, you get a form of intellisense if you
type
the first few letters then hit Ctrl-Spacebar.

--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)

"RB Smissaert" wrote in message
...
OK, I understand.
I think I will take the .ini file related variables and change them
to
elements
of a UDT and see how that is suiting me.
I don't need the option to loop through a load of these variables, so
I
think
the UDT is better here than the array with constants for the array

index
as
with
the UDT I will have the intellisense.
Maybe in the end I might come to the same conclusion as Bob Phillips,

if
it
ain't broke, don't fix.

RBS

"keepITcool" wrote in message
.com...

Consider a userform with a few related checkboxes for string
manipulation: to transfer/store the settings you could use 1 long

(eg.
lngStrConv iso 5 booleans: blnEnabled, blnUpperCase,
blnTrimLeading,
blnTrimTrailing, blnRemComments.

Somewhere you define the constants for the flags
(note the values use sequential and exclusive bits)
Const lscENABLED = &H1&
Const lscUPPER = &H2&
Const lscTRIML = &H4&
Const lscTRIMT = &H8&
Const lscREMCMT = &H10&

'setting the long would look like:
if chkEnabled then lngStrCont = lscENABLED
if chkUppercase then lngStrConv = lngStrConv OR lscUPPER
if chkTrimLeading then lngStrConv = lngStrConv OR lscTRIML
if chkTrimTrailing then lngStrConv = lngStrConv OR lscTRIMT
if chkRemComments then lngStrConv = lngStrConv OR lscREMCMT


'Using the individual flags somewhere else could look like:
If lngStrConv and lscENABLED then
If lngStrConv And lscUPPER then
s = UCase$(s)
end if
if lgnStrConv And lscTRIML then
s = LTrim(s)
end if
end if

You'll have to get comfortable with working with bits/flags.
but once familiar it's not difficult. However be carefull with NOT.
often you must use NOT CBool(lngMask AND flag)

e.g
?13 AND 4 = 4
?Not 4 = -5
?cbool(-5) = TRUE
but
?not cbool(13 and 4) = FALSE

HTH

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


RB Smissaert wrote in

Interesting the idea of an array with constants for the array
index.
As you say, it has the benefit above a UDT that you can loop
through
it. You can't do that with a UDT or can you?

Didn't get this one:
Use 1 long with 'bit flags' to replace a series of booleans.
Could you give a simple example?

RBS


"keepITcool" wrote in message
.com...

Alternatively you can:

Use 1 long with 'bit flags' to replace a series of booleans.
(you'll need to add several constants (or enums if you code for
VBA6), but I find it very convenient for managing data from and
to
userforms with many options.

Use arrays for similar variables of same type. Use constants to
identify the position in the array. e.g. iso
rngMain,rngDest,rngSource,rngTemp you'd use
arng(MAIN),arng(DEST),arng(SOURCE),arng(TEMP)


Arguably the above may look like cosmetics, but I find it
produces
more readable and manageable code (reset 20 range variables with
erase arng) Further it's fairly easy to implement in an existing
situations with (CAREFULL) search & replace. (use MZtools!)


-- keepITcool
www.XLsupport.com | keepITcool chello nl | amsterdam


RB Smissaert wrote in



When you have a large application with lots of modules and
forms
and a large number of public variables what would be the best

way
to organize and reduce these public variables? Making all
these
variables private or local is not an option as it would mean
an
enormous duplication of code. The 2 options I can see are

making
classes or UDT's and maybe UDT's are the simplest option. So
with a UDT I would get a public type with a number of elements
that are the old public variables. Then there would be a
public
variable declared as this public type. Would this make sense
or
are there any better options?

RBS