r/visualbasic • u/chacham2 • Jan 20 '21
Tips & Tricks Just discovered Partial Class
So, i just discovered Partial Class. Well, actually, Partial is a modifier and not just for Class. But i happened to be excited to use it for Class right now.
Then again, i didn't actually discover it just now. I've seen it before, perhaps many times. It just never clicked, or for whatever reason, i never saw too much value in it. Now i do.
Some time ago, someone taught me one of his own practices: If the code doesn't fit on one screen, split it into another function. He was using C++, and 20 years ago, that really helped with reading code.
I never picked up that practice exactly, but the idea still rings true. Long routines are hard to read and often should be split. I often split things by function, and have many subs and functions with just a line or two, where the purpose of splitting it was just to give it a descriptive name and be kind to the next person (probably me) who looks at the code.
But this idea extends further. Often i'll have Form1.vb itself as some long winding file impossible to navigate without knowing the process. Yeah, there are the dropdowns on top of the editor that i never use, but they do not help with conceptualizing what goes together. That's what Modules and Classes are for.
Modules are okay but have no shared context. So, i need a class. But creating another class means it has its own context. If i want logging to continue in the other class (because it's really just an extension of the current class) i need to pass it a reference to the log, or any other object it wants to use. That's might be okay, except it breaks some of the ideas of encapsulation, can lead to other issues, and it needs to be instantiated, which seems silly for the task at hand.
A second look made me realize that i did not need another class in this case. I just wanted a second file for the purpose of organization. Enter Partial Class. I just changed Class <new class> to Partial Class Form1 (note to self that i'll probably ignore: that really does need a better name) and poof, i'm done. (Well, actually, i also needed to remove the sub new and the variables to hold the passed objects.)
I'm so much happier now. I got organization down so people can understand the logical sectioning of the code and yet kept it as one class, for there is no reason for it to be otherwise. The only downside is that looking at a routine name will not tell you which file it is in. Well, unless you know what it does.
2
u/TheFotty Jan 21 '21
Code regions are way better than using partial classes from a pure organizational standpoint. The main use cases for partial classes are for auto generated code and when you have multiple developers working on a class and you want to split up the workload without having to do a ton of code merges.
1
u/chacham2 Jan 21 '21
What's a code region?
2
u/TheFotty Jan 21 '21
Code region is syntax used in Visual Studio to create collapsable regions of code to organize them better.
You use a pound/hash to create one
#region "Functions" Private Function DoStuff() as integer End Function Private Function DoOtherStuff() a string End Function #End RegionYou can even have regions in regions
A sample class looks like this
Public Class SampleClass #Region "Class Level Variables" Private _SomeVariable As String = "" Private _SomeOtherVariable As Integer = 0 #End Region #Region "Constructors" Public Sub New() End Sub Public Sub New(DefaultValue As String) _SomeVariable = DefaultValue End Sub #End Region #Region "Properties" Public Property SomeProp As String Public Property OtherProp As Integer #End Region #Region "Methods" Public Function GetAString() As String Return _SomeVariable End Function #Region "Math Functions" Public Function AddNumbers(int1 As Integer, int2 As Integer) As Integer Return int1 + int2 End Function #End Region #End Region End ClassWhen collapsed, the class in the code editors looks like this, and each has a + next to it to expand the region to show the code.
Public Class SampleClass Class Level Variables Constructors Properties Methods End Class1
u/chacham2 Jan 21 '21
Ah, so that's what they are called. :)
I'm not a fan of those, because it "hides" code, and is not obvious how to look at all your code, section by section, like how another file does it.
Now, if code regions would open in a separate tab, i might like that.
2
u/Nswl VB.Net Intermediate Jan 20 '21
I’m quite new to vb, so is this basically like making a new class except it’s contained within another class? So like a class called Human could have a partial class called Legs within it or something like that?
3
u/TheFotty Jan 21 '21
No, it does not change the way a class works at all, it's just a way to split a single class across multiple physical files. It was introduced to allow auto generated code (like form designer code in winforms) to be separated from user code in the same class.
2
u/revennest Jan 21 '21
You can also use Partial with Structure and Interface, I use it with Module a lot mostly for host Extension method; about Region, it more like remark for me, I usually use it where I might be loss.
3
u/Seismicsentinel Jan 20 '21 edited Jan 20 '21
It's still a little confusing for me when I click on a class in VS and don't notice the arrow under it with all the partial classes. But it's wonderful for big classes that make more sense as one class. My favorite is a big database context with partial classes organized by which DB schema each function in the context class deals with. Better than regions, better than a 3000 line file, better than stapling 20+ contexts together, or injecting 20+ dependencies into any class that needs full database access