|
-
Sep 22nd, 2002, 03:18 AM
#1
Thread Starter
Fanatic Member
Jeepers is this really the right way to do this??
I've been looking for a way to refer to a DataSet created in one form with code in a second form, and I've found what appears to be a way to do it (playing with it right now) - is this the "right" way?? It seems majorly hackish!
http://msdn.microsoft.com/library/de...adingtonet.asp
Global data in .NET
Earlier I mentioned that global variables are not available in Visual Basic .NET, and now I'm going to tell you how to make something global. How will you ever trust me after this? Actually, I am not being dishonest in either case; global variables are not allowed in Visual Basic .NET, but you can achieve similar functionality using Shared (called static in C#) class members. A Shared class member, as used by the Visual Basic Upgrade Wizard when it adds the DefInstance property to your forms, is available without creating an instance of a class and, if it is a property, its value is shared across your entire application. You could therefore create a class like this:
Code:
Public Class myForms
Private Shared m_CustomerForm As CustomerForm
Public Shared Property CustomerForm() As CustomerForm
Get
Return m_CustomerForm
End Get
Set(ByVal Value As CustomerForm)
m_CustomerForm = Value
End Set
End Property
End Class
When you first create an instance of your form you could store it into this class:
Code:
Dim myNewCust As New CustomerForm()
myNewCust.Show()
myForms.CustomerForm = myNewCust
After the CustomerForm property has been populated with an instance of your form, you could then use this class to access that same instance from anywhere in your code:
Code:
Module DoingStuffWithForms
Sub DoExcitingThings()
myForms.CustomerForm.Text = _
DateTime.Now().ToLongTimeString
End Sub
End Module
Storing your form in this manner comes as close to Visual Basic 6.0's Global variables as you are going to get. The next level of variable scope below this level is class (module, class or form, actually) scope, where a variable is declared within a class and is available to any code in that class, and below that is procedure scope, where a variable is local to a single routine.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|