I have a Module in VB.NET called Math.vb and in that module, I created a Function that displays returns the percentage of one number to another. I have a Sub that calls the Function and I call the Sub from my Form1 class. Is there an advantage/disadvantage to putting the function in the class or the module?
Share
It all depends. Modules in VB.net are basically the same as static classes, but you don’t have to preface method names in a module with the “module name”.
This makes modules a nice place to put “Utility” functions that are of a very generic nature and that don’t relate to any existing class. Your “Percentage” function would probably fit in this category.
On the other hand, if you were to create a class that represented those numbers you’re calculating the percentage for, and one method of that class was going to be this “percentage” function, that would return the percentage based on the numbers that are defined within the class instance, then, yes, I’d put the function in a class.
And finally, if the Percentage function would only make sense to be called from the Form1 class, then I’d make it a PRIVATE function in that class.
For methods, you generally want to apply the most restrictive scope possible.