Results 1 to 8 of 8

Thread: Variables declarations: What's the difference? (RESOLVED)

  1. #1

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

    Question Variables declarations: What's the difference? (RESOLVED)

    What's the difference between it:

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.     Dim myVar As String = "Some text" 'Variable declaration here
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         MsgBox(myVar)
    6.     End Sub
    7. End Class
    And it:

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim myVar As String = "Some text" 'Variable declaration here
    5.         MsgBox(myVar)
    6.     End Sub
    7. End Class
    ?

    Thanks.
    Last edited by AlvaroF1; Aug 20th, 2003 at 08:01 AM.

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    the first one is declared as a class level variable, so it can be used throughout the entire class.

    The second one is declared within a sub, so it is a block level variable and can only be used within the block or sub it was declared in.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The scope. The first one enables the variable to be used anywhere in the form. The second one means the variable is only valid within the Button click event.

  4. #4
    Addicted Member The Phoenix's Avatar
    Join Date
    Aug 2003
    Location
    With my wife
    Posts
    142
    By way of example, here's a code snippet from a project a did last semester in college:

    VB Code:
    1. '''''
    2.     'The Timer variable for the progress bar
    3.     Private pinttime As Integer = 0
    4.  
    5.     Private Sub tmrSplash_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSplash.Tick
    6.         pgrProgress.Value += 1
    7.         pinttime = pinttime + 1
    8.         '''''
    9.         'When the timer hits the maximum value of
    10.         'the progress bar, close the form
    11.         If pinttime = pgrProgress.Maximum Then
    12.             Me.Close()
    13.             tmrSplash.Dispose()
    14.         End If
    15.     End Sub

    If the "Private pinttime As Integer = 0" was in the "Private Sub" section, the value would be reset to 0 every time the timer ticked, but since I want the timer to keep increasing, I put the variable before the procedure.

    They are correct, I just thought an example might be helpful.
    Take my love
    Take my land
    Take me where I cannot stand
    I don't care, I'm still free
    You can't take the sky from me...

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    I'd suggest reading a book. Learning the scope of things is very helpful in all languages.

    Variables in functions in VB, C++, and most other languages, they are local to that function, it's in that function's scope.

  6. #6

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    What about CPU and memory usage?


    Thanx.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by AlvaroF1
    What about CPU and memory usage?


    Thanx.
    What about them? Learning and using the proper scope will improve memory usage.

  8. #8
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by AlvaroF1
    What about CPU and memory usage?


    Thanx.

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