Cannot convert to bool error in VB but not C#

B

Bill Dee

In C# this line compiles and works fine:
if (Request.Params["something"] != null)

But when trying to convert this to VB.NET and changing it to:
if (Request.Params["something"] <> Nothing)
I get the following error: Compiler Error Message: BC30311: Value of type
'System.Collections.Specialized.NameValueCollection' cannot be converted to
'Boolean'

Anyone know why this works in C# but not VB? How do I fix this?

Basically I do not care what the value of the param is, I just want to know
whether its set to anything or not. If I try and check its value without
testing it for null, then I will receive an error from trying to access a
null object. So it seems I need a way to check for null in the first place,
but cannot figure out how to do this in VB.NET. Any ideas? Thank you.
 
A

Armin Zingler

Bill Dee said:
In C# this line compiles and works fine:
if (Request.Params["something"] != null)

But when trying to convert this to VB.NET and changing it to:
if (Request.Params["something"] <> Nothing)
I get the following error: Compiler Error Message: BC30311: Value of
type 'System.Collections.Specialized.NameValueCollection' cannot be
converted to 'Boolean'

Anyone know why this works in C# but not VB? How do I fix this?


Use the correct operator to compare references: Is

if bla is nothing then

or

if not bla is nothing then


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
H

Herfried K. Wagner [MVP]

* "Bill Dee said:
In C# this line compiles and works fine:
if (Request.Params["something"] != null)

But when trying to convert this to VB.NET and changing it to:
if (Request.Params["something"] <> Nothing)

That's not VB.NET code!!!

\\\
If Request.Params("something") Is Nothing Then
...
End If
///
I get the following error: Compiler Error Message: BC30311: Value of type
'System.Collections.Specialized.NameValueCollection' cannot be converted to
'Boolean'

Anyone know why this works in C# but not VB? How do I fix this?

Fix your code to make it compile in VB.NET.
 
C

Cor Ligthert

But when trying to convert this to VB.NET and changing it to:
if (Request.Params["something"] <> Nothing)

That's not VB.NET code!!!

\\\
If Request.Params("something") Is Nothing Then
...
End If
///
That is not good !!!!

:))))

Cor
 
H

Herfried K. Wagner [MVP]

Errata:
In C# this line compiles and works fine:
if (Request.Params["something"] != null)

But when trying to convert this to VB.NET and changing it to:
if (Request.Params["something"] <> Nothing)

That's not VB.NET code!!!

\\\
If Request.Params("something") Is Nothing Then

.... sorry, should read: 'If Not ... Is Nothing'.
 
C

Chris Dunaway

Use the correct operator to compare references: Is

if bla is nothing then

or

if not bla is nothing then

And soon to be availble in Whidbey: IsNot

if bla IsNot Nothing Then

or

'This one is strange!!
If Not bla IsNot Nothing Then
 
C

Cor Ligthert

if Not (Request.Params("something") Is Nothing then
... won't compile!

if Not Request.Params("something") Is Nothing then

You are right I was looking at it, and thought there is something wrong but
what.
I should have put it in the IDE however I did not.

Thanks

Cor
 
C

Cor Ligthert

Hi Chris,
'This one is strange!!
If Not bla IsNot Nothing Then

I find "object Is Something", much nicer

However what would be the equivalent in C#

Just my thoughts about it.

Cor
 
A

Al Reid

Hey, Larry, are you making the move to VB.Net or just checking it out? Somehow, I didn't expect to see you here.
 

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

Top