Is it possible to implement a Nullable Type like NullableOfInteger in VB6
Just wondering if anyone knows of a way to implement a Typed Nullable Type like Null开发者_高级运维ableOfInteger in VB6? (I'm trying to avoid using variants)
You can easily create a custom class NullableOfInteger and use its uninitialized state to indicate a Null state but this comes with the obvious disadvantages.
Beyond that I can't really think of any other ways? My gut tells me there would be no good way.
VB6 doesn't have the operator overloading or custom implicit casting that nullable types in VB.NET utilize. You really can't do it any better than variant.
An alternative is to choose a specific value and consistently treat that value as null. In .NET 1.0 days people used to use int.MinValue
. I don't know what the VB6 equivalent is but I'm sure there's something. This works and is not nearly as bad as it sounds (but nullable types are better).
I think you answered your own question ; Nullable is a convenience - .NET has an implementation, VB6 doesn't (largely because of Variant). If you want a type-safe version for VB6, you have to implement it, and many have - I recall that a common place to see this sort of thing was in database APIs.
Just another point of view
Instead of Nullable you can handle this by using Optional
If you define it as Optional BLABLA As Integer
it will have a default 0
value so if its null or empty u will have a default value as 0
..
Here is an example that i made for my self! It might come in handy:
Usage:
ProgressInc ProgressBar1 'you can add other options if you want as shown below
'ProgressInc ProgressBar1, 500, 50, 25, True
'I always change Min value to 1 in my ProgressInc so if you even choose it as 0 it still gonna be 1
also works this way
Dim TheThing As Long
ProgressInc ProgressBar1 ,TheThing
'See no definition about TheThing except being Long type
'cause of this its value is 0
Sub:
Public Sub ProgressInc(ProgressBarName As ProgressBar, Optional Max As Long, Optional Min As Long, Optional Inc As Long, Optional Continues As Boolean = False)
Dim Recent As Long
On Err GoTo ProgressBarErr
ProgressBarName.ShowWhatsThis
DoEvents
'Maximum ProgressBar Value
If Max <> 0 Then
ProgressBarName.Max = Max
Else
Max = 100
ProgressBarName.Max = Max
End If
'Minimum ProgressBar Value
If Min <> 0 Then
ProgressBarName.Min = Min
Else
Min = 1
ProgressBarName.Min = Min
End If
If Inc <> 0 Then Inc = Inc Else Inc = 1
'When the ProgressBar value is at Maximum
'Return to the Minimum value
If Continues = True And ProgressBarName.Value = Max Then
ProgressBarName.Value = Min
End If
'Checkout Recent progress (pre calculate bar value)
Recent = ProgressBarName.Value + Inc
If Recent >= Max Then
'Recent value is higher than or equals to Max value
'to avoid errors caused by this issue Value should equal to Max
ProgressBarName.Value = Max
ElseIf Recent < Max Then
'Recent(pre calculated bar value) is lower than Max
'So nothing wrong here, proceed..
ProgressBarName.Value = ProgressBarName.Value + Inc
End If
Exit Sub
ProgressBarErr:
'ProgressBar error report.
MsgBox "With " & Err.Number & " number : '" & Err.Description & "' error occured. "
End Sub
see up there im getting Min, Max, Inc As Long and when i dont define them they'r having 0
as their default value.
精彩评论