Following this answer. Can I disable automatic instantiation? I want VS to show the error:
Reference to a non-shared member requires an object reference
Following this answer. Can I disable automatic instantiation? I want VS to show the error:
Reference to a non-shared member requires an object reference
When I originally wrote this answer, it was based on empirical evidence from the .vbproj file and the VB IDE and I had no reference material. Since then I have come across the article "Simplify Common Tasks by Customizing the My Namespace" published in the July 2005 MSDN Magazine (online version; full magazine edition download
This article shows to control the generation of the various My Namespace features via compilation constants. In particular it shows how to overide the MyType tag stored in the .vbproj file.
This is accomplished by setting the "Custom Constants" accessible by going to Project Properties->Compile Tab and clicking on the "Advanced Compile Options" button.
To override then MyType tag, set the custom constant _MyType.
If you want to include specific My Namespace items, you can set the pertinent constant(s) shown in the following table from the above referenced article.
The default form instances are part of the "My.Forms" member. Note that in order to prevent "My.Forms"
from being generated, set _MyType to either "Empty" or "Custom". You do not need to explicitly include _MYFORMS="False".
To use the additional constants defined in Table 5, set _MyType="Custom".
To include all the standard My Namespace items except for My.Forms, use the following constants:
_MyType="Custom", _MYAPPLICATIONTYPE="Windows", _MYCOMPUTERTYPE="Windows", _MYUSERTYPE="Windows", _MYWEBSERVICES="True"
End Update
The features enabled in Visual Studio are initialized based on the initial project template you select when creating a project. Normally, one would select the "Windows Forms App(.Net Framework) Visual Basic" template as shown below.
(Note: All images are based on VS 2017 Version 15.8.7)
This will load up a VB project environment that includes a lot of automatic code generation that supports the VB Application Framework including the "Default Form Instance" feature via additions to the My Namespace (see: My.Internals: Examining the Visual Basic My Feature. Now you could try to disable this framework via the Project Properties->Appliction Tab->Enable Application Framework checkbox, but all that does is require you to provide you own "Sub Main" implementation.
To create a project that free of all the My Namespace code generation including the the default form instances, you need to start with the "Empty Project (.Net Framework)" template.
This will create a bare-bones VB project. If you add a WinForm (Project Menu->Add Window Form) and go to the code-view and add the following:
Class Demo_NoDefaultForms
Sub ErrorOnForm1Reference()
Form1.Text = "ain't gonna work"
End Sub
End Class
You will see that the default Form1 instance is not supported.
Furthermore, all the "Application Framework" stuff in the project properties is disabled.
The project type My Namespace information is like all project information is stored in the projName.vbproj file and is stored in the <MyType> tag. Although I have never found any official documentation on this tag, the following summarizes my observations from creating various project types.
MyType Tag Value Project Type ----------------------------- ------------------------------------------------ WindowsForms Normal Windows Form App w/ Application Framework enabled WindowsFormsWithCustomSubMain Normal Windows Form App w/ Application Framework disabled Console Console App w/ Application Framework enabled Custom WPF Application Empty No My Namespace additions
Starting with the "Empty Project (.Net Framework)" template would be tedious as you would need to perform a lot of boiler-plate setup and declare a "Sub Main" each time. I recommend that you create a base project with all your own customizations and then export the project a new template (see: How to: Create project templates.
PropertyGroup: <DefineConstants>_MyType="Custom",_MYCOMPUTERTYPE="Windows"</DefineConstants>