Comparing String with ASP.net

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?
 
E

Eric Lawrence [MSFT]

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.
 
K

Kevin Spencer

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.
 
S

- Steve -

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
 
K

Kevin Spencer

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top