Visual Studio - manage multiple files that are part of one Class - classes, modules - vb.net

My VB project is large enough that it requires several files. It was originally developed as a Console App and I created each file as a MODULE. All modules could use subroutines, data structures and constants from other MODULES and everything worked fine. I needed to add basic windowing to the app and this required that the app be converted from a Console App to a Windows Forms App. The main window is Form1 which is not a MODULE but a CLASS. The problem is that some MODULE based functions cannot access subroutines, data and constants that are defined within the CLASS Form1 unless they are incorporated into the CLASS file and this makes the CLASS file very large. If I add a new Class file to the project, it also cannot interoperate with Class Form1 in the same way that multi-MODULE code interoperates.
How does one spread CLASS code across several files and still allow it to interoperate as if it were in a single file? Alternatively, how does one create several CLASS files that operate the way multiple MODULE files operate.
I am sure that there are all kinds of best practices that I am violating but the goal to to get some prototype software working and interfaced to some lab equipment.
Thank you in advance

Use a partial class (Partial keyword on the class declaration). Each partial "bit" of the class will be merged at compile time. All partial bits must be in the same project.

Modules are default shared and do not require initialization with the New keyword. When you made your console app a windows app, it became a class...You could change it to the same behavior as a module simply by making it a Public shared Class and making all properties and methods inside shared as well.
so while you can access your methods and properties in your modules without initialization, you would need to use the NEW method to initialize your Class methods.
To access the Class from the module you would simply have to use:
SomeModulemethod
dim x as new CLASS
CLASS.SOMEMETHOD
someModuleMethod End
You could also use Partial Classing to split up your Classes, but it is much better to decide if you really need a separate class for what you want to do.

Related

Forcibly opening or otherwise impersonating a class in Kotlin

I'm working in Kotlin, and I have a library that I can't modify (without maintaining my own fork of this huge and rapidly changing library).
There's a class in that library I need to instantiate, and it requires an instance of another class that does some stuff for it. I need to change how that second class works, so it feeds different information to the first class. But the second class is not open, and the first class asks for it by its full, non-open type.
How do I force my way into the non-open class and extend it anyway, against the desires of the library authors? Alternately, how do I cheat the type system to pass my own class off as an instance of the class the library is demanding?
Do I need to do some fiddling around at the JAR/build system level to replace the library's class files with my own versions? Can I use reflection to somehow impersonate the non-open class? Is there some other way to do it?

References to startup object fail when converting WinForms app to Class Library

I'm trying to convert WinForms app into a class library - long story short the production environment I'm working in will allow our users to make changes to DLLs but not EXEs, so I'm trying to shove an entire existing app into a DLL and then just create and show an instance of the startup object/form from a second WinForms app with the goal of creating some kind of auto-update system.
I've changed the output type of the project to Class Library, added the launcher app, etc, but attempting to build the old app as a class library throws hundreds of errors, almost all of which are Reference to a non-shared member requires an object reference.
Upon inspection, these errors are appearing everywhere in the code that the startup object/form or any of its properties or methods are referenced. Since a great many things in a WinForms application naturally reference the main form... this is problematic.
Stuff like:
If DbConn = n.DbConn_.Prod Then
miParent = mainform.MiProdReq
throws the aforementioned error upon the attempt to access mainform.MiProdReq
Am I missing some simple/obvious step here?
You are referring to the default instance of the mainform type in that code. Default instances are provided by something automagically generated when building Windows Forms Application projects. Class Library projects have no such thing as default instances, so any code that tries to use them will appear to be trying to access instance members as though they were Shared.
You need to put an instance somewhere and change your code to refer to that instead. If you use a global variable, which is not ideal itself but the simplest option for where you're at, then you can just do a Find & Replace In Files to find the references you need to change.
Note that default instances are something that most experienced developers would suggest avoiding anyway. They don't exist in C# and I've never heard complaints about that, so it's hardly onerous. They were added to VB as a convenience for beginners and migrating VB6 developers who weren't used to proper OOP.
EDIT:
I haven't tested it but you may be able to use Application.OpenForms(0) to get a reference to the startup form anywhere in your library. You could, perhaps, add a module like this:
Module Module1
Private _mainform As Form1
Public ReadOnly Property mainform As mainform
Get
If _mainform Is Nothing Then
_mainform = DirectCast(Application.OpenForms(0), mainform)
End If
Return _mainform
End Get
End Property
End Module
and then your code may even just work as it is.

How to hide variables and some functions in a module in vb.net

I am hobby programmar.Now, am in the middle of making a class library in vb.net. I am making some necessary win32 API functions inside a class library and use it in my hobby projects for ease of use.
I have a main class and few modules in this project. For easiness, i made each module for each section. For example a window module for window related api declarations and variables and structure. The main class contains only all the wrapper functions. So i am declared the variables and structures and api declarations as public so that i can access them from main class. After i build this class library, i just tested it. But there is a problem. All public vars and structures and declarations are available to access there in my test project. I need to hide those declarations and structures and special vars. They are for internal use. But how to make them hide ? When i declared them as private, i can't access them from my wrapper function in Mainclass. What to do

VB.NET how do I insulate a class from accessing external functions

We are working on updating a code project that has become very messy over time. There are lots of modules which contain public functions that can be used from anywhere.
We want to move as much of the code into classes as possible, so that these can eventually be re-used in the next generation of the application. Is there a way that we can prevent these classes from using any (non-system) functions?
Example:
public module annoyingModule
public function addOneAndOne() as int
return 2
end function
.....(load more functions)....
end module
public class pricer
...(class code)...
end class
I want to make sure that nobody on the team can accidentally make a reference to the addOneAndOne function - if they need functionality from a module which is not part of the class, they need to re-implement it inside the new class.
You need to move your new classes to a separate class library project. As long as your new class library project does not reference the old project and does not include a copy of the old modules, all of that stuff will be inaccessible from the classes in that class library project. You then, in the old project, need to add a reference to your new class library project. That way, the old code can use the new classes, but the new classes will not be able to use the old modules.

Visual Basic Module v Singleton

So I've been programming in C# for the last 6 years or so and now I'm getting my feet wet with VB.net.
The code base I'm working with uses some modules. To me the module looks a lot like a singleton. Only one exists; it can be called anywhere inside the namespace.
Is there something I'm missing here? Does VB not support the normal way a singleton is structured (private constructor/public instance field)?
Modules are not a singleton. It is much more akin to a static class in C#. If you decompile the code you will see they have a very similar structure (modules have an extra attribute).
The major differences between a C# static class and a VB.Net module are ...
Don't have to add Static / Shared qualifiers to methods in a module. They are Shared by default and you cannot change this
If a Module is in an Imported namespace, all of its methods are available without qualification.
Static classes in C# can be generic, modules cannot (although they can have generic members)
If I'm not mistaken, a VB module is the same thing as a static class.