boolean expression

H

HS1

Hello

Could you please help for a simple boolean expression

If a is not equal to Null

I tried

If (a != null) Then

This expression does not work

I look at Help but can not find out

Could you please help

Thanks
S.Hoa
 
J

Josip Medved

Could you please help for a simple boolean expression
If a is not equal to Null
I tried
If (a != null) Then


From my point of view it seems that you are trying
to check whether object exists.

Try:
If Not (a Is Nothing) Then
 
G

Gerald Hernandez

What sort of "Null" are you looking for?
Database Null?
Object is Nothing?
a <> 0&?
 
H

HS1

Thank you
a is a value of a field in a table of a database. I have to check it is null
or not
I have a solution

If Not (a is DBNull.Value) Then
 
G

Gerald Hernandez

you got it, mostly.
i'm not certain if the IS keyword will give you the proper results or not.
this is not a value comparison, but an Object Reference comparison.
it would be safer to use:
If Not (a = DBNull.Value) Then

or use the alternate test using
If Not IsDBNull(a) Then

Gerald
 
H

HS1

Thank you

I tried to use =, the compiler get the error

Operator '=' is not defined for types 'System.Object' and 'System.DBNull'.
Use 'Is' operator to compare two reference types.
Therefore, I have to change to "Is"
I use VB.Net 2001

Regards
S.Hoa
 
G

Gerald Hernandez

What object type is "a"?
You say a database field, but which object.
Is there an a.value property?

I'm still concerned about using IS here.
Take for example that A is a particular object, and System.DBNull is another
object.
The IS operator is more appropriate to determine if "A" is the same object
as DBNull, not the same value.

Gerald
 
H

HS1

Here is my code

If (DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 5) Is DBNull.Value) Then

In this code, dataGrid1 presents data for a table. I check the value
(textual type) of current row at column 5 to see that it is Null or not.
 
G

Gerald Hernandez

Ah, I see. Well, if it were me, I would explicitly cast the underlying value
to its appropriate sqltype then check for null. However, I do not know what
kind of underlying data you are dealing with.
You could take the shortcut and use the Convert.IsDBNull which will handle
an expression.
Like so:

If Convert.IsDBNull(DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 5)) Then

It is possible that the way you are doing it will work fine. But it just
seems dangerous to me.

Gerald
 
H

HS1

Thank you

Gerald Hernandez said:
Ah, I see. Well, if it were me, I would explicitly cast the underlying value
to its appropriate sqltype then check for null. However, I do not know what
kind of underlying data you are dealing with.
You could take the shortcut and use the Convert.IsDBNull which will handle
an expression.
Like so:

If Convert.IsDBNull(DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 5)) Then

It is possible that the way you are doing it will work fine. But it just
seems dangerous to me.

Gerald

check
 

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

Similar Threads


Top