Use Proc with Static Variables Multiple Times 
Author Message
 Use Proc with Static Variables Multiple Times

I have a procedure that contains static variables.

This procedure can be called multiple times from a list
which allows different parameters to be passed to the procedure
with the static variables.  The return value of each call is then used to
update other arrays which are then processed independently of each other.

I need a solution to save the static values for each
independent call so that when the list (each call) is executed
the return value is correct.

Any ideas?



Thu, 18 Aug 2011 23:47:25 GMT  
 Use Proc with Static Variables Multiple Times

Quote:

> I have a procedure that contains static variables.

> This procedure can be called multiple times from a list
> which allows different parameters to be passed to the procedure
> with the static variables.  The return value of each call is then used to
> update other arrays which are then processed independently of each other.

> I need a solution to save the static values for each
> independent call so that when the list (each call) is executed
> the return value is correct.

An array or if need these permanently, a corollary file.  If sizable and
lookup is an issue, some sort of indexing/hashing scheme may be desirable.

If, of course, my crystal ball is actually working...

--



Fri, 19 Aug 2011 00:15:53 GMT  
 Use Proc with Static Variables Multiple Times

Quote:
>I have a procedure that contains static variables.

> This procedure can be called multiple times from a list
> which allows different parameters to be passed to the procedure
> with the static variables.  The return value of each call is then used to
> update other arrays which are then processed independently of each other.

> I need a solution to save the static values for each
> independent call so that when the list (each call) is executed
> the return value is correct.

> Any ideas?

I'm not trolling, honestly!  This sounds like *exactly* one of the scenarios
that VB6 Class modules handle though.

One of the premises of OOP is data and code encapsulation.  By writing a
Class that wraps your "static" data along with the procedures that operate
upon it, you can have multiple instances (objects) each of which has its own
copy of the Private data in the Class.

Something you get "for free" is that several routines can be put into the
Class and they can all operate as needed on the same copy of this
private/static data.

Using a Class can also cut down the size of your parameter lists in the same
way.  You might use Properties or parameters on some Init Method to set
things up, and all of the other Methods will be able to use the instance's
copy of this Private data.

Just a thought.



Fri, 19 Aug 2011 02:30:45 GMT  
 Use Proc with Static Variables Multiple Times
Thanks for response dpb:

Affirms my thoughts -- just don't like the solution -- difficult to
implement in current design configuration.

David

Quote:


>> I have a procedure that contains static variables.

>> This procedure can be called multiple times from a list
>> which allows different parameters to be passed to the procedure
>> with the static variables.  The return value of each call is then used to
>> update other arrays which are then processed independently of each other.

>> I need a solution to save the static values for each
>> independent call so that when the list (each call) is executed
>> the return value is correct.

> An array or if need these permanently, a corollary file.  If sizable and
> lookup is an issue, some sort of indexing/hashing scheme may be desirable.

> If, of course, my crystal ball is actually working...

> --



Fri, 19 Aug 2011 04:21:59 GMT  
 Use Proc with Static Variables Multiple Times


Quote:
> I have a procedure that contains static variables.
<...>
> I need a solution to save the static values for each
> independent call so that when the list (each call) is executed
> the return value is correct.

> Any ideas?

That sounds like abuse of static variables.  Are they 'really' static
if they need to change from one call to the next?

Looking at it from anouther perspective, static variables are
little more than module level variables that are private to a
specific routine.  You have the option of promoting those
variables to the module level, but then loose the private
scope.  Not a bad trade off, since you also get the ability
to reset, or initialize those variables because they are module
level.

One thing inherent in your problem is the need to identify
which set of 'static' variables you need to work with.

Another bit of info required is how many variables are
supposed to be static?

It would be best if you work up a small example of
the problem and post your code.  Such an example
shows the context of your problem.  Others might then
be better able to help with solutions to your specific
problem....

LFS



Fri, 19 Aug 2011 05:29:33 GMT  
 Use Proc with Static Variables Multiple Times
'Static' does not mean that the variable does not change from one call to
another.  It means that the variable is not initialized to its default value
at each invocation of the procedure.  Static variables are often used as
counters or to indicate a state.  Altering the static variable within the
procedure is not a form of abuse.

Making the static variable private to the procedure ensures that it
accurately reflacts whatever it is reporting about that procedure becasue it
is updated only within the procedure.  To 'promote' it to a module-level
variable breaks that connection and renders it unreliable for its purpose.


Quote:


>> I have a procedure that contains static variables.
> <...>

