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

时间:2015-01-03 04:40:39

标签: vb.net class constructor instance declare

我正在创建自己的消息框类(称为MessageBoxC,无论如何),就像System.Windows.Forms.MessageBox一样,我想让我的类没有构造函数,也没有可能声明它的新实例。 / p>

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

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

(**)这是我的意思。你不能创建一个MessageBox类的新实例,你会得到以下错误消息:“类型System.Windows.Forms.MessageBox没有构造函数。” 好吧,我的类也没有构造函数,但是可以声明它的实例。这里的诀窍是什么?

非常感谢!


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

2 个答案:

答案 0 :(得分:2)

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

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

Public Class MessageBoxC

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

End Class

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

答案 1 :(得分:1)

这是隐藏构造函数的一种方法 - 主要是因为有问题的类无法访问。

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