declare a variable globally?

mohammedq

New member
Joined
Sep 5, 2007
Messages
3
Programming Experience
Beginner
hello,

i am a total beginner...and would like to apologize in advance for any "ridiculous" questions.

i am working in VS 2005 and in my code behind file (class)...i have several procedures...what i would like to now is to be able to declare a variable globally so that its value can be accessed by any of the procedures. how can i do this please? any suggestion is greatly appreciated. thank you!


mohammed
 
hi

yes you can.

For a variable to be accessible globaly throughout all the program - you can declare one inside a "Module" as Public

Example Public myName as String

IF you want to keep the variable available inside a particualr form or class only (not to all the program) you can put the declaration of teh variable at the top of the code window

Hope that helps ;)
 
Hmmm....

Hello,

Thanks for your reply. I am sorry but I am not sure I understand what you are suggesting. What I am trying to do is increase the value of a certain variable in one procedure and used that increased variable as a counter in another procedure....this is how i have it declared but it doesn't really work...

VB.NET:
Partial Class TestFile
    Inherits System.Web.UI.Page

    Public cgv_searchStr As Integer = 5


    Protected Sub Attributes_Submit_Btn(ByVal sender As Object, ByVal e As System.EventArgs)

            cgv_searchStr += 10

    End Sub
    
Protected Sub Submit_Btn2(ByVal sender As Object, ByVal e As System.EventArgs)

            if cgv_searchStr = 15 then

              response.write("Hello")

            end if

    End Sub

End Class

Thanks for your assistance. I really do appreciate it.


Regards,
Mohammed
 
Last edited by a moderator:
i tried the following and it worked

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cgv_searchStr = 5
        cgv_searchStr = cgv_searchStr + 10

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If cgv_searchStr = 15 Then
            MessageBox.Show("Hello")
        End If
    End Sub

btw cgv_searchStr is declared globaly

VB.NET:
 Public cgv_searchStr As Integer
 
Thread was moved from winforms section (VB.NET) to webforms section (ASP.NET), be careful to ask in most relevant forum.

If your purpose with the class level variable is to preserve state between postbacks this will not work. ASP.Net serves pages in a stateless environment, see ASP.NET State Management Overview, using a Session variable could be an option for you.
 
Thank you Femus...I apologize for not specifying that I was trying to generate some webpages! I really appreciate your help.

Thank you JohnH. It is postback (and my lack of knowledge) that was causing the problem! Also, sorry for not posting my question in the right section.


Thanks again guys and take care,
Mohammed
 
Back
Top