How to initialize a local static variable? 
Author Message
 How to initialize a local static variable?

Well, VB 7.0 will support the C++ style of variable initialization:

Dim MyVar as Integer = 23

Kris


Quote:
> Hi,

> While reading my VB textbook, I found it has code using local static
> variables like the following,

> sub foo()
> static t as Integer
> if t = 0 then  'a static integer is by default initialized to 0
>   t = 10  '10 is actually the needed initial value
> end if
> ...
> end sub

> I think that really sucks to do initialization in such a way,
> especially what if the static variable is of types other than Integer
> (so it won't happen to have a default value of zero)?

> thanks



Wed, 18 Jun 1902 08:00:00 GMT  
 How to initialize a local static variable?

Quote:

> While reading my VB textbook, I found it has code using local static
> variables like the following,

> sub foo()
> static t as Integer
> if t = 0 then  'a static integer is by default initialized to 0
>   t = 10  '10 is actually the needed initial value
> end if
> ...
> end sub

> I think that really sucks to do initialization in such a way,
> especially what if the static variable is of types other than Integer
> (so it won't happen to have a default value of zero)?

You should probably complete reading that book before starting to
criticize the language.

Static string variables are initialized to "". Look up the MSDN (or other
VB help) to see full details. Better yet, get your hands dirty; fire up VB,
declare static variables of all data types you can think of, check VB help
to see if you remembered all built-in VB data types, run the code and see the
values of the variables the first time execution enters the routine.

Do you have a specific case with a specific data type?

BTW, all numeric data types are initialized to 0, not just integers.

--
MikeC

Please reply to the group.



Wed, 18 Jun 1902 08:00:00 GMT  
 How to initialize a local static variable?
Hi,

While reading my VB textbook, I found it has code using local static
variables like the following,

sub foo()
static t as Integer
if t = 0 then  'a static integer is by default initialized to 0
  t = 10  '10 is actually the needed initial value
end if
...
end sub

I think that really sucks to do initialization in such a way,
especially what if the static variable is of types other than Integer
(so it won't happen to have a default value of zero)?

thanks



Wed, 18 Jun 1902 08:00:00 GMT  
 How to initialize a local static variable?

Quote:

> Then, usually how do you do initialization for static variables in VB?
> I think the following is one way to go,

> Sub foo()
> Static aStaticVar as Integer  'or some other type
> Static entered as Boolean

> if entered = False then  'False is the default value to a Boolean variable
>   aStaticVar = 10  'needed initial value is 10
>   entered = True  'foo() has been entered before
> end if
> ....
> aStaticVar = 0  'now having got a value of 0 won't hurt
> ...
> End Sub

> Please tell me whether the way above is okay or not?

That works.

--
MikeC

Please reply to the group.



Wed, 18 Jun 1902 08:00:00 GMT  
 How to initialize a local static variable?

Quote:


> > sub foo()
> > static t as Integer
> > if t = 0 then  'a static integer is by default initialized to 0
> >   t = 10  '10 is actually the needed initial value
> > end if
> > ...
> > end sub

> > I think that really sucks to do initialization in such a way,
> > especially what if the static variable is of types other than Integer
> > (so it won't happen to have a default value of zero)?

> You should probably complete reading that book before starting to
> criticize the language.

Oops! I did criticize VB, I confess, because it doesn't have this or
that feature that other languages I've learned already have years ago.
Actually, what I'm mostly unsatisfied is that the book should point
out this limitation and then suggest a way to go.
I hope VB can, as someone's told me in this newsgroup, asap support it.

Then, usually how do you do initialization for static variables in VB?
I think the following is one way to go,

Sub foo()
Static aStaticVar as Integer  'or some other type
Static entered as Boolean

if entered = False then  'False is the default value to a Boolean variable
  aStaticVar = 10  'needed initial value is 10
  entered = True  'foo() has been entered before
end if
....
aStaticVar = 0  'now having got a value of 0 won't hurt
...
End Sub

Please tell me whether the way above is okay or not?



Wed, 18 Jun 1902 08:00:00 GMT  
 How to initialize a local static variable?

     Glenn,
     Alternatively, you could pass an [Optional] Argument into your
     function to forcibly reset everything.  I use this method when I
     need to use this routine more than once during the run of an
     application, as in

         Public Sub foo( _
           Optional ByVal ReStart As Boolean = False _
         )
         Static iT As Integer

         If ReStart Then
             iT = 143 ' Whetever initial value is required
             Exit Sub
         End If

         ... Rest of Sub code ...

         End Sub

     Just remember to 'Restart' foo before you use it for the first time,
     as in

         Call foo( True )

HTH,
    Phill  W.

Quote:

> > sub foo()
> > static t as Integer
> > if t = 0 then  'a static integer is by default initialized to 0
> >   t = 10  '10 is actually the needed initial value
> > end if
> > ...
> > end sub



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How do I initialize static variables?

2. Too many local, non-static variables

3. Would like to get value from local static variable

4. Static variable not static enough ???

5. Better method of detecting static array initialized?

6. Initializing a static

7. Initialize a Static Array

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