declaring global variables


Partial Class _Default
    Inherits System.Web.UI.Page
    Private nvers As Integer
    Private nid As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
             nid = 0
        End If
    End Sub
    Protected Sub NewVer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewVer.Click
        nid=1
        nvers=3
    End Sub
    Protected Sub NewOp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewOp.Click
        nid=2
        nvers=3
    End Sub
Protected Sub myWizard_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs)
      
        If nid =1 Then
            'Do something
        End If
    End Sub
But although I click NewVer_click(which sets nid to 1) in myWizard_FinishButtonClick nid remains 0
How should I declare my variable nid to be global?
thanks
0
engnouna
3/22/2007 9:22:12 AM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 5 Replies
👁️‍🗨️ 538 Views



Try this:
Application["NidVar"] = nid to save the nid value
 and cast nid = (int) Application["NidVar"] to get the value back.
 

Trirange Portal Server Online Demo
http://www.trirange.com/demo/
0
upgView
3/22/2007 10:43:55 AM

I think that cast nid = (int) Application["NidVar"]  is a C# code
can you give it to me in VB code?
thanks
0
engnouna
3/22/2007 10:49:37 AM

Global variables cannot be considered this way in asp.net, since it works a stateless manner. Any private variables that you set in your _Default class will be lost at each page roundtrips.
I think you may want to read a little bit more about the way ASP.NET is processed code behind pages.
You may want to use Session or ViewState variable capabilities instead. Session is a collection that is used to store values for a authenticated user. Try this instead of private nid declaration :
private int nid
{
    get { 
            if (Session["nid"] == null)
                return 0;
            else
                return (int) Session["nid"];
        }
    set {
            Session["nid"] = value;
        }
}

            
I'm sorry, this is C#. I do not have Visual Basic installed on my box. The conversion is easy.
Good luck- 

          
0
CtrlBr34k
3/22/2007 10:49:40 AM

Thanks to this, here is the VB version :
Private Property nid() As Integer
        Get 
            If Session("nid") Is Nothing Then
                Return 0
            Else 
                Return CType(Session("nid"), Integer)
            End If
        End Get
        Set (ByVal Value As Integer) 
            Session("nid") = value
        End Set
End Property

            
Application or Session variable ? That depends if the process you intend to use in your Web application is application dependant or user dependant.

            
Good luck

          
0
CtrlBr34k
3/22/2007 1:28:45 PM

Partial Class _Default
    Inherits System.Web.UI.Page
    Private nvers As Integer
    Private nid As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
             Application["NID"] = 0
        End If
    End Sub
    Protected Sub NewVer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewVer.Click
        Application["NID"] =1
        nvers=3
    End Sub
    Protected Sub NewOp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewOp.Click
        Application["NID"] =2
        nvers=3
    End Sub
Protected Sub myWizard_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs)
      
        If CInt(Application["NID"])Then
            'Do something
        End If
    End Sub
 
If you want your variable scope is over all your application then use Application, else you can use Session. CInt is used to cast to Int type.

Trirange Portal Server Online Demo
http://www.trirange.com/demo/
0
upgView
3/22/2007 1:29:22 PM
Reply: