[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Null value into an integer variable


Am 19.04.24 um 11:20 schrieb BB:
On 19/4/24 6:25 pm, Mayost Sharon wrote:

Hello


Public Sub Main()
   Dim sVar As String
   Dim iVar As Integer

   sVar = "gb"
   iVar = 3
   sVar = Null
   iVar = Null
End


When I put an Null value into a variable of type string I don't get an error and the variable gets a value of ""

But when I put an Null value into a variable of integer type it gives an error

Is this normal?

Thank you


Very much so! A string of zero length is considered a null or vice versa a null is considered a string of zero length. But consider the internal (memory) representation of an integer. There is no way to represent a null value.

This is of course a big PIA when dealing with databases where integer columns can contain nulls and that has a very specific meaning i.e. "no value has been set". I have to date, after some time considering this, have not arrived at a viable solution.

Why do you want to set an integer variable to Null?

b





Usually I try to catch this case by checking for "" and IsNull(Val(...)) like

If sMyString = "" Then
  iMyInteger = 0
Else
  If IsNull(Val(sOriginal)) Then
    iMyInteger = 0
  Else
    iMyInteger = Val(sOriginal)
  End If
End If

to be completely sure.

Regards
Rolf


Follow-Ups:
Re: Null value into an integer variableBB <adamnt42@xxxxxxxxx>
References:
GRIDVIEW color and size"Mayost Sharon" <sharon@xxxxxxxxx>
Re: GRIDVIEW color and sizeT Lee Davidson <t.lee.davidson@xxxxxxxxx>
Re: GRIDVIEW color and sizeT Lee Davidson <t.lee.davidson@xxxxxxxxx>
Re: GRIDVIEW color and size"Mayost Sharon" <sharon@xxxxxxxxx>
Re: GRIDVIEW color and sizeT Lee Davidson <t.lee.davidson@xxxxxxxxx>
Null value into an integer variable"Mayost Sharon" <sharon@xxxxxxxxx>
Re: Null value into an integer variableBB <adamnt42@xxxxxxxxx>