Click here to Skip to main content
15,891,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a way to split a big class into sub "modules" (or something).
The class has global DataSet and many methods which access or modifies the DataSet.

Origin class (pseudo)
VB
Public Class MyBigClass()
    Public myData As New DataSet()

    Public Sub InitMyData()
        '...Build Dataset from database and custom data
        myData = New DataSet("dummy")
    End Sub

    Public Sub DoCheck()
        If myData.Tables(0).Rows(0)("checkField").ToString() = "123" Then
            myData.Tables(0).Rows(0)("checkField").ToString() = "456"
        End If
    End Sub
    
    Public Sub SaveToDatabase()
        myData = Nothing
    End Sub
End Class


Current call of class
VB
Dim MyClass As New MyBigClass()
MyBigClass.InitMyData()
MyBigClass.DoCheck()
MyBigClass.SaveToDataBase()


Wanted structure of calls
VB
Dim MyClass As New MyBigClass()
MyBigClass.DataAccess.InitMyData()
MyBigClass.CheckFunctions.DoCheck()
MyBigClass.DataAccess.SaveToDataBase()


What I have tried:

Actually I have no idea how to split the class into descriptive segments.
Any suggestions or patterns?

In the past I used the DataSet (or a base class) as a parameter to the sub classes, but I think best would be to avoid this.

VB
Public Class MyBigClass()
    Public myData As New DataSet
    Public DataAccess As _DataAccess() = Nothing

    Public Sub Init()
        DataAccess = New DataAccess(myData)
    End Sub

    Public Class _DataAccess()
        Public myData As New DataSet

        Public Sub New(ByRef myData2use As DataSet)
            myData = myData2use
        End Sub
    End Class
End Class
Posted
Updated 14-Mar-18 2:51am
v3

Well, you could nest the classes:
VB
Public Class MyBigClass
   Public Class DataAccess
      ...
   End Class
   Public Class CheckFunctions
      ...
   End Class
   ...
End Class
Or even nest them and use Partial to separate them into separate files:
VB
Public Partial Class MyBigClass
   Public Class DataAccess
      ...
   End Class
   ...
End Class
 
Share this answer
 
Comments
CPallini 14-Mar-18 7:50am    
5.
Sni.DelWoods 14-Mar-18 7:57am    
That the base idea.
I already split the class into a partial class and different files (MyBigClass.DataAccess.vb)

But how can I access the myData-object in MyBigClass frm the DataAccess class?
I added an example of my current solution. I think there should be a bettern pattern to to this....
OriginalGriff 14-Mar-18 8:10am    
You said this was a Global: they are accessible from all classes.
If it isn't, then you have two options:
1) If there is only ever one instance, then Shared is the way to go.
2) If There is an instance per instance of the MyBigClass class, then you will have to pass the instance to the sub class when it needs it.
Sni.DelWoods 14-Mar-18 8:41am    
I found a very good nested class solutions, where I don't have to care about the references. See solution below. Thanks.
See also What not to do: Anti-Patterns and the Solutions[^] (from our Sander Rossel) - The God Object section.
 
Share this answer
 
Great nested class solution:
I don't have to care about the Dataset - it referes perfect to the base class.
So from the sub classes I have full access to all methods of the base and It's splited into functional groups.

My solution
VB
Public Class MyBigClass
    Public MyData As New DataSet
    Public DataAccess As New DataAccess(Me)

    Public Class _DataAccess 'must be Public due to self reference 
        Private _MyBigClass As MyBigClass = Nothing

        Sub New(ByRef MyBigClass2use As MyBigClass)
            _MyBigClass = MyBigClass2use
        End Sub
    End Class
End Class
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900