I’ve seen some code where a Class is imported, instead of a namespace, making all the static members/methods of that class available. Is this a feature of VB? Or do other languages do this as well?
TestClass.vb
public class TestClass public shared function Somefunc() as Boolean return true end function end class
MainClass.vb
imports TestClass public class MainClass public sub Main() Somefunc() end sub end class
These files are in the App_Code directory. Just curious, because I’ve never thought of doing this before, nor have I read about it anywhere.
One of the reasons this feature is in place is to emulate Visual Basic 6.0’s GlobalMultiUse Option for Instancing. Visual Basic 6.0 doesn’t have the ability to make modules public across a DLL boundary. Instead you set the instancing property to
GlobalMultiUse. It is used mainly for utility classes like a class that export a series of math functions.Every time you call a subroutine or function on a class with the
GlobalMultiUse Instancing, Visual Basic 6.0 instantiates a class behind the scenes before calling the function.It can be abused to generate global functions/variables with all the advantages and disadvantages.