Vb.net 如何在运行时控制变量的值

Vb.net 如何在运行时控制变量的值,vb.net,variables,controls,Vb.net,Variables,Controls,如何在运行时控制变量值?我想控制变量值,如果它大于10,项目停止运行。 我的变量在我的项目中重复,我自己不检查它。我希望项目本身控制它。 我怎么做? 这样地 这艘潜艇或。。。控制我的变量 sub or ... control_ypos() if ypos>10 then exit environment end if end sub sub main() for i=0 to myarray1.length-1 ypos +=

如何在运行时控制变量值?我想控制变量值,如果它大于10,项目停止运行。 我的变量在我的项目中重复,我自己不检查它。我希望项目本身控制它。 我怎么做? 这样地 这艘潜艇或。。。控制我的变量

sub or  ... control_ypos()
      if ypos>10 then
           exit environment
      end if
end sub
sub main()
  for i=0 to myarray1.length-1
      ypos +=i
  next
  for i=0 to myarray2.length-1
      ypos +=i
  next
....
end sub

好的,我能想到的唯一方法是创建一个类,如果一个数字超出创建实例时定义的范围,该类将引发一个事件

好的,这是一节课,看一下注释,看看它是如何工作的

Friend Class MonitoredNumber(Of t As IComparable)
    ' Using of T means that the variable can be any type - Integer, Single, Double etc
    Private MonitoredValue As t
    Private _minValue As t
    Private _maxValue As t

    'this creates a an event handler
    Public Event OutOfRange()

    'this is the constructor that you will use when you create the "Variable" even though it is actually an instance of the MonitoredNumber class
    'and it defines the minimum and maximum values
    Public Sub New(min As t, max As t)
        _minValue = min
        _maxValue = max
    End Sub

    Public Property Value As t
        Get
            Return MonitoredValue
        End Get
        Set(value As t)
            MonitoredValue = value
            'if the "variable is set to a value outside the defined range, then the OutOfRangeEvent is raised
            If MonitoredValue.CompareTo(_minValue) < 0 Or MonitoredValue.CompareTo(_maxValue) > 0 Then
                RaiseEvent OutOfRange()
            End If
        End Set
    End Property

End Class
在上面的一行中,关键字
WithEvents
确保OutOfRange事件由下面的事件处理程序处理

整数的
应更改为要跟踪的数字类型。如果是双精度,则将其更改为双精度的

这两个参数是数字的最小和最大可接受值-将它们更改为您需要的任何值

在表单中,您最终需要为
OutOfRange
事件添加下面的事件处理程序

Private Sub MonitoredNumberChanged() Handles x.OutOfRange
    'do stuff here to do what you want to do if the event is out of range
    MessageBox.Show("Value out of range. Value =" & x.Value.ToString)
End Sub
因此,要使用您的代码,请将ypos定义为

Dim WithEvents ypos As New MonitoredNumber(Of Integer)(0, 10)
我假设0是这里的最小值,但是你可以随意改变它

然后,在事件处理程序中,您可以编写数字超出范围时要执行的代码

要更改数字的值,请使用 ypos.value=ypos.value+1'向其中添加一个,依此类推

希望这能为你指明正确的方向。如果要声明MonitoredNumber类的多个实例,当然应该为该实例创建另一个事件处理程序


如果其他用户有更好的方法,请随意编写您自己的答案。

好的,我能想到的唯一方法是创建一个类,如果一个数字超出创建实例时定义的范围,该类将引发一个事件

好的,这是一节课,看一下注释,看看它是如何工作的

Friend Class MonitoredNumber(Of t As IComparable)
    ' Using of T means that the variable can be any type - Integer, Single, Double etc
    Private MonitoredValue As t
    Private _minValue As t
    Private _maxValue As t

    'this creates a an event handler
    Public Event OutOfRange()

    'this is the constructor that you will use when you create the "Variable" even though it is actually an instance of the MonitoredNumber class
    'and it defines the minimum and maximum values
    Public Sub New(min As t, max As t)
        _minValue = min
        _maxValue = max
    End Sub

    Public Property Value As t
        Get
            Return MonitoredValue
        End Get
        Set(value As t)
            MonitoredValue = value
            'if the "variable is set to a value outside the defined range, then the OutOfRangeEvent is raised
            If MonitoredValue.CompareTo(_minValue) < 0 Or MonitoredValue.CompareTo(_maxValue) > 0 Then
                RaiseEvent OutOfRange()
            End If
        End Set
    End Property

