Persistent variable values...

  • Thread starter Thread starter AMeador
  • Start date Start date
A

AMeador

I can't remember and am having a hard time finding a way to make a
variable maintain its value between procedure calls. I have a counter
in a procedure that I want to retain its value between call so I can
see how ofter this procedure is called. How do you define the variable
to do this? I read it somewhere, and I know it's not that complex to
do, I just can't find it now that I want it.
 
You have two choices.

1. The most common case is that there is a separate counter for each
object that you create from the class that defines the procedure. For
example, if the class is Book, then each Book has its own, independent
counter. In this case the counter becomes a private member in the class
and forms part of its state.

2. The less common case is that you want one global counter for your
entire program. For example, if the class that contains the procedure
is Book, then there is one, common counter for all Books. In this case
you make the counter a "static" member of the Book class.

So... do you want each instance of your class to count the number of
times that the procedure was called on _that instance_, or do you want
one grand count for the entire program?
 
Both methods would be good. From a follow-up that another user listed
for me, I see how to get the variable to do this, but it is not clear
to me if this is per instance or shared by all instances.
 
If you declare the class member as "private static" then it will be
shared by all instances.

If you simply declare it as "private" then it will be per-instance.
 
Back
Top