1

I have replaced (regex) all accesses to the default form instances in my project and I now want to make sure that I'm not using the default instances any more.

It seems that the default instances feature can't be disabled (without changing the classes' ctor)

Anybody know how to find all usages of the default form instances in VB.Net (or - access to non-shared members using the class name)

FYI: I'm using VS2017 with ReSharper

Dennis Kuypers
  • 546
  • 4
  • 16

2 Answers2

3

Contrary to popular belief, the generation of Default Instances in a WinForm application is not a mandatory feature of Visual Basic.

From the Visual Basic Language Specification Version 11 (emphasis added) :

11.6.2 Default Instances In some situations, classes derived from a common base class usually or always have only a single instance. For example, most windows shown in a user interface only ever have one instance showing on the screen at any time. To simplify working with these types of classes, Visual Basic can automatically generate default instances of the classes that provide a single, easily referenced instance for each class.

The problem is that I know of no documentation that tells you how to enable/disable their creation. It is known that this feature is part of the Application Framework that can be enabled/disabled in the "Application Tab" of the project's properties pages. However, disabling the Application Framework does not disable the default instance generation.

If instead selecting the "Windows Forms App" template when creating a project, you select the "Empty Project" template and proceed to add a form, you can create a WinForm project in which the "Enable application framework" checkbox is grayed out and not selectable. You will also find that you can not use default form instances. From this, you can deduce that this feature can be configured via "*.vbproj" file.

The controlling item is the <MyType> tag. The possible values for this property are:

  • Empty - application framework unavailable
  • Console
  • WindowsForms - application framework enabled
  • WindowsFormsWithCustomSubMain - application framework disabled
  • Windows - Class Library
  • WebControl - library

So if you want to convert an existing project to one that does not support default instances, you can edit the "*.vbproj" file and change the <MyType> property to Empty.

Be aware that doing this will also eliminate other features of the application framework such as My.Computer and My.User, but you could always implement those features yourself such is described in How to: Use the My Namespace (C# Programming Guide).

TnTinMn
  • 11,522
  • 3
  • 18
  • 39
1

A form without a parameterless constructor does not have a default instance, so you can add a constructor with a parameter to your form as a temporary measure. That will flag any place that you're either creating an instance or using the default instance as errors and you can confirm that they are all the former and not the latter. Once you have eliminated all uses of the default instance, you can remove the temporary constructor and the implicit parameterless constructor will be restored automatically.

Note that this assumes that your form doesn't already have multiple constructors. If it does then you can simply comment out the parameterless one temporarily.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46