RE:Declaring Global Variable


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: RE:Declaring Global Variable

  1. #1
    Join Date
    Mar 2008
    Posts
    130

    RE:Declaring Global Variable

    hi good day...is it possible to declare a variable to be access on all my forms..and is it possible to make a function that can also be access to all my forms?like in php...if module is the answer how do i declare it and access it in my forms..tried it but i failed...im try to make my program more simple

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Which language are you using? If you're using VB, add a Module to your project. Public variables in a Module are visible throughout the application:
    Code:
    Module Globals
    
        Public MyGlobalVariable As String
    
    End Module
    
    Class MyForm
    
        Public Sub SomeMethod()
        
            Dim LocalString As String = Globals.MyGlobalVariable
    
        End Sub
    
    End Class
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Mar 2008
    Posts
    130
    im using VB...

    Class MyForm

    Public Sub SomeMethod()

    Dim LocalString As String = Globals.MyGlobalVariable

    End Sub

    End Class
    is this declared together with the module or its an example on how will i access it in my form

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    It's an example of how to access the variable from your form. Access the variable using <ModuleName>.<VariableName>.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Mar 2008
    Posts
    130
    Module Global

    Public MyString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\posdb.mdb"
    Public MyConn As New Oledb.OledbConnection(MyString)
    Public MyCmd As Oledb.OledbCommand

    End Module
    is this possible?i think its not working...

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Try
    Code:
    Public MyString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\posdb.mdb"
    Public MyConn As OleDb.OleDbConnection
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyConn = New OleDbConnection(MyString)
        MyConn.Open()
    End Sub
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  7. #7
    Join Date
    Mar 2008
    Posts
    130
    thanks hack working fine..thank you also phil..

    btw is it possible to make functions that can be access globally?

  8. #8
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Yes, same method as above: declare the functions as public within a module, then call them using <ModuleName>.<FunctionName>.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  9. #9
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by chokies12 View Post
    btw is it possible to make functions that can be access globally?
    Sure....just like variables, make 'em Public.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  10. #10
    Join Date
    Mar 2008
    Posts
    130
    can you give me a example on how to define and access it?

  11. #11
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Code:
    Module Globals
    
        Public Function MyFunction(ByVal MyParameter As String) As String
            Dim SomeValue As String = "blah"
            Return SomeValue
        End Function
    
    End Module
    
    Class MyForm
    
        Public Sub SomeMethod()
        
            Dim ReturnValue As String = Globals.MyFunction("Foo")
    
        End Sub
    
    End Class
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2008, 01:29 PM
  2. Replies: 0
    Last Post: 08-28-2001, 03:18 AM
  3. Global variable
    By veena in forum XML
    Replies: 0
    Last Post: 07-09-2001, 04:16 PM
  4. global variable accessible from stored procedure
    By Michael Shutt in forum Database
    Replies: 2
    Last Post: 03-29-2001, 02:34 PM
  5. Global Variable not being read
    By Roberto Marques in forum Web
    Replies: 0
    Last Post: 06-08-2000, 10:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center