Search code examples
vb.netdllvisual-studio-2019desktop-applicationclass-library

Launch a class library interface in console app vb.net


Good morning, I am new to VB.NET. I have created an interface in which I can write data and transfer it to a database with a submit button, I used class library template in visual studio 2019. This template generates a .dll file after compilation. I would like to create a .exe file. So, I created a new console project to the solution and I set it to the starter project. But, I did not know how to launch the interface created from a console application. Is there a method in VB.NET that launch an interface of a class library? Or am I doing things wrong. I will need the .exe for the final user.


Solution

  • When you create a C# WinForms Application project targeting .NET Framework, VS generates the following class to provide the entry point:

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
    

    When you create a similar project in VB, the VB Application Framework is enabled by default. That framework hides the entry point from and you just specify the form type you want to be the first displayed. If you disable the Application Framework then you lose some automatic functionality, e.g. splash screen and shutdown on last form close, but you also can/must provide your own Main method. The VB equivalent to that C# code would be this:

    Module Program
    
        <STAThread>
        Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1)
        End Sub
    
    End Module
    

    You can do that in your own VB WinForms Application project and then replace Form1 with whatever form you want, including a form defined in a separate referenced assembly. You'll need to specify that module or the Main method as the Startup Object in the project properties.