> That sounds like abuse of static variables.  Are they 'really' static
> if they need to change from one call to the next?

> Looking at it from anouther perspective, static variables are
> little more than module level variables that are private to a
> specific routine.  You have the option of promoting those
> variables to the module level, but then loose the private
> scope.  Not a bad trade off, since you also get the ability
> to reset, or initialize those variables because they are module
> level.

> One thing inherent in your problem is the need to identify
> which set of 'static' variables you need to work with.

> Another bit of info required is how many variables are
> supposed to be static?

> It would be best if you work up a small example of
> the problem and post your code.  Such an example
> shows the context of your problem.  Others might then
> be better able to help with solutions to your specific
> problem....

> LFS



Sun, 21 Aug 2011 14:52:45 GMT  
 Use Proc with Static Variables Multiple Times
<altered for context>


Quote:
> > That sounds like abuse of static variables.  Are they 'really' static
> > if they need to change from one call to the next?
> 'Static' does not mean that the variable does not change from one call to
> another.

I see your reading more into a message than was originally posted.  I didn't
make such a statement, even if that is the practical response.

Quote:
> > >This procedure can be called multiple times from a list
> > >which allows different parameters to be passed to the procedure
> > >with the static variables.<...>
> > >I need a solution to save the static values for each
> > >independent call so that when the list (each call) is executed
> > >the return value is correct.
> > That sounds like abuse of static variables. <...>
> Altering the static variable within the procedure is not a form of abuse.

Is that how it sounds to you?  It sounded to me like he was passing
in parameters from a list and wanted a set of associated static variables
for each set of parameters.

On second read, perhaps what the OP wanted was a way to associate
a persisted set of values for each independent call.

None the less, you could have focused more on the original post instead
of being overly critical to a reply....

<shrug>
LFS



Sun, 21 Aug 2011 16:54:12 GMT  
 Use Proc with Static Variables Multiple Times



Quote:
> <altered for context>


>> > That sounds like abuse of static variables.  Are they 'really' static
>> > if they need to change from one call to the next?

>> 'Static' does not mean that the variable does not change from one call to
>> another.

> I see your reading more into a message than was originally posted.  I
> didn't
> make such a statement, even if that is the practical response.

>> > >This procedure can be called multiple times from a list
>> > >which allows different parameters to be passed to the procedure
>> > >with the static variables.<...>
>> > >I need a solution to save the static values for each
>> > >independent call so that when the list (each call) is executed
>> > >the return value is correct.

>> > That sounds like abuse of static variables. <...>

>> Altering the static variable within the procedure is not a form of abuse.

> Is that how it sounds to you?  It sounded to me like he was passing
> in parameters from a list and wanted a set of associated static variables
> for each set of parameters.

> On second read, perhaps what the OP wanted was a way to associate
> a persisted set of values for each independent call.

> None the less, you could have focused more on the original post instead
> of being overly critical to a reply....

> <shrug>
> LFS

Agree, static variables IMO are to be used inside a proc. for the internal
working in that proc. If the 'static' variables are to change reflecting the
state of something outside the proc., then create them static in the caller
routine(s) and pass the values to the called routine. Dim them 'static'
where they belong.

/Henning



Sun, 21 Aug 2011 19:16:04 GMT  
 Use Proc with Static Variables Multiple Times
I am reading your reply exactly as it was posted:
"Are they 'really' static if they need to change from one call to the next?"

Static variables will frequently change from one call to the next if they
need to reflect some change in the state of the procedure where the new
value is in some way dependant on the previous value - ie, where the
previous value needs to be available when the procedure next executes.  This
is perfectly proper use of a static variable and does not amount to 'abuse'.

OP is using a procedure with static variables and varying parameters.  The
static variables are probably being used to record certain details of prior
processing of related records.  In this case the value of these static
variables must be returned to the caller in order to facilitate the further
procvessing of the list item, and the calling routine needs some way to save
this data.  The question was how to do this, and the only reasonable
approach seems to be to keep some form of matched array.

Maintaining the variables outside the procedure, as per your suggestion,
would be poor practice for the reasons I gave, but in any case is not
relevant to OP's problem of associating the value of the variable at the
time of calling the procedure with the list item that made the call.

I can't improve on the suggestion of a matched array or file, but it is
appropriate to comment on your misstatements about proper use of static
variables, and that "the option of promoting those variables to the module
level" is somehow a worthwhile change.


Quote:
> <altered for context>


>> > That sounds like abuse of static variables.  Are they 'really' static
>> > if they need to change from one call to the next?

