Take the 2-minute tour ×

I have a class that has the following property that is generated by the methods in the constructor.

Public Class clsLoadTables
    Private _ds As New DataSet
    Public Property ds() As DataSet
            Return _ds
        End Get
        Set(ByVal value As DataSet)
            _ds = value
        End Set
    End Property

    Sub New()

            loadXMLFiles(pathMainTable, "MainRMDTable")
            loadXMLFiles(pathBeneLifeExp, "pathBeneLifeExp")
        Catch ex As Exception
        End Try

    End Sub

End Class

My problem is that I do not want to inherit this class, but I have other classes that need to access the ds DataSet property. If at all possible I would like to not use inheritance and not load my datatable more than once in the program.

Here is my failed attempt to access the property inside another class that is not inheriting clsLoadTables:

        Dim tableRow As DataRow = ds.Tables("MainRMDTable").Select(String.Format("age={0}",  age.ToString()))(0)

Any ideas on how I can access this dataset that I want to load only once in the program from many classes without using class inheritance or a global module?

share|improve this question
What is the purpose of the class, and how is it different from a global module (that you don't want to use)? –  Pavel Minaev Apr 7 '12 at 0:23
You could make it shared and load it in the shared constructor‌​. –  Tim Schmelter Apr 7 '12 at 0:29
Pavel - Your right it's not. I'm trying to do this to force myself to become a more object oriented programmer –  Lance Collins Apr 7 '12 at 1:30
Tim - I'm looking into that now. Thanks for the comment. –  Lance Collins Apr 7 '12 at 1:31

3 Answers 3

up vote 3 down vote accepted

You have it as a public property, so you should be able to access it if you have a reference to an instance of the clsLoadTables class.

Dim foo As New clsLoadTables

Dim tableRow As DataRow = foo.ds.Tables("MainRMDTable").Select(String.Format("age={0}",  age.ToString()))(0)
share|improve this answer
Wouldn't that rerun the methods I have in the constructor though? I am using Sub New in that class to load data into a dataset. I only want to call that once. –  Lance Collins Apr 7 '12 at 1:27
@LanceCollins: You create one clsLoadTables object and use it throughout. You don't need to recreate a new object everytime, you just need to keep a reference to it. Really, the ideal way would be to use a singleton pattern for this. –  Matt Burland Apr 7 '12 at 1:52

For Global Scope in VB.Net use a MODULE with PUBLIC variables and PUBLIC methods or friend variables like

Public _ds As New DataSet


Friend _ds As New DataSet

hope i didn't misunderstood your question..

share|improve this answer
Sandeep - thanks for the comment. That will definetly work. I was trying to see if I could implement this correctly without using modules, but if using a module is considered best practice then you are absolutely correct. Thanks! –  Lance Collins Apr 7 '12 at 1:33
yeah, if you use this approach then you don't need to inherit the class , i suggest for these cases you create a MODULE for storing public variables –  appdroid Apr 7 '12 at 2:27

Typically what i do for something like this is just change _ds in your class to be shared and load the data thefirst time the get of your property is accessed.

share|improve this answer

Your Answer


