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

Re: Null value into an integer variable


For what it's worth, I concur with Jussi.
Here's a working example with a Gambas implementation (sorry for not
adhering to using the standard private variable prefixes, force of habit)
of other languages' "Nullable" as NullableInteger.class:

' Gambas class file

Private _hasValue As Boolean = False
Private _value As Integer

Property Read HasValue As Boolean
Property Value As Integer

Private Function HasValue_Read() As Boolean
  Return _hasValue
End

Private Function Value_Read() As Integer
  If (_hasValue) Then
    Return _value
  Else
    Error.Raise("Nullable integer has no value set yet.")
  Endif
End

Private Sub Value_Write(newValue As Integer)
  _hasValue = True
  _value = newvalue
End

And an example implementation of the class above; feel free to play with
commenting/uncommenting the lines setting values to vars nullInt1 and
nullInt2:

Public Sub Form_Open()

  Dim mssg As String = Null
  Dim nullInt1 As New NullableInteger
  Dim nullInt2 As New NullableInteger

  'nullInt1.Value = 10
  nullInt2.Value = 20

  If (nullInt1.HasValue) Then
    mssg &= "NullInt 1 is assigned: " & CStr(nullInt1.Value) & ",\n"
  Else
    mssg &= "NullInt 1 is assigned no value yet; you can safely assume it's
NULL,\n"
  Endif

  If (nullInt2.HasValue) Then
    mssg &= "NullInt 2 is assigned: " & CStr(nullInt2.Value)
  Else
    mssg &= "NullInt 2 is assigned no value yet; you can safely assume it's
NULL."
  Endif

  Message.Info(mssg, "OK")

  Me.Close()

End

If you try to read the .Value property before assigning it a value, an
error will be thrown on purpose as a property of type Integer (base-type)
cannot return Null.
Working with the .HasValue property you can control the program flow and
assign NULL to a database field if the Nullable Integer has no value,
instead of having "null flagging values" such as -1 for an age field, for
example. These "flag values" almost always backfire.

Regards,
zxMarce.

On Fri, Apr 19, 2024 at 10:22 AM Gianluigi <gradobag@xxxxxxxxxxx> wrote:

> Il 19/04/24 14:41, BB ha scritto:
> > Consider this.
> >
> > Public Sub Main()
> >
> >    Dim vIntero As Variant
> >
> >    Dim intval as Integer
> >
> >
> >    vIntero = 450000
> >    intval = IntValue(vIntero)
> >    vIntero = 2200000000
> >    intval= IntValue(vIntero)
> >    vIntero = -2200000000
> >    intval = IntValue(vIntero)
> >    vIntero = "PIPPO"
> >    intval = IntValue(vIntero)
> >
> > End
> >
> > See the difference?
> >
> > b
> >
> >
>
> What you wrote doesn't make much sense.
> We are talking generically here.
> Benoit has already explained that the Integer variable is not nullable,
> we need to find a 'workaround'.
> Jussi also seems to agree...
>
> Regards
> Gianluigi
>
>
>

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 variableGianluigi <gradobag@xxxxxxxxxxx>
Re: Null value into an integer variableBB <adamnt42@xxxxxxxxx>
Re: Null value into an integer variableBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>
Re: Null value into an integer variableBB <adamnt42@xxxxxxxxx>
Re: Null value into an integer variableGianluigi <gradobag@xxxxxxxxxxx>
Re: Null value into an integer variableBB <adamnt42@xxxxxxxxx>
Re: Null value into an integer variableGianluigi <gradobag@xxxxxxxxxxx>