setting to Nothing

M

Mark Kamoski

Hi--


Is there any significant difference between the code samples below?

If so, what is it? And, which is preferred?

Also, is there ANY need for setting to Nothing ever in a case like this?

(Put aside discussion of error handling, for now.)

Please advise.

Thank you

--Mark


'''''''''''''''''''''''''''''Sample 1

Private Sub BindPopupExBKDS()

Dim objButton As Button

objButton = Nothing
objButton = New Button()
objButton = CType(pnlJobSheetDetailBKDS.FindControl("btnDetailAdd1"),
Button)
objButton.Attributes.Add("onclick", "return ShowPopup();")

objButton = Nothing
objButton = New Button()
objButton = CType(pnlJobSheetDetailBKDS.FindControl("btnDetailAdd2"),
Button)
objButton.Attributes.Add("onclick", "return ShowPopup();")

End Sub


'''''''''''''''''''''''''''''''''Sample 2


Private Sub BindPopupExBKDS()

Dim objButton As Button

objButton = New Button()
objButton = CType(pnlJobSheetDetailBKDS.FindControl("btnDetailAdd1"),
Button)
objButton.Attributes.Add("onclick", "return ShowPopup();")

objButton = New Button()
objButton = CType(pnlJobSheetDetailBKDS.FindControl("btnDetailAdd2"),
Button)
objButton.Attributes.Add("onclick", "return ShowPopup();")

End Sub

''''''''''''''''''''Sample 3

Private Sub BindPopupExBKDS()

Dim objButton As Button

objButton = CType(pnlJobSheetDetailBKDS.FindControl("btnDetailAdd1"),
Button)
objButton.Attributes.Add("onclick", "return ShowPopup();")

objButton = CType(pnlJobSheetDetailBKDS.FindControl("btnDetailAdd2"),
Button)
objButton.Attributes.Add("onclick", "return ShowPopup();")

End Sub
 
R

Robert Jacobson

The first two examples are needlessly inefficient. When you use this code:

objButton = New Button()
objButton = CType(pnlJobSheetDetailBKDS.FindControl("btnDetailAdd1")

the first line is creating a new Button object (with default properties),
assigning it to objButton, and placing it on the heap. The second line
_reassigns_ objButton to an existing button (btnDetailAdd1), which causes
the first button to go out of scope, so it will be garbage collected. In
other words, you're creating a new Button object that just pops in and out
of existance very quickly (taking up extra processor cycles and memory),
before you assign objButton to btnDetailAdd1.

Also, as Jeremy said, there's no need to explicitly use "Nothing" in your
examples. When you "Dim" an object reference, it is set to Nothing by
default. The second line in this code is redundant:

Dim objButton As Button
objButton = Nothing

--Robert Jacobson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top