Search code examples
vb.netclassconstructorinstancedeclare

VB.NET Class with no constructor (no new instance can be declared)


I'm making my own message box class (called MessageBoxC, whatever), and like System.Windows.Forms.MessageBox, I want to make my class with no constructors and no possibility to declare a new instance of it.

E.g.:

Public Class MessageBoxC
   Public Overloads Sub Show(ByVal message As String)
      Me.Message = message
      ProcessData()   '(*)
      Me.ShowDialog()
   End Sub
End Class

Public Class Form1
   Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      System.Windows.Forms.MessageBox.Show("Hello World!") 'works fine
      MessageBoxC.Show("Hello World!") 'works fine

      Dim msgBox As New System.Windows.Forms.MessageBox    'and you'll get an error message here (**)
      Dim msgBoxC As New MessageBoxC    'no error message
      End Sub
End Class

(*) Not important. It just calculates text size (width and height in pixels) to correct form size if needed and the corresponding label gets value of Me.Message property.

(**) This one is what I mean. You cannot make a new instance of a MessageBox class, you'll get following error-message: "Type System.Windows.Forms.MessageBox has no constructors." Well, my class has also no constructors, but it's possible to declare an instance of it. What's the trick here?

Thanks a lot!


Solved. Thanks to OneFineDay.

Public Class MessageBoxC

    Private Sub New()
       'Empty
    End Sub

    Public Overloads Shared Function Show(ByVal message As String) As System.Windows.Forms.DialogResult
       Return Show(message, Constants.MyAppName, Constants.messageTitle, MessageBoxCButtons.OK, MessageBoxCIcon.Undefined)
    End Function

    Public Overloads Shared Function Show(ByVal message As String, _
                                          ByVal caption As String, _
                                          ByVal title As String, _
                                          ByVal buttons As Library.MessageBoxCButtons, _
                                          ByVal icon As Library.MessageBoxCIcon) As System.Windows.Forms.DialogResult
       Dim msgBoxC As New CBox(message, caption, title, buttons, icon)
       msgBoxC.ShowDialog()
       Return msgBoxC.DialogResult
    End Function

    Private Class CBox
    Inherits System.Windows.Forms.Form

       Sub New(ByVal message As String, _
               ByVal caption As String, _
               ByVal title As String, _
               ByVal buttons As Library.MessageBoxCButtons, _
               ByVal icon As Library.MessageBoxCIcon)

          MyBase.New()
          InitializeComponent()

          Me.Message = message
          Me.Text = caption
          Me.Title = title
          Me.Buttons = buttons
          Me.Icon64 = icon

          Me.OptimizeMe()
       End Sub
    End Class
End Class

Public Class Form1
   Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      Dim dialogResult As New DialogResult
      dialogResult = MessageBoxC.Show("This is a simple message.")
      MessageBox.Show(dialogResult.ToString)
   End Sub
End Class

Solution

  • Here is one way to hide the constructor - mainly because the class in question is not accessible.

    Public Class Form1
    
      Private Sub meLoad() Handles Me.Load
        'Usage
        FooBar.Show("Hi")
      End Sub
      '...
    End Class
    
    Public Class FooBar
    
      Private Sub New()
      End Sub
    
      Public Shared Sub Show(message As String)
        Dim mbc As New MessageBoxC(message)
        mbc.ShowDialog()
      End Sub
    
      'MessageBoxC is not exposed outside of Foobar which is the entry point
      Private Class MessageBoxC : Inherits Form
        'define cTor's as needed
        Public Sub New(message As String)
          Me.Text = message
        End Sub
        'define content
      End Class
    End Class