vb.net auto instantiation (forms)

前端 未结 2 2016
挽巷
挽巷 2021-01-24 18:44

In VB.Net you can show a form without crete an object reference before... vb.net do it to you, but, that \"feature\" is generating many problems, eg:

Public Clas         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-24 19:24

    I looking for a solution to do the same and ran across this thread.

    Seeing that there is no way to get rid of the default instance, and it would allow you to make the "whoops" of calling the form without an object reference, I just resorted to do this:

    ''' 
    ''' This overrided of Sub New is only here to force you to create an object reference. Passing true or false will make no difference.
    ''' 
    Public Sub New(MustInstanciate As Boolean)
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub
    

    This forces you to create an object reference because it gets rid of the implicit Sub New, having only one constructor which requires a variable, which requires an object reference.

    This trick works for me at least. I just thought I would just add it as a solution in case someone else runs into this thread for the same reason I did.

提交回复
热议问题