Answered by:
How to check if a variable is holding any reference?

Question
-
Hello,
How to check whether a variable is actually holding a reference or not?
For example, suppose that I have declared [just declared not instantiated] 'b' as a button and later in the code I want to check if b is holding any reference a Button object. How to do this?
Thanks,
Raraju.
Wednesday, July 15, 2009 3:47 PM
Answers
-
Hi,
You can also useif (b IsNot Nothing) Then 'Holds a Reference EndIf
Hope it helps
FEAR NOT TO BE JUST Please mark posts as answers/helpful if it answers your query- Proposed as answer by Rahul P Nath Wednesday, July 15, 2009 4:19 PM
- Marked as answer by Raraju Wednesday, July 15, 2009 4:24 PM
Wednesday, July 15, 2009 4:18 PM
All replies
-
if (b != null)
{
// it's holding a reference.
}
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client- Proposed as answer by Rahul P Nath Wednesday, July 15, 2009 4:18 PM
Wednesday, July 15, 2009 3:54 PM -
if (b != null)
{
// it's holding a reference.
}
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
Thanks David. Bu can you let know how to do this in VB? Because I tried the following, but they didn't work!
IF b=Nothing -> it says that '=' operator is not defined for Button and Nothing
IF b.Equals(Nothing) -> if b doesn't have an object reference, this will throw an exception
Any other ideas?
-RarajuWednesday, July 15, 2009 4:13 PM -
if (b != null)
{
// it's holding a reference.
}
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
Thanks David. Bu can you let know how to do this in VB? Because I tried the following, but they didn't work!
IF b=Nothing -> it says that '=' operator is not defined for Button and Nothing
IF b.Equals(Nothing) -> if b doesn't have an object reference, this will throw an exception
Any other ideas?
-Raraju
If b <> Nothing Then
' it's holding a reference.
End IfAlso, I believe in VB, you can use "If b Is Nothing"
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums ClientOr,- Proposed as answer by Rahul P Nath Wednesday, July 15, 2009 4:18 PM
Wednesday, July 15, 2009 4:16 PM -
Hi,
You can also useif (b IsNot Nothing) Then 'Holds a Reference EndIf
Hope it helps
FEAR NOT TO BE JUST Please mark posts as answers/helpful if it answers your query- Proposed as answer by Rahul P Nath Wednesday, July 15, 2009 4:19 PM
- Marked as answer by Raraju Wednesday, July 15, 2009 4:24 PM
Wednesday, July 15, 2009 4:18 PM -
if (b != null)
{
// it's holding a reference.
}
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
Thanks David. Bu can you let know how to do this in VB? Because I tried the following, but they didn't work!
IF b=Nothing -> it says that '=' operator is not defined for Button and Nothing
IF b.Equals(Nothing) -> if b doesn't have an object reference, this will throw an exception
Any other ideas?
-Raraju
If b <> Nothing Then
' it's holding a reference.
End IfAlso, I believe in VB, you can use "If b Is Nothing"
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client Or,
If b<> Nothing -> Operator '<>' is not defined for types 'System.Windows.Controls.Button' and 'System.Windows.Controls.Button'.
-RarajuWednesday, July 15, 2009 4:23 PM -
Hi,
You can also useif (b IsNot Nothing) Then 'Holds a Reference EndIf
Hope it helps
FEAR NOT TO BE JUST Please mark posts as answers/helpful if it answers your query
It worked!! Thanks Rahul.
-RarajuWednesday, July 15, 2009 4:23 PM