Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以在VB6中实现像NullableOfInteger这样的可空类型_Vb6_Nullable - Fatal编程技术网

是否可以在VB6中实现像NullableOfInteger这样的可空类型

是否可以在VB6中实现像NullableOfInteger这样的可空类型,vb6,nullable,Vb6,Nullable,只是想知道是否有人知道如何在VB6中实现一个类型化的可为null的类型,比如NullableOfInteger?(我试图避免使用变体) 您可以轻松创建自定义类NullableOfInteger,并使用其未初始化状态指示Null状态,但这有明显的缺点 除此之外,我真的想不出其他办法了?我的直觉告诉我没有好办法。VB6没有VB.NET中可空类型所利用的运算符重载或自定义隐式转换。你真的做得再好不过了 另一种方法是选择一个特定的值,并始终将该值视为null。在.NET 1.0时代,人们习惯于使用int

只是想知道是否有人知道如何在VB6中实现一个类型化的可为null的类型,比如NullableOfInteger?(我试图避免使用变体)

您可以轻松创建自定义类NullableOfInteger,并使用其未初始化状态指示Null状态,但这有明显的缺点


除此之外,我真的想不出其他办法了?我的直觉告诉我没有好办法。

VB6没有VB.NET中可空类型所利用的运算符重载或自定义隐式转换。你真的做得再好不过了


另一种方法是选择一个特定的值,并始终将该值视为null。在.NET 1.0时代,人们习惯于使用
int.MinValue
。我不知道VB6的等价物是什么,但我肯定有些东西。这是可行的,并没有听起来那么糟糕(但空类型更好)。

我想你回答了你自己的问题;Nullable是一个方便的工具-.NET有一个实现,VB6没有(主要是因为变量)。如果您想要VB6的类型安全版本,您必须实现它,而且许多人已经实现了-我记得,在数据库API中可以看到这种情况的常见位置。

这只是另一种观点

您可以使用可选

如果将其定义为
可选BLABLA as Integer
它将有一个默认值
0
,因此如果其为null或为空,u将有一个默认值
0

这是我为自己做的一个例子!它可能会派上用场:

用法:

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
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
也是这样

Dim TheThing As Long 

ProgressInc ProgressBar1 ,TheThing 
'See no definition about TheThing except being Long type
'cause of this its value is 0
Sub:

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
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
Public Sub ProgressInc(ProgressBarName作为ProgressBar,可选的最大长度,可选的最小长度,可选的Inc作为长度,可选的继续作为布尔值=False)
黯淡的
在错误上继续前进
ProgressBarName.show这是什么
多芬特
'最大进度条值
如果最大值为0,则
ProgressBarName.Max=Max
其他的
最大值=100
ProgressBarName.Max=Max
如果结束
'最小进度条值
如果最小值为0,则
ProgressBarName.Min=Min
其他的
最小值=1
ProgressBarName.Min=Min
如果结束
如果Inc 0,则Inc=Inc Else Inc=1
'当ProgressBar值为最大值时
'返回到最小值
如果Continues=True,ProgressBarName.Value=Max,则
ProgressBarName.Value=Min
如果结束
'签出最近进度(预计算条形图值)
最近=ProgressBarName.Value+Inc
如果最近>=最大值,则
'最近的值高于或等于最大值
'为避免此问题导致的错误,值应等于最大值
ProgressBarName.Value=Max
ElseIf最近<最大值
'最近(预先计算的条形图值)低于最大值
“所以这里没什么问题,继续。。
ProgressBarName.Value=ProgressBarName.Value+Inc
如果结束
出口接头
ProgressBarErr:
'ProgressBar错误报告。
MsgBox“带有”&Err.Number&“Number:”&Err.Description&“出现错误。”
端接头

看到了吧,我得到了Min,Max,Inc作为Long,当我没有定义它们时,它们的默认值是
0

你能改用VB.NET吗?或者一个
变量
类型?对于任何新的应用程序,我都使用C,但不幸的是,我被困在堆积如山的“遗留”VB6代码后面。我不知道VB6中有一个预定义的常量,但您始终可以使用
常量MININT32作为Long=&h8000000
常量MAXINT32作为Long=&H7FFFFFFF
常量MININT16作为Integer=&H8000
Const MAXINT16作为整数=&H7FFF
Const MININT8作为字节=0
Const MAXINT8作为字节=&HFF
。注意:
Long
在VB6中是一个32位整数<代码>整数是一个16位整数
Byte
是一个无符号的8位整数。如果0是变量的有效值,这有点可怕。我曾想过对我的布尔变量使用同样的方法,但后来我意识到,因为它默认为False,所以这是行不通的。