Sticky Boolean

G

Greg Maxey

Hi All,

Are Boolean's sticky? I cobbled togther this routine which is called from a
bigger macro that I am cobbling and it seemed sticky. After all
highlighting had been removed from a document the boolean fHight (a Private
declaration in the module) would still return True. I finally forced it to
be false at each time by adding

fHLight = False - immediately after the Call ResetFRParameters line.
is this how this need to be done?

Thanks

Sub TestForAnyHighlight()
Call ResetFRParameters
fHlight = False
With Selection
.HomeKey wdStory
.Find.ClearFormatting
.Find.Highlight = True
If .Find.Execute Then
.Collapse wdCollapseEnd
fHlight = True
End If
End With
End Sub
 
M

Malcolm Smith

Greg

What is the purpose of the boolean variable anyway? It doesn't seem to do
anything at all.

Have you considered making your variables local to the routine and if the
boolean required outside the routine then why isn't it a function which
returns a boolean type?

Such as:


Function TestForAnyHighlight() as Boolean

Dim bHasFoundHighlight as Boolean

bHasFoundHighlight = False
ResetFRParameters

With Selection
.HomeKey wdStory
.Find.ClearFormatting
.Find.Highlight = True
If .Find.Execute Then
.Collapse wdCollapseEnd
bHasFoundHighlight = True
End If
End With

TestForAnyHighLight = bHasFoundHighlight

End Function


If this is what you want then this would be a better way to do it. I
think that your problem is that your boolean variable' scope is far too
large. Try to keep your scopes as small as possible and use functions to
return values rather than module level variables. This way everything is
self contained in nice neat little packages.

Does this help at all?

- Malc
www.dragondrop.com / www.ukhorseracing.co.uk
 
G

Greg Maxey

Malcolm,

Yes the bolean is used in a larger routine. I will have a hard look at your
suggestions this evening. I have never used a function. Actually I have
never heard of one until doing some reading last night.

Thank you.
 
G

Greg Maxey

Malcolm,

I have changed my Sub (which was called by another
routine) to a Function.

Function TestForAnyHighlight() As Boolean

bHLFinder = False
ResetFRParameters
With Selection
.HomeKey wdStory
.Find.ClearFormatting
.Find.Highlight = True
If .Find.Execute Then
.Collapse wdCollapseEnd
bHLFinder = True
End If
End With
TestForAnyHighlight = bHLFinder
End Function

FHLFinder is declared as Boolean in the Module.

Before in my main routine I would:

Call TestForAnyHighlight()
If fHighlight Then ...

How to use the Function statement had me stumped for a
bit. This is how I am using it. It works, but is it
correct:

If TestForAnyHighlight() Then ...

What is the purpose of the ()? It seems to work with or
without them.

Thanks again Malcolm. You are one of many helpful
individuals involved in my schooling :)
 
M

Malcolm Smith

Greg

You really shouldn't use the Boolean at a modular level; since you are
only working with it in the function then you ought to define it after the
Function statement so that it's not seen outside of this routine.

The aim of successful coding is to keep everything encapsulated as
possible. The fewer modular or Public variables then the less chance you
will have of saying to yourself "Now, where on earth is that defined?" or
"Why the blazes is that variable being overwritten?".

Or expletives to that effect.

So in your routine you say you do the following:
Call TestForAnyHighlight()
If fHighlight Then ...

Now, remember that TestForAnyHighlight() returns a boolean so you should
really use the value it returns:

If TestForAnyHighlight() then


end if

There is NO need to use that Boolean as that boolean should be internal to
that function. Now, you ought to use that function like this because the
function returns a value (and value type) which you would want to use.

Now, as a rule put brackets after a function call. Also if you call a
function then make sure that you use the type it returns, for example:

Dim bIsAnyHighlights as Boolean

bIsAnyHighLights = TestForAnyHighlight()


There are some coding practices which ought to be followed:

1. Always limit the scope of every variable as much as possible.
2. If the routine is a function then use the returned value. Even if
nothing is done with it. If nothing is done with it then why is it a
function?
3. Take the 'Call' statement outside and shoot it. It's a throwback to
ancient times.


I would suggest that you get hold of the excellent book: Code Complete by
Steve McDonnel and read that. It's a great and easy read and it will help
you no end.

Hope that this helps
- Malc
www.dragondrop.com
 
G

Greg Maxey

Malcolm,

Thanks for the lessons. I think I have it worked out now. I will keep an
eye out for the book you recommend.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top