Reputation: 113
How can i control variable value during run time? I want control the variable value that if it larger than 10 project stop the run. The my variable repeat in my project and i myself do not check it. I want the project itself control it. How can i do it? like this this sub or ... control my variable.
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
Upvotes: 0
Views: 126
Reputation: 521
Please correct me if I'm wrong; I don't have Visual Studio at hand right now so I write from memory. It is possible there are some syntax errors, but the principle itself should be right.
If you aren't planning on creating such control variables at runtime, you could just create a property in some dummy module:
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
Upvotes: 1
Reputation: 4439
OK The only way I can think of is to create a class which raises an event if a number is outside the range defined when an instance is created.
Ok this is the class have a look at the comments to see how it works.
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
To create the "variable" add this to your code where you defined the variable that you want to monitor - naming it how you like of course
Dim WithEvents x As New MonitoredNumber(Of Integer)(4, 5)
In the above line the keyword WithEvents
makes sure that the OutOfRange event is handled by the event handler below.
Of Integer
should be changed to what type of number you want to keep track of. If it's a Double then change it to Of Double
etc.
The two parameters are the minimum and maximum acceptable values for the number - changed these to whatever you need.
In your form you finally need to add the event handler below for the OutOfRange
event.
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
So to use your code, define ypos as
Dim WithEvents ypos As New MonitoredNumber(Of Integer)(0, 10)
I'm presuming 0 as the minimum value here, but you can change it to whatever you like.
Then, in your event handler, you can write the code that you want to execute when the number is out of range.
To change the value of the number, use ypos.value=ypos.value+1 'to add one to it and so on
Hope this points you in the right direction. If you're declaring more than one instance of the MonitoredNumberClass, you should create another event handler for that instance of course.
If any other user has a better way, please feel free to write your own answer.
Upvotes: 2