End Class
在上面的一行中,关键字
WithEvents
确保OutOfRange事件由下面的事件处理程序处理

整数的
应更改为要跟踪的数字类型。如果是双精度,则将其更改为双精度的

这两个参数是数字的最小和最大可接受值-将它们更改为您需要的任何值

在表单中,您最终需要为
OutOfRange
事件添加下面的事件处理程序

Private Sub MonitoredNumberChanged() Handles x.OutOfRange
    'do stuff here to do what you want to do if the event is out of range
    MessageBox.Show("Value out of range. Value =" & x.Value.ToString)
End Sub
因此,要使用您的代码,请将ypos定义为

Dim WithEvents ypos As New MonitoredNumber(Of Integer)(0, 10)
我假设0是这里的最小值,但是你可以随意改变它

然后,在事件处理程序中,您可以编写数字超出范围时要执行的代码

要更改数字的值,请使用 ypos.value=ypos.value+1'向其中添加一个,依此类推

希望这能为你指明正确的方向。如果要声明MonitoredNumber类的多个实例,当然应该为该实例创建另一个事件处理程序

如果其他用户有更好的方法,请随意写下自己的答案。

如果我错了,请纠正我;我现在手头没有VisualStudio,所以我用记忆写作。可能存在一些语法错误,但原则本身应该是正确的

如果您不打算在运行时创建此类控制变量,您可以在某个虚拟模块中创建一个属性:

Module Dummy
    Dim _ypos As Integer = 0
    Public Property control_ypos As Integer
        Get
            Return _ypos
        End Get
        Set
            If _ypos>10 Then 'Do the checking stuff
                End
            Else
                _ypos = value
            End If
        End Set
    End Property
End Module

Sub Main()
    For I=0 To myarray1.Length-1
        Dummy.control_ypos += I
    Next
    For I=0 to myarray2.Length-1
        Dummy.control_ypos += I
    Next
    ...
End Sub
如果我错了,请纠正我;我现在手头没有VisualStudio,所以我用记忆写作。可能存在一些语法错误,但原则本身应该是正确的

如果您不打算在运行时创建此类控制变量,您可以在某个虚拟模块中创建一个属性:

Module Dummy
    Dim _ypos As Integer = 0
    Public Property control_ypos As Integer
        Get
            Return _ypos
        End Get
        Set
            If _ypos>10 Then 'Do the checking stuff
                End
            Else
                _ypos = value
            End If
        End Set
    End Property
End Module

Sub Main()
    For I=0 To myarray1.Length-1
        Dummy.control_ypos += I
    Next
    For I=0 to myarray2.Length-1
        Dummy.control_ypos += I
    Next
    ...
End Sub


使用
End
停止关闭整个程序,而不是
Exit Environment
。如果您只想退出子系统,请使用
exit sub
。如果要退出的
。。接下来
循环,
退出
感谢您的回复…但我的问题不是退出环境或类似的情况…我的问题是在运行时自动检查变量值。确定。您希望在代码执行过程中以固定的时间间隔或特定的时间点检查哪个变量?变量值在运行过程中经常更改,并且没有特定的时间点。我想知道project是否可以自动检查变量值?使用
结束
停止关闭整个程序,而不是
退出环境
。如果您只想退出子系统,请使用
exit sub
。如果要退出的
。。接下来
循环,
退出
感谢您的回复…但我的问题不是退出环境或类似的情况…我的问题是在运行时自动检查变量值。确定。您希望在代码执行期间的固定时间间隔或特定时间点检查哪个变量?变量值在运行期间经常更改,并且没有特定时间点。我想知道project是否可以自动检查变量值?谢谢您的帮助reply@MDA,很高兴它起到了作用,尽管不如另一个好。请注意,在这里,在StackOverflow上,不允许发表诸如“谢谢!”、“+1”、“我也是!”之类的评论。如果你觉得这个答案很有用,就直接投票吧是的,你是对的,但我不允许投票给你的答案,因为我的声誉不到15。谢谢你的支持reply@MDA,很高兴它起到了作用,尽管不如另一个好。请注意,在这里,在StackOverflow上,不允许发表诸如“谢谢!”、“+1”、“我也是!”之类的评论。如果你觉得这个答案很有用,就直接投票吧是的,你是对的,但我不允许投票表决你的答案,因为我的声誉不到15。谢谢你…我认为这更好,这是我的答案。使用抽象lambda谓词而不是最小/最大值可能是更好的主意,如果传统的话,会重载默认构造函数。这也提高了l