Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

So I know how everyone feels about global variables, but can someone tell me the best way to go about using the same variables (i.e. DB connection object, rs object, connection string) in many forms without redefining the same stuff over and over if not making them global? Or is it a better pratice to dim/set them in each form? Just curious.

 

Jenn

  • Leaders
Posted

you could bung it in a module

Imports System.Data

Module Module1

   Public Function OpenAccess(ByVal strFile As String, ByVal strCommand As String, ByVal strTable As String, ByVal DG As DataGrid)
       Dim objAccess As New OleDb.OleDbConnection(strFile)
       Try
           Dim objCmd As New OleDb.OleDbCommand(strCommand, objAccess)
           Dim objAdapt As New OleDb.OleDbDataAdapter(objCmd)
           Dim objSet As New Data.DataSet()
           objAdapt.Fill(objSet, strTable)
           DG.DataSource = objSet.Tables(strTable)
       Catch ex As Exception
           MessageBox.Show(ex.Message)
       Finally
           objAccess.Close()
       End Try

   End Function

End Module

then in a form / all the forms required :

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDb.mdb"
       Dim command As String = "SELECT * FROM Table1"

       OpenAccess(constring, command, "Table1", DataGrid1)

   End Sub

Posted
Mutant, I'm not sure if the eyes are more charming than the horns. But so much love emenates out of your icon that its always a pleasure to accounter one of your otherwise interesting contribution.

Auto-suggestion: "I have a life"

Uncontroled thinking: "So what the.."

  • *Experts*
Posted

As a militant coding-standard follower, I would never use a Module in my program. It defeats the entire purpose of VB (to provide a full OOP programming language) by allowing you to use old, obselete modular programming practices. You should do this:

Public Class SomeClass
 Public Shared Sub MySub()
   'do stuff!
 End Sub
End Class

and call it like this:

SomeClass.MySub()

Just as easy to do as a module, but it keeps your code fully framework compliant, object-oriented, and organized.

 

Shared allows you to access members of class without first creating an instance.

Posted

I'm not sure either of these are exactly what I need. Here's a different senerio - let me know if I'm just not thinking about this correctly. I have a login form first. This will run to the daatabase, verify the user, and return what this user has permission to do. Throughout the entire rest of the program (multiple forms) I need that user id and permissions readily available - not having to instanciate a new class or call a function or anything. Just grab the ID so I can stick it into the database when they add a record. I am more familiar with doing web apps so I would just use a session variable for the ID and one for the permission level. How does that equate to a desktop app? If its still by using the above examples I guess I didn't quite understand - can you elaborate or point me to a related tutorial? Thanks so much!!!

 

Jenn

Posted

Okay, so I attempted do follow you guys and here's what I came up with (doesn't work). Where am I not thinking correctly? (I cut out most of my variables so it would be easier to read)

 

globals.vb

Module globals
   Public Class objGlobal
       Dim iUserID As Integer
              
       Public Sub New()
           MyBase.New()
        End Sub
      
       Function getVar(ByVal sVar)
           Select Case sVar
               Case "iUserID"
                   Return iUserID
               End Select
       End Function

       Sub setVar(ByVal sVar, ByVal sValue)
           Select Case sVar
               Case "iUserID"
                   iUserID = sValue
              End Select
       End Sub

   End Class
End Module

 

Then in my Forms I use:

 

Dim objGlobal As New objGlobal
objGlobal.setVar("iUserID", ObjReader("iUserID"))

 

-OR -

 

Dim objGlobal As New objGlobal
iUserID = objGlobal.getVar("iUserID")

 

However, because I am making a new global object in each form the values I set are only good for that form. If I setVar in one form then go to getVar in another form its reset back to zero. But if I don't use "Dim objGlobal as NEW objGlobal" then I get an error saying "Object is not set to instance" or something. How can I get the setVar to set it across all forms?

 

Jenn

Posted

Following Volte Face's advice, you can do something like:

Public Class GlobalVars
       Private Shared iUserID As Integer
              
       Public Sub New()
           MyBase.New()
        End Sub
      
       Public Shared Function getVar(ByVal sVar) As String
           Select Case sVar
               Case "iUserID"
                   Return iUserID
               End Select
       End Function

       Public Shared Sub setVar(ByVal sVar, ByVal sValue)
           Select Case sVar
               Case "iUserID"
                   iUserID = sValue
              End Select
       End Sub
   End Class

 

so that you can do GlobalVars.getVar("DesiredValueKey"). Btw, you may want to use a private shared arraylist to store your globals; would make your set/get code so much simpler.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...