>> 'Static' does not mean that the variable does not change from one call to
>> another.

> I see your reading more into a message than was originally posted.  I
> didn't
> make such a statement, even if that is the practical response.

>> > >This procedure can be called multiple times from a list
>> > >which allows different parameters to be passed to the procedure
>> > >with the static variables.<...>
>> > >I need a solution to save the static values for each
>> > >independent call so that when the list (each call) is executed
>> > >the return value is correct.

>> > That sounds like abuse of static variables. <...>

>> Altering the static variable within the procedure is not a form of abuse.

> Is that how it sounds to you?  It sounded to me like he was passing
> in parameters from a list and wanted a set of associated static variables
> for each set of parameters.

> On second read, perhaps what the OP wanted was a way to associate
> a persisted set of values for each independent call.

> None the less, you could have focused more on the original post instead
> of being overly critical to a reply....

> <shrug>
> LFS



Mon, 22 Aug 2011 08:32:08 GMT  
 Use Proc with Static Variables Multiple Times
Mr. Hahn, Mr. Henning, Mr. Serflaten, and Mr Riemersma.

Thanks for all the input and debate.

For the record this sums up what I was after::

Quote:
> OP is using a procedure with static variables and varying parameters.  The
> static variables are probably being used to record certain details of
> prior processing of related records.  In this case the value of these
> static variables must be returned to the caller in order to facilitate the
> further procvessing of the list item, and the calling routine needs some
> way to save this data.  The question was how to do this.

------------------------

I resolved the problem by including another variable in my UDT array which
is used by the procedure where the static resides.  This variable becomes
the placeholder for the ending static variable value between calls  as each
listbox line is processed in the loop.


Quote:
>I am reading your reply exactly as it was posted:
> "Are they 'really' static if they need to change from one call to the
> next?"

> Static variables will frequently change from one call to the next if they
> need to reflect some change in the state of the procedure where the new
> value is in some way dependant on the previous value - ie, where the
> previous value needs to be available when the procedure next executes.
> This is perfectly proper use of a static variable and does not amount to
> 'abuse'.

> OP is using a procedure with static variables and varying parameters.  The
> static variables are probably being used to record certain details of
> prior processing of related records.  In this case the value of these
> static variables must be returned to the caller in order to facilitate the
> further procvessing of the list item, and the calling routine needs some
> way to save this data.  The question was how to do this, and the only
> reasonable approach seems to be to keep some form of matched array.

> Maintaining the variables outside the procedure, as per your suggestion,
> would be poor practice for the reasons I gave, but in any case is not
> relevant to OP's problem of associating the value of the variable at the
> time of calling the procedure with the list item that made the call.

> I can't improve on the suggestion of a matched array or file, but it is
> appropriate to comment on your misstatements about proper use of static
> variables, and that "the option of promoting those variables to the module
> level" is somehow a worthwhile change.



>> <altered for context>


>>> > That sounds like abuse of static variables.  Are they 'really' static
>>> > if they need to change from one call to the next?

>>> 'Static' does not mean that the variable does not change from one call
>>> to
>>> another.

>> I see your reading more into a message than was originally posted.  I
>> didn't
>> make such a statement, even if that is the practical response.

>>> > >This procedure can be called multiple times from a list
>>> > >which allows different parameters to be passed to the procedure
>>> > >with the static variables.<...>
>>> > >I need a solution to save the static values for each
>>> > >independent call so that when the list (each call) is executed
>>> > >the return value is correct.

>>> > That sounds like abuse of static variables. <...>

>>> Altering the static variable within the procedure is not a form of
>>> abuse.

>> Is that how it sounds to you?  It sounded to me like he was passing
>> in parameters from a list and wanted a set of associated static variables
>> for each set of parameters.

>> On second read, perhaps what the OP wanted was a way to associate
>> a persisted set of values for each independent call.

>> None the less, you could have focused more on the original post instead
>> of being overly critical to a reply....

>> <shrug>
>> LFS



Wed, 24 Aug 2011 21:02:31 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Call a proc using a string variable for the proc name

2. MDI - Using Proc multiple Children or Multiple Calls

3. Static variable not static enough ???

4. How to declare variables set in form proc but used after form unloaded

5. How do I select a constant time range from a variable time range using VBA

6. Selecting multiple rows using variables

7. using a variable in multiple forms

8. How do I declare a static global variable ?

9. Static Class and Variable

10. Declaring variables as Static vs Private/Public -- XML

11. Static variables are not supported...

 

 
Powered by phpBB® Forum Software