Variable inside a class

fabiomarcos

Member
Joined
Oct 21, 2008
Messages
5
Programming Experience
Beginner
Hi,

I need to make the value of the public variable bellow accessed by all sub´s... how to do this ?

VB.NET:
Public class cTest

    public myVar as string

    public sub mySub1()
         myVar = "Hello "        
    end sub

    public sub mySub2()
         myVar =+ "World"
    end sub

    public function mySub3() as string
       return myVar
    end function
end class

The return of function show be "Hello Word" after calling mySub1 and mySub2

Thank´s
 
Ahm...and it's not working?

VB.NET:
Public class cTest
    Private myVar As String = String.Empty 'it's declared within the class.makes it accessable for all functions

    public sub mySub1()
         Me.myVar = "Hello "        
    end sub

    public sub mySub2()
         Me.myVar &= "World"
    end sub

    public function mySub3() as string
       mySub3 = Me.myVar
    end function
end class

Bobby
 
Unfortunatelly no...
Usage:

VB.NET:
 Dim clTest as New cTest
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If (Not Page.IsPostBack) Then
            clTest.mySub1()
            clTest.mySub2()
        End If

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        label1.Text = clTest.mySub3()  <- Should appear Hello World here...
    End Sub

Any ideas ?

Thank´s
 
Thread moved to ASP.NET section, a unique environment that is stateless, this means each time your server code is called a new request was made and everything starts over. Various tricks is used to preserve state each time a web client wants an update, here's a start for you about that: ASP.NET State Management Overview
 
Back
Top