本文介绍了(求助)拆分(VB).NET类到subs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有没有办法将一个大类拆分成子模块(或其他东西)。 该类具有全局DataSet和许多访问或修改DataSet的方法。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.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 SubEnd Class 班级当前通话Dim MyClass As New MyBigClass()MyBigClass.InitMyData()MyBigClass.DoCheck()MyBigClass.SaveToDataBase()Dim MyClass As New MyBigClass()MyBigClass.DataAccess.InitMyData()MyBigClass.CheckFunctions.DoCheck()MyBigClass.DataAccess.SaveToDataBase() 我试过的: 实际上我不知道如何将课程分成描述性部​​分。 任何建议或模式? 在过去,我使用DataSet(或基类)作为子类的参数,但我认为最好是避免这种情况。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.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 ClassEnd Class推荐答案好吧,你可以嵌套这些类:Well, you could nest the classes:Public Class MyBigClass Public Class DataAccess ... End Class Public Class CheckFunctions ... End Class ...End Class甚至嵌套它们并使用Partial将它们分成单独的文件:Or even nest them and use Partial to separate them into separate files:Public Partial Class MyBigClass Public Class DataAccess ... End Class ...End Class另见什么不该做:反模式和解决方案 [ ^ ](来自我们的) - 上帝对象部分。See also What not to do: Anti-Patterns and the Solutions[^] (from our Sander Rossel) - The God Object section.伟大的嵌套类解决方案: 我不必关心数据集 - 它是参考完美的基类。 所以从子类我可以完全访问基础的所有方法,它被分成功能组。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.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 ClassEnd Class 这篇关于(求助)拆分(VB).NET类到subs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 00:06
查看更多