开发者

How to create instance of class in different methods

Do I have to instantiate description every time for different method? Or should I use static? Here's how I'm doing this now: What is the best way of handling this kind of situations. it seems that I repeat this line:Dim description As BLLDescription = New BLLDescription() without any good reasn.

   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.GetAllDescriptio开发者_Python百科ns()
           .....
        End Sub


IF you define BLLDescription as a static class, you can call the GetAllDescriptions() method without having to instantiate:

 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


It depends on what the instance of BLLDescription does and how it gets the data.

If it is accessing the same data again and again, you can declare it as static. If it gets same data per request, then have it as a property at the class level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