Test if variable defined without giving "Object reference not set to an instance of an object" error

<%

If (XYZ IsNot Nothing) Then 

 Response.write(XYZ)

End If

%>

Open in new window

This gives me an error:

Server Error in '/' Application.
Object reference not set to an instance of an object.


How can I test if XYZ is actually defined?
LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paul JacksonSoftware EngineerCommented:
Try using the IsNothing() function:
<%

If Not IsNothing(XYZ) Then 

 Response.write(XYZ)

End If

%>

Open in new window

0
Paul JacksonSoftware EngineerCommented:
You could also use, which probably reads better:

<%

If XYZ Is Not Nothing Then 

 Response.write(XYZ)

End If

%>

Open in new window

0
Paul JacksonSoftware EngineerCommented:
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

skijAuthor Commented:
That does not work.  I get an error:
Compiler Error Message: BC30451: Name 'XYZ' is not declared.

The problem is that XYZ has not been defined, so checking to see if it is empty causes an error.  
I need to test if it has been defined NOT if it is empty.
0
Paul JacksonSoftware EngineerCommented:
That error message says it has not been declared not that it has not been defined, there is a difference.
You won't be able to test something if it is not declared.
Testing against Nothing tests whether a reference points to an object not whether the reference exists in the first place.


Can I ask what is XZY and in what situation would it not de declared in the code, not sure I understand the scenario where you need to test against something that isn't declared.

To get rid of the compiler error you have you could turn off option strict in web.config but that is generally not considered good practice:
Change the strict option to false in web.config.

<compilation debug="true" strict="false" explicit="true"/>


You could also turn if off for just that one page by changing the line at the top of the aspx page :


<%@ Page Language="VB" strict="False" %>
0
skijAuthor Commented:
There is "code behind" code that declares XYZ if certain conditions are met.  The "code behind" code is compiled and I don't have access to it.  I need to test if that code has already been declared or not.  If there is no other way, could "try" and "catch" be used?
0
Paul JacksonSoftware EngineerCommented:
Sure you could :

<%
Try
    If XYZ Is Not Nothing Then 

       Response.write(XYZ)

    End If
Catch ex As Exception
      ' Do something or Ignore here
End Try
%>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.