Cannot convert to bool error in VB but not C#

  • Thread starter Thread starter Bill Dee
  • Start date Start date
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.
 
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
 
* "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.
 
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
 
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'.
 
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
 
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
 
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
 
Hey, Larry, are you making the move to VB.Net or just checking it out? Somehow, I didn't expect to see you here.
 
Back
Top