Results 1 to 5 of 5

Thread: [RESOLVED] Declaring 'global' variable in sub, or alternatives...

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    2

    Resolved [RESOLVED] Declaring 'global' variable in sub, or alternatives...

    Hi all,

    I want to declare a global variable, which I know can be done using Public Shared, but needs to be determined by a sub before that.

    This is what it currently looks like:

    Code:
    Public Sub...
        ...
        ...
        ...
        Dim getMeOutside As String = "Take me outside!"
    End Sub
    
    Public Shared globalVar As String = getMeOutside
    It doesn't need to be in this order, but I would like it to perform briefly as stated above.

    Thank you in advance.

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

    Re: Declaring 'global' variable in sub, or alternatives...

    Code:
    Public Sub...
        ...
        ...
        ...
        globalVar = "Take me outside!"
    End Sub
    
    Public Shared globalVar As String
    Why would setting a global variable any different to setting any other variable. Obviously, until you set the variable, it's value will be Nothing. That's just a fact of life: a variable doesn't have a value until you assign a value to it. Depending on the specifics of the actual situation, as opposed to this contrived one, you might be able to do something like this:
    Code:
    Public Shared Function GetGlobalValue()
        ...
        ...
        ...
        Return "Take me outside!"
    End Sub
    
    Public Shared globalVar As String = GetGlobalValue()
    In that case, that function will be called when the variable is initialised, which will happen before it's ever used, so it will definitely have a value before you try to use it.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    2

    Re: Declaring 'global' variable in sub, or alternatives...

    How would I do this when the sub handles an event?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: [RESOLVED] Declaring 'global' variable in sub, or alternatives...

    I'm not sure that you can get exactly what you want through this. A global variable will exist before the program even starts. It may not have a value, and certainly won't until you give it one, but it will exist, and it will be global. This means that code can access it at any time, it just may not have a value. If the value is only set in some event handler, then the variable will still be there, it just won't have a value until that event has happened. You can detect that the value has not been set, but you can't prevent the variable from being accessible.

    Therefore, you could have a string variable, which would be Nothing. The event handler could set that variable to something. Any other code that makes use of the variable prior to the event handler would see the variable as being Nothing. Once the event handler has run, then any code that makes use of the variable would see the variable as having whatever the event handler set it to. That's the best you can do, but there's nothing special about that. That's just a global variable. So, if that's all you need, then there is nothing special that needs to be written. If you need more than that, then I'm not understanding what you want.
    My usual boring signature: Nothing

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Declaring 'global' variable in sub, or alternatives...

    Quote Originally Posted by tkburis View Post
    How would I do this when the sub handles an event?
    The first thing to do would be to provide all the relevant information when you make your first post.

    As for the question, the answer is to use the first code snippet I posted previously. That means that, until that event is raised and the method executed, the Shared variable will have a value of Nothing, so you may have to account for that in any code that uses that variable. There's no point griping about that fact. If the value of that variable depends on user input and the user hasn't provided the input then obviously that variable can't have a value. That is, of course, unless you provide a default value explicitly, e.g. an empty String. Anything else would be magic.

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