VB.NET Class 没有构造函数(不能声明新实例)

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

我正在制作我自己的消息框 class(称为 MessageBoxC,随便什么),就像 System.Windows.Forms.MessageBox,我想制作我的 class 没有构造函数,也没有可能声明它的一个新实例。

例如:

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

(*) 不重要。它只是计算文本大小(以像素为单位的宽度和高度)以在需要时更正表单大小,并且相应的标签获取 Me.Message 属性的值。

(**) 这个就是我的意思。您无法创建 MessageBox class 的新实例,您将收到以下错误消息:"Type System.Windows.Forms.MessageBox has no constructors." 好吧,我的 class 也没有构造函数,但是可以声明它的一个实例。这里有什么技巧?

非常感谢!


已解决。感谢 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

如果您不声明任何构造函数,则会自动创建一个默认构造函数(这是一个 public 没有参数的构造函数)。

为了防止任何人创建您的 class 的实例,您可以创建一个私有构造函数,如下所示:

Public Class MessageBoxC

    Private Sub New()
        ' Prevents anyone creating an instance of this class.
    End Sub

End Class

请注意,您的 Show 方法需要声明 Shared,否则您将无法调用它。事实上,即使使用您提供的代码,它也需要 Shared

这是隐藏构造函数的一种方法 - 主要是因为所讨论的 class 不可访问。

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