sharing variables between methods

T

tfsmag

here is some test code i've set up trying to figure out how to share
variables between two different methods.

__________________________________________________

Public Class test
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "


#End Region

Public Shared testvar As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
testing.Text = testvar
End Sub

Sub makevar()
testvar = "this is a test"
DataBind()
End Sub

End Class

__________________________________________________

this is not working... can someone tell me what i'm doing wrong or if
this is even possible?

thanks,
Jeff
 
G

Guest

Web application is stateless.So If you want to get testvar variable in your
page_load method,You must set testvar's value before using.

or you can declare this varable is static.

or set testvar in your oninit method.It look like this:
protected override void OnInit(EventArgs e){
.....
testvar = "this is a test";
}
 
S

Scott Allen

You'll want to avoid the Shared keyword then - it will share the
variable among all instances of your class (only one testvar in the
entire application no matter how many test classes exist).

From a high level point of view, what is it you need to accomplish?
 
T

tfsmag

scott,

what i'm trying to accomplish is in a page where i have a calendar
control... on the DayRenderEventArgs method i've created i want to
capture the current month and year shown, and set a variable in that
method that i can carry over to the bindgrids() method so i can pull
all records within a daterange as determined by the 'month shown' into
a datagrid.
 
G

Guest

I think you should use viewstate for your question.
For example:
private string testvar{
set{
this.ViewState["testvar"] = value;
}
get{
return (string)this.ViewState["testvar"];
}
}
 
S

Scott Allen

You could take the Shared keyword out - the variable approach should
work for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top