Loading values into a Module

Windsailor

Well-known member
Joined
Aug 2, 2006
Messages
45
Programming Experience
1-3
What I have is a function on a form that uses 12+ values that originally is populated from a dataset.
I would like to use a module for the function so I can use it globally.
The obvious issue is that I can not populate the dataset through the module.
I have thought of populating the dataset and then assigning the values as global values, but the trick here is to tie it to the correct form, or parent form when it is loaded (there may be times when I will want to refresh the dataset to check for new values).

Any suggestions of other ways to do this?

Thanks
 
What I have is a function on a form that uses 12+ values that originally is populated from a dataset.
I would like to use a module for the function so I can use it globally.
The obvious issue is that I can not populate the dataset through the module.
I have thought of populating the dataset and then assigning the values as global values, but the trick here is to tie it to the correct form, or parent form when it is loaded (there may be times when I will want to refresh the dataset to check for new values).

Any suggestions of other ways to do this?

Thanks

Create a class (don't use a module for something like this):

VB.NET:
Public Class MyClass
    Public Property1 As String
    Public Property2 As String
    Public dsDataSet As DataSet

    Public Sub New()
        ' initialize something if you want, add arguments to New(), etc
    End Sub
End Class

Now assign a private variable to the form so it always has access to it after it is instantiated, and a public property to the form so that other forms, etc, can manipulate and read the MyClass reference:

VB.NET:
Public Class MyForm

    '...

    Private m_MyClass As MyClass

    Public ReadOnly Property AssociatedMyClassObject As MyClass
        Get
             Return Me.m_MyClass
        End Get
    End Property

    '...

    Public Sub Form_Load(...) Handles Form.Load
         m_MyClass = New MyClass
         m_MyClass.Property1 = "Test string"
         m_MyClass.Property2 = "Test #2"
         m_MyClass.dsDataSet = <dataset object>
    End Sub

    Public Sub Button_Click(...) Handles Button.Click
         Debug.WriteLine("Property1=" & m_MyClass.Property1)
         Debug.WriteLine("Property2=" & m_MyClass.Property2)
         Debug.WriteLine("Tables in dataset=" & m_MyClass.dsDataSet.Tables.Count
    End Sub
    
    '...

End Class
 
Last edited:
Thanks

Got it to go in 1...
Well not quite :eek:
But I did get it to go with your time and help... it is greatly appreciated.

To expand on this a little...

Is there a way to add the adapter.fill event to a class?
In other words is there a way to be able to fill the dataset from the class without any form loaded?
That would be great treat to use globally.
Seems like it should be easy to do... just not quite sure how to do it right now.
 
Ok... wait a minute.
Noob here.

What can't I just throw the function inside the class?
I suppose that would be self defeating... and not good practice.

Just create the class properties to jive with the function properties and let her fly...?
I would assume the return properties would be the same...
 
It would actually be a good idea to put your data-gathering code inside the class as well. When I design my apps, I create the n-layer approach, where I have one class which gets/writes the data, one class which renders, validates or otherwise modifies the data to ready it to/from presentation, and finally the code on the form which fills/gets controls values, etc.

If you do put the function in the class, make sure you make the class Disposable or give it a close method (disposable is the preferred method), so that all resources and connections are freed when you're done with the class.
 
Back
Top