如何用不同的方法创建类的实例

时间:2011-08-03 20:23:25

标签: asp.net vb.net

我是否必须每次为不同的方法实例化描述?或者我应该使用静态?我现在就是这样做的:处理这种情况的最佳方法是什么。似乎我重复了这一行:Dim description As BLLDescription = New BLLDescription()没有任何好处。

   Protected Sub Button8_Click(sender As Object, e As System.EventArgs) Handles Button8.Click
            Dim description As BLLDescription = New BLLDescription()
           List<String> = description.GetDescriptionWithoutNotes()
           .....
        End Sub

  Protected Sub Button9_Click(sender As Object, e As System.EventArgs) Handles Button9.Click
        Dim description As BLLDescription = New BLLDescription()
       List<String> = description.GetDescriptionWithNotes()
       .....
    End Sub
 Protected Sub Button10_Click(sender As Object, e As System.EventArgs) Handles Button10.Click
            Dim description As BLLDescription = New BLLDescription()
           List<String> = description.GetAllDescriptions()
           .....
        End Sub

2 个答案:

答案 0 :(得分:0)

如果将BLLDescription定义为静态类,则可以调用GetAllDescriptions()方法而无需实例化:

 Protected Sub Button8_Click(sender As Object, e As System.EventArgs) Handles Button8.Click
           List<String> = BLLDescription.GetDescriptionWithoutNotes()
           .....
        End Sub

  Protected Sub Button9_Click(sender As Object, e As System.EventArgs) Handles Button9.Click
       List<String> = BLLDescription.GetDescriptionWithNotes()
       .....
    End Sub
 Protected Sub Button10_Click(sender As Object, e As System.EventArgs) Handles Button10.Click
           List<String> = BLLDescription.GetAllDescriptions()
           .....
        End Sub

答案 1 :(得分:0)

这取决于BLLDescription的实例以及它如何获取数据。

如果一次又一次地访问相同的数据,您可以将其声明为静态。如果每个请求获得相同的数据,则将其作为类级别的属性。