Question getting a variable from sub

mstpavan

New member
Joined
Mar 24, 2010
Messages
1
Programming Experience
1-3
I'm stuck up with this code...How do I access the sContents variable in loadBoard() sub to LoadTable() sub

here is the code that might be useful....
VB.NET:
Sub loadBoard()
        Dim oFile As FileStream
        Dim oReader As StreamReader = Nothing
        Dim sContents As String
        Dim count As Integer = 0
        Try
            oFile = New FileStream("some/path/to/the/text/file", FileMode.Open, FileAccess.Read)
            oReader = New StreamReader(oFile)
            sContents = oReader.ReadToEnd
        Catch ex As Exception
            sContents = ex.Message
        Finally
            If oReader IsNot Nothing Then
                oReader.Close()
            End If
        End Try
        Console.WriteLine(sContents)
    End Sub

    Sub Loadtable(ByRef Name() As String, ByRef Id() As Integer, ByRef Phone() As Integer, ByRef Connect() As Double, ByRef sContents As String, ByRef norows As Integer)

        Dim temp1 As Integer
        Dim temp2 As Double
        Dim no As Integer = 0
        Do While sContents <> "0" And no <= max
            Name(no) = sContents
            sContents = Console.ReadLine()
            temp1 = Integer.Parse(sContents)
            Id(no) = temp1
            Console.WriteLine("id:", Id(no))
            sContents = Console.ReadLine()
            temp1 = Integer.Parse(sContents)
            Phone(no) = temp1
            sContents = Console.ReadLine()
            temp2 = Double.Parse(sContents)
            Connect(no) = temp2
            no += 1

        Loop
        norows = no
    End Sub

any help would be appreciated...
 
Last edited by a moderator:
put them as "form variables" instead of sub variables.

Instead of having it in the sub, put it under "Public Class myformname"

VB.NET:
Public Class frmMyForm
Dim sContents As String

Sub loadBoard()
        Dim oFile As FileStream
        Dim oReader As StreamReader = Nothing
        ' ~~~I have removed Dim sContents As String from here ~~~
        Dim count As Integer = 0
        Try
            oFile = New FileStream("some/path/to/the/text/file", FileMode.Open, FileAccess.Read)
            oReader = New StreamReader(oFile)
            sContents = oReader.ReadToEnd
        Catch ex As Exception
            sContents = ex.Message
        Finally
            If oReader IsNot Nothing Then
                oReader.Close()
            End If
        End Try
        Console.WriteLine(sContents)
    End Sub

You then don't need to declare it ByRef and can use it in other subs on the form.
 
Back
Top