Global Variables

kumarangopi

Member
Joined
Apr 12, 2008
Messages
14
Programming Experience
Beginner
Iam doing a small form where two push buttons.one for accept data into structure,second display data.I have used structures with following structure

structure student
dim sname as string
dim sage as integer
end structure

Since I want to accept more than 1 students information, I went to arrays.The key is that iam accepting the array index from the user like "How many students information you want to enter" and then iam declaring structure array like this:

dim sstruct(num) as student
'where num is the index accepted from user

The above declaration is in button1.When I go to button2 for display,I cannot use num value in for loop since it is local to button1.How to do this now?I dont want the user to enter the index value while form load.Only when user clicks first button,he should enter index value.

Hope my question is clear
 
At the top of the code window declare a variable as Private, for example:

VB.NET:
Option Explicit On
Option Strict On

Public Class Form1

  Private m_NumStudents As Integer

  Private Sub Button1_Click (...) Handles Button1.Click
    'Set m_NumStudents to somethere here
  End Sub

  Private Sub Button2_Click (...) Handles Button2.Click
    'Use m_NumStudents here
  End Sub
 
Thanks for your reply

Thanks for your immediate reply.I did as you say

VB.NET:
Option Explicit On
Option Strict On

Public Class Form1


    Structure student
        Dim sname As String
        Dim sage As Integer
    End Structure
    Dim sstruct(num) As student
    Dim i As Integer
    Dim num As Integer


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        [B]num = InputBox("How many Students you want to enter:", "Students Info")[/B]
        Dim sstruct(num) As student
        For i = 0 To num - 1
            With sstruct(i)
                .sname = InputBox("Enter Student Name", "Record: " & i + 1)
                .sage = InputBox("Enter Student Age", "Record: " & i + 1)
            End With
        Next
    End Sub

Errror is throwing at num=inputbox("how many students...")
Error desc:eek:ption strict on disallows implicit conversions from string to integer

I feel some problem with conversion.I hope u understood that what i want to do,i want to declare array with user typed index which will be available for all the events in the form.

Thank you
 
Last edited by a moderator:
You'll need to convert the number coming out of the InputBox to an actual number:
VB.NET:
num = CInt(InputBox("How many Students you want to enter:", "Students Info"))
Keep in mind that if the user doesn't type in an integer, it'll cause your program to crash
 
Thanks for your reply.I did it, now the error is not coming.But I have a problem with the logic
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        num = CInt(InputBox("How many Students you want to enter:", "Students Info"))

        Dim sstruct(num) As student
        For i = 0 To num - 1
            With sstruct(i)
                .sname = InputBox("Enter Student Name", "Record: " & i + 1)
                .sage = CInt(InputBox("Enter Student Age", "Record: " & i + 1))
            End With
        Next
        i = 0
        For i = 0 To num
            MsgBox("Student Name: " & sstruct(i).sname & vbCrLf & "Student Age: " & sstruct(i).sage)
        Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        i = 0
        For i = 0 To num
            MsgBox(i)
            MsgBox(num)
            MsgBox("Student Name: " & sstruct(i).sname & vbCrLf & "Student Age: " & sstruct(i).sage)
        Next
        'sstruct(3).sname.
 End Sub

The 2nd for loop(for displaying data) works well in button1 procedure but not in button2 procedure.I dont know why?Does option explicit/strict has impact on it?
 
Last edited by a moderator:
You've declared sstruct(num) twice, once outside and once inside the Button1.Click Sub procedure. What has then happened is that you've stored all the information in the "inside" variable, but when you run Button2.Click, it accesses the "outside" variable.

If you want to access variables from more than one Sub procedure, you have to declare the variable outside any procedures.

So ...

VB.NET:
Structure student
        Dim sname As String
        Dim sage As Integer
    End Structure
    Dim sstruct(num) As student   <---- LEAVE THIS LINE IN
    Dim i As Integer
    Dim num As Integer


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        num = CInt(InputBox("How many Students you want to enter:", "Students Info"))

        Dim sstruct(num) As student   <-------- REMOVE THIS LINE
        For i = 0 To num - 1
            With sstruct(i)
                .sname = InputBox("Enter Student Name", "Record: " & i + 1)
                .sage = CInt(InputBox("Enter Student Age", "Record: " & i + 1))
            End With
        Next
        i = 0
        For i = 0 To num
            MsgBox("Student Name: " & sstruct(i).sname & vbCrLf & "Student Age: " & sstruct(i).sage)
        Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        i = 0
        For i = 0 To num
            MsgBox(i)
            MsgBox(num)
            MsgBox("Student Name: " & sstruct(i).sname & vbCrLf & "Student Age: " & sstruct(i).sage)
        Next
        'sstruct(3).sname.
 End Sub
 
Yeah Inertia,You are true.I tried doing it how you said.But the problem is that Iam getting value of array index in button1.click i.e.num->"How many students you want to enter" and then Iam declaring structure array sstruct(num).If I remove the sstruct(num)declaration line in form.declarations,sstruct in button2click says "variable not declared" since the declaration is done in button1.If I remove second line which is in button1.clickl,run time error occurs stating "index is out of bounds".Hpe you understood.I think this cannot be solved.Tell me your comments
 
I think this cannot be solved.Tell me your comments
Students read books. You are better off reading a VB.Net beginner book than asking about this in forums. Spending 24 hours reading might get you through ALL the basics of VB.Net, learning in a structured manner get you through more faster. In the same period of time someone might have bothered telling you to lookup the Redim statement. Do work through the books practical exercises also, it is an important part of getting the information settled (if you didn't learn it on first read you have to find info and read again), it triggers creativity and it challenges you to combine different information to solve the problem.
 
Back
Top