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

skij
skij used Ask the Experts™
on
<%

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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Paul JacksonSoftware Engineer
Top Expert 2011

Commented:
Try using the IsNothing() function:
<%

If Not IsNothing(XYZ) Then 

 Response.write(XYZ)

End If

%>

Open in new window

Paul JacksonSoftware Engineer
Top Expert 2011

Commented:
You could also use, which probably reads better:

<%

If XYZ Is Not Nothing Then 

 Response.write(XYZ)

End If

%>

Open in new window

Paul JacksonSoftware Engineer
Top Expert 2011

Commented:
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

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.
Paul JacksonSoftware Engineer
Top Expert 2011

Commented:
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" %>

Author

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?
Software Engineer
Top Expert 2011
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial