|
-
Aug 14th, 2003, 10:19 AM
#1
Thread Starter
Addicted Member
Variables declarations: What's the difference? (RESOLVED)
What's the difference between it:
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim myVar As String = "Some text" 'Variable declaration here
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(myVar)
End Sub
End Class
And it:
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myVar As String = "Some text" 'Variable declaration here
MsgBox(myVar)
End Sub
End Class
?
Thanks.
Last edited by AlvaroF1; Aug 20th, 2003 at 08:01 AM.
-
Aug 14th, 2003, 10:21 AM
#2
Frenzied Member
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
-
Aug 14th, 2003, 10:21 AM
#3
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.
-
Aug 14th, 2003, 05:28 PM
#4
Addicted Member
By way of example, here's a code snippet from a project a did last semester in college:
VB Code:
'''''
'The Timer variable for the progress bar
Private pinttime As Integer = 0
Private Sub tmrSplash_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSplash.Tick
pgrProgress.Value += 1
pinttime = pinttime + 1
'''''
'When the timer hits the maximum value of
'the progress bar, close the form
If pinttime = pgrProgress.Maximum Then
Me.Close()
tmrSplash.Dispose()
End If
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...
-
Aug 14th, 2003, 06:20 PM
#5
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.
-
Aug 19th, 2003, 03:33 PM
#6
Thread Starter
Addicted Member
What about CPU and memory usage?
Thanx.
-
Aug 19th, 2003, 04:20 PM
#7
Originally posted by AlvaroF1
What about CPU and memory usage?
Thanx.
What about them? Learning and using the proper scope will improve memory usage.
-
Aug 19th, 2003, 04:35 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|