Home All Groups Group Topic Archive Search About

Use Proc with Static Variables Multiple Times

Author
1 Mar 2009 3:47 PM
David
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?

Author
1 Mar 2009 4:15 PM
dpb
David wrote:
> 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...

--
Author
1 Mar 2009 8:21 PM
David
Thanks for response dpb:

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

David

Show quoteHide quote
"dpb" <n***@non.net> wrote in message news:goeced$kfg$1@aioe.org...
> David wrote:
>> 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...
>
> --
Author
1 Mar 2009 6:30 PM
Bob Riemersma
Show quote Hide quote
"David" <dw85745***@earthlink.net> wrote in message
news:OZM6CUomJHA.1184@TK2MSFTNGP04.phx.gbl...
>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.
Author
1 Mar 2009 9:29 PM
Larry Serflaten
"David" <dw85745***@earthlink.net> wrote
> 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
Author
4 Mar 2009 6:52 AM
James Hahn
'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.

Show quoteHide quote
"Larry Serflaten" <serfla***@usinternet.com> wrote in message
news:utVltSrmJHA.1168@TK2MSFTNGP05.phx.gbl...
>
> "David" <dw85745***@earthlink.net> wrote
>> 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
>
>
Author
4 Mar 2009 8:54 AM
Larry Serflaten
<altered for context>

"James Hahn" <jh***@yahoo.com> wrote

> > 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
Author
4 Mar 2009 11:16 AM
Henning
Show quote Hide quote
"Larry Serflaten" <serfla***@usinternet.com> skrev i meddelandet
news:ucmbmaKnJHA.5980@TK2MSFTNGP06.phx.gbl...
> <altered for context>
>
> "James Hahn" <jh***@yahoo.com> wrote
>
>> > 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
Author
5 Mar 2009 12:32 AM
James Hahn
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.

Show quoteHide quote
"Larry Serflaten" <serfla***@usinternet.com> wrote in message
news:ucmbmaKnJHA.5980@TK2MSFTNGP06.phx.gbl...
> <altered for context>
>
> "James Hahn" <jh***@yahoo.com> wrote
>
>> > 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
>
>
Author
7 Mar 2009 1:02 PM
David
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::

> 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.



Show quoteHide quote
"James Hahn" <jh***@yahoo.com> wrote in message
news:O0NqinSnJHA.500@TK2MSFTNGP06.phx.gbl...
>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.
>
> "Larry Serflaten" <serfla***@usinternet.com> wrote in message
> news:ucmbmaKnJHA.5980@TK2MSFTNGP06.phx.gbl...
>> <altered for context>
>>
>> "James Hahn" <jh***@yahoo.com> wrote
>>
>>> > 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
>>
>>
>