2

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

4
  • When you want to see this error? Can you show that code? Commented Nov 2, 2018 at 10:31
  • @Tim Schmelter: at compiling. Commented Nov 2, 2018 at 12:41
  • "I am not going to change and instantiate them with a junk parameter". I wasn't suggesting that you should. I was stating a fact: that's the only way to prevent a form from having a default instance. As you say, you're not going to do that so you can't prevent your forms having default instances. The fact that you want to disable default instances doesn't mean that it's possible. Commented Nov 2, 2018 at 14:38
  • Have you checked products like Re-Sharper? Perhaps you can set up a rule there regarding this. Commented Nov 2, 2018 at 16:03

1 Answer 1

4

Update:

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.

Custom Constants

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.

My Namespace constants

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)

Windows Forms App Template

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.

WinForm Props

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.

Empty VB Project

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.

Default Instance fail

Furthermore, all the "Application Framework" stuff in the project properties is disabled.

Empty Proj Props

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Much love for this answer, as this feature just bit me again (seemingly "correct" code that set the event handler on the wrong instance). I could've sworn I had previously searched for such a setting to no avail. For those wondering, in Sdk-style projects, you can put something like this in a PropertyGroup: <DefineConstants>_MyType="Custom",_MYCOMPUTERTYPE="Windows"</DefineConstants>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.