Monday, March 26, 2012

sharing variables between methods

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,
JeffIs anything actually calling 'makevar'? It doesn't look like it.

"tfsmag" <tfsmag@.gmail.com> wrote in message
news:1118951955.575037.29800@.f14g2000cwb.googlegro ups.com...
> 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
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?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 16 Jun 2005 12:59:15 -0700, "tfsmag" <tfsmag@.gmail.com> wrote:

>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
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";
}

"tfsmag" wrote:

> 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
>
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.
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"];
}
}

"tfsmag" wrote:

> 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.
>
You could take the Shared keyword out - the variable approach should
work for you.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 16 Jun 2005 19:11:11 -0700, "tfsmag" <tfsmag@.gmail.com> wrote:

>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.

0 comments:

Post a Comment