Results 1 to 10 of 10

Thread: public (global) variables

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    public (global) variables

    when you have a module that is used to house public variables for the entire project to use, when are those initialized? are they done before formLoad or after? from what i can TELL, it's after.

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    hi,

    Don't know where you got that idea.

    Try declaring a public variable in the module, referencing it in the first line of the formLoad event and putting a breakpoint on it.

    You will see it is recognised.

    Or have I completely misunderstood you??
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Don't know if it makes a difference, but are you starting your app from a form or sub main?

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    "Don't know if it makes a difference, but are you starting your app from a form or sub main?"


    No, it makes no difference.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    it's starting from a form.

    I meant that I'm seeing the variables get initialized BEFORE the load (oops ). I have variables in the module and then reference them in LOAD. No problems. What I was really curious about is whether or not I should just go ahead and declare (example)

    VB Code:
    1. public x as An_Object = New An_Object
    2.  
    3. 'OR
    4.  
    5. public x as An_object

    I'm simply gonna use that one 'x' over and over so i've set it to a new An_object in the declaration.

    Just curious if that's the way to go.
    Last edited by Andy; Apr 8th, 2004 at 10:24 AM.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "I meant that I'm seeing the variables get initialized BEFORE the load. "

    That's the opposite of what you first posted

    It appears that all the declarations in the project are activated before any form or module loads.

    "public x as An_Object = New An_Object

    'OR

    public x as An_object"

    This is a totally different subject. Either will work but using NEW is more VB.NET like.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Now, we all agree that " all the declarations in the project are activated before any form or module loads" (if in a module, obviously, not in a Form not yet loaded) and that is a good result, I think

    If we analize the second problem:

    --------------------------------------------------------------------------------
    public x as An_Object = New An_Object

    'OR

    public x as An_object

    The difference is that "public x as An_object" declare the object but it does not instance it, so if you will try to refere to the object you will have a run time error.
    To make your object fully usable, you must use, for example:

    Dim Form1 as FormGoofy
    Form1=New FormGoofy

    or

    Dim Form1 as NEW Formgoofy


    Good job
    Live long and prosper (Mr. Spock)

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Alextyx,

    I assume you have not tried out what you are saying

    "Now, we all agree that " all the declarations in the project are activated before any form or module loads" (if in a module, obviously, not in a Form not yet loaded) "


    We are NOT all agreed

    You are contradicting yourself! if you amend
    "not in a Form not yet loaded"
    to
    "not in a Form not yet being loaded"

    then it makes sense.

    Again

    "The difference is that "public x as An_object" declare the object but it does not instance it, so if you will try to refere to the object you will have a run time error."

    NO!!!

    You can't refer to it because it is EMPTY.

    When you fill it with an instanced object, you will be able to refer to the properties of that instanced object .
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Hi taxes,

    This is the runtime error I obtain, in italian version:

    Eccezione non gestita di tipo "System.NullReferenceException" in GIN.exe

    Informazioni aggiuntive: Riferimento a un oggetto non impostato su un'istanza di oggetto.



    The declaration in a public module is:

    ' ----------------- Dichiarazioni di Form -----------------------------------
    Public FrmReg As New FrmRegistrazioni
    Public FrmNota As New FrmEditaPrimaNota
    Public FrmDaCancellare As FrmCercaRegistrazioni

    The last line is the 'bad declaration' the others work properly, I assure!


    The line in which the error is generated is among the lines of the load routine and it is, obviously: "GIN.FrmDaCancellare.Show()"


    ' --------- vvvvvvvvvvvvvv EVENTI vvvvvvvvvv -------------------

    Private Sub FrmRientri_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
    GIN.FrmDaCancellare.Show()
    End Sub

    As you can see, I can refer to an object declared and I can run the program and I can access to its methods, but I will have a run time error. I intended this, but perhaps I can't explain well. I guess you can also understand what I intended about form not loaded, or not? I know english from school and for electronics, that is my main occupation and surely I make a lot of mistake.I try to write in a correct way, but, try to be patience with me, please!
    Live long and prosper (Mr. Spock)

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    I think this thread has moved completely away from the original post and I have difficulty in understanding the last post. Better let it lie.

    Actually, re-reading the posts one of my responses missed the mark

    I was referring to creating a container (if that is the right word!) variable to contain instantiated objects as required.

    See Andy's "I'm simply gonna use that one 'x' over and over so i've set it to a new An_object in the declaration"

    I did not notice the AN_ bit in front of OBJECT.

    My apologies on this point.
    Last edited by taxes; Apr 7th, 2004 at 10:27 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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