Operator '=' is not defined for type 'DBNull' and type 'Integer'

Miss leah
Miss leah used Ask the Experts™
on
I am migrating vb6 project to vb.net. there are variant type arrays that I migrated as object types.
having this code line I get the error: Operator '=' is not defined for type 'DBNull' and type 'Integer'
                    If arrY(13, X) = gloT And (CBool(arrY(18, X)) = True Or (CBool(arrY(18, X)) = False And arr(65, X) = 100)) Then  
arr(65, X) has a null value . If I cannt initialize it to 0 how can I avoid the exception?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Peter HutchisonSenior Network Systems Specialist

Commented:
To be honest, it would be best practise try to avoid using Object when migrating to VB.Net and you should always initialise arrays before using them, as you cannot guarantee what is in the values before using them.

e.g.
Dim arr(100,100) as Integer
Dim r as integer, c as integer

For r=1  to 100
For c=1 to 100
arr(r,c)=0
Next c
Next r

Alternatively, you could modify the values in the condition:

f arrY(13, X) = gloT And (CBool(arrY(18, X)) = True Or (CBool(arrY(18, X)) = False And (arr(65, X) + 0) = 100)) Then
Miss leahdeveloper & project manager

Author

Commented:
Thank you,  I initialize the array but the vb6 code is using the null value after the initialize to 0. I assume there is a significance to that. I tried to use +0 method you mentioned but still getting the error: Operator '+' is not defined for type 'DBNull' and type 'Integer'
Peter HutchisonSenior Network Systems Specialist

Commented:
How do you set up the values from the array, do you read them from a database or a file.

I find that, you should read values as strings (add a empty string to covert from Null to a value) and then try to convert to a integer value (using VAL or CInt or whatever) before assigning to the array.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Retired
Distinguished Expert 2017
Commented:
Hi Hagar;

The DbNull is the a non existent value used in databases for not having a value. When you attempt to use a variable that may contain a DbNull you need to test to see if the variable has a valid value first. Something like the following can be used.
Dim arr65 = If( DBNull.Value.Equals(arr(65, X)), 0, arr(65, X))
If arrY(13, X) = gloT And (CBool(arrY(18, X)) = True Or (CBool(arrY(18, X)) = False And arr65 = 100)) Then

Open in new window

What this does is check to see if arr(65, X) has a valid value if it is a DBNull it assign 0 to the variable arr65 other wise it assigns the valid value to arr65.
Miss leahdeveloper & project manager

Author

Commented:
thank you !
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem Miss leah, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial