Results 1 to 3 of 3

Thread: Where may Static statements appear?

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    43

    Where may Static statements appear?

    I know they can appear in local procedure's, but can they appear in a "block of code" as well?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Where may Static statements appear?

    When you tried it for yourself, what happened? If it worked then the answer is yes, otherwise it's no.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Where may Static statements appear?

    What is a 'block of code'? Do you mean something like "within an If statement", or "in a file".

    The simplest solution is to try it, as JM stated, but it would also be good to reason through it. If your meaning is the former of those two options, in what way would a variable declared in a local procedure be different when located within such a block? The answer is that both are local variables, so there is no difference other than the scope of the variable. Therefore, since you already know that they can appear in local procedures, then you know that they can appear on local variables, and a variable declared at the top of a procedure is no different from one declared later in the procedure from that perspective.

    Alternatively, where would a static variable be located? It had best not be created within the stack frame of a procedure, like other local variables, because if it was, then it would be destroyed when the stack frame gets destroyed on function exit. Therefore, the static has to be located on the heap, as that's the other choice. Unfortunately, that would suggest that the static variable can be ANYWHERE, but we know (by looking at where local variables can be declared) that the compiler can enforce scoping issues regardless of where a variable is declared, in which case you'd need to check.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width