Comparing String with ASP.net

  • Thread starter Thread starter - Steve -
  • Start date Start date
S

- Steve -

The following line of vb.net code works fine:

if(strAnswer.toUpper() = strUserAnswer.toUpper())

However in an ASP.net page I'm told

Object reference not set to an instance of an object

What am I doing wrong?
 
Its likely that either strAnswer or strUserAnswer is null.

If performance matters, you might also want to use the String.Compare()
function with the case-insensitive flag, as it's faster.

--
Thanks,

Eric Lawrence
Program Manager
Assistance and Worldwide Services

This posting is provided "AS IS" with no warranties, and confers no rights.
 
The only possible thing that could be wrong in this case is that one of the
variable values is null. Those are the only 2 object references in the
statement.
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Okay I found the problem. Apparently I'm having troubles with how to use
global variables.

<script language="VBScript" runat="server>
dim strAnswer as String

sub getInfo()
strAnswer = <the code to get the answer>
end sub

sub validateAnswer()
if(strAnswer.toUpper() = strUserAnswer.toUpper())
etc . . . . .
end sub
</script>

Should the strAnswer from getInfo() be the same strAnswer in
validateAnswer()?


--

Steve Evans
Email Services
SDSU Foundation
 
Hi Steve,

First of all, that's not a global variable. It's a Private field of your
inherited Page Class (remember, this is object-oriented programming, not
procedural ASP). Secondly, it is not anything until you assign a value to it
(it is null, or Nothing in VB.Net). A field or variable declaration is
simply a declaration that a certain amount of memory space (the size of the
data type) should be allocated for storing that type of data. It has no
value until one is assigned to it.

Your error indicates that an object reference has been used that is null.
There are only 2 objects in the statement; both are strings. Apparently, you
are not assigning a value to at least one of them prior to invoking the Sub
that is comparing them.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top