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

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?
Miss leahdeveloper & project managerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peter HutchisonSenior Network Systems SpecialistCommented:
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.

Dim arr(100,100) as Integer
Dim r as integer, c as integer

For r=1  to 100
For c=1 to 100
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 managerAuthor 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 SpecialistCommented:
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.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Fernando SotoRetiredCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Miss leahdeveloper & project managerAuthor Commented:
thank you !
Fernando SotoRetiredCommented:
Not a problem Miss leah, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.