mandelbug
mandelbug

Reputation: 1798

Declare a Global (or public?) variable on load

Just working on getting more fluent in Visual Basic over the summer so I'm still sharp for my next visual basic class. My teacher went over variables, but only in local scope.

I have looked everywhere, but I can't find exactly what I need. I'm making an alarm clock of sorts, and I have this code to populate the minute array and the hour array.

Public Class Form1 
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.clockTimer.Interval = 1 * 1000
        Me.clockTimer.Enabled = True


        Dim hourArray(0 To 11) As String
        For i As Integer = 1 To 12
            If i.ToString.Length < 2 Then
                hourArray(i - 1) = "0" & i
            Else
                hourArray(i - 1) = i
            End If
        Next

        Dim minuteArray(0 To 59) As String
        For i As Integer = 0 To 59
            If i.ToString.Length < 2 Then
                minuteArray(i) = "0" & i
            Else
                minuteArray(i) = i
            End If
        Next

        hourLabel.Text = hourArray(0)
        minuteLabel.Text = minuteArray(0)

    End Sub

(Note: The adding of the "0" is just so the clock display will have a "01" instead of a "1".)

Right now I have this going happening on the forms load (only one form in this project), but it doesn't have global or public scope. I want to be able to access the hourArray and minuteArray later in the program, but still have this happen on the forms load. How would I do this? Also, what variable scope am I describing? (ie scope for the entire form).

Thanks.

Upvotes: 0

Views: 10862

Answers (3)

Mark Hall
Mark Hall

Reputation: 54532

You are describing Module Level Scope.

From above link:

For convenience, the single term module level applies equally to modules, classes, and structures. You can declare elements at this level by placing the declaration statement outside of any procedure or block but within the module, class, or structure.

When you make a declaration at the module level, the access level you choose determines the scope. The namespace that contains the module, class, or structure also affects the scope.

Elements for which you declare Private (Visual Basic) access level are available to every procedure in that module, but not to any code in a different module. The Dim statement at module level defaults to Private if you do not use any access level keywords. However, you can make the scope and access level more obvious by using the Private keyword in the Dim statement.

So in your case I would do something like this

Public Class Form1  
    
    Dim hourArray(0 To 11) As String   'These variables are private to your Class
    Dim minuteArray(0 To 59) As String 

    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        Me.clockTimer.Interval = 1 * 1000  
        Me.clockTimer.Enabled = True 

        For i As Integer = 1 To 12 
            If i.ToString.Length < 2 Then 
                hourArray(i - 1) = "0" & i 
            Else 
                hourArray(i - 1) = i 
            End If 
        Next 

        For i As Integer = 0 To 59 
            If i.ToString.Length < 2 Then 
                minuteArray(i) = "0" & i 
            Else 
                minuteArray(i) = i 
            End If 
        Next 

        hourLabel.Text = hourArray(0) 
        minuteLabel.Text = minuteArray(0) 

    End Sub 

Upvotes: 0

Fredou
Fredou

Reputation: 20100

since your learning i'm going to tell you where to put them

but later on, you should remove the array and take the time directly and padding it with 0 when needed, all that without the array

put these between the class and the sub like this

 Public Class Form1 
     private hourArray(0 To 11) As String
     private minuteArray(0 To 59) As String
     Public Sub Form1_Loa

if you want to use these array outside, you should create a public property like

Public readonly Property hour As String()
    Get
         return hourArray
    End Get
End Property

Upvotes: 0

Saurabh R S
Saurabh R S

Reputation: 3177

You didnt tell about your class structure. Anyways. Declare the variables hourArray and minuteArray as public. Declare them in the beginning of your class and initialize them in the form load method.

EDIT Added code example.

Public Class Form1
    Dim hourArray(0 To 11) As String 'Declaration here
    Dim minuteArray(0 To 59) As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For i As Integer = 1 To 12
            If i.ToString.Length < 2 Then
                hourArray(i - 1) = "0" & i
            Else
                hourArray(i - 1) = i
            End If
        Next
        For i As Integer = 0 To 59
            If i.ToString.Length < 2 Then
                minuteArray(i) = "0" & i
            Else
                minuteArray(i) = i
            End If
        Next
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        hourArray(1) = "x"
        minuteArray(1) = "y"
    End Sub
End Class

Hope it helps !!

Upvotes: 1

Related Questions