1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
'
' FlowControl.vb
'
' Author:
' Mizrahi Rafael (rafim@mainsoft.com)
'
'
' Copyright (C) 2002-2006 Mainsoft Corporation.
' Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
'
' Permission is hereby granted, free of charge, to any person obtaining
' a copy of this software and associated documentation files (the
' "Software"), to deal in the Software without restriction, including
' without limitation the rights to use, copy, modify, merge, publish,
' distribute, sublicense, and/or sell copies of the Software, and to
' permit persons to whom the Software is furnished to do so, subject to
' the following conditions:
'
' The above copyright notice and this permission notice shall be
' included in all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
' MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
' LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
' OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Imports System
Namespace Microsoft.VisualBasic.CompilerServices
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)> _
Public NotInheritable Class FlowControl
' LoopFor reference object
Private Class LoopForObject
Public Start As Decimal
Public Limit As Decimal
Public StepValue As Decimal
End Class
' Counter reference object
'Private CounterObject As Object
Private Sub New()
'Nobody should see constructor
End Sub
Public Shared Sub CheckForSyncLockOnValueType(ByVal obj As Object)
'obj can not be null (checked by monitor.enter)
Dim t As Type = obj.GetType()
If (t.IsValueType) Then
Throw New ArgumentException("'SyncLock' operand cannot be of type '" + t.Name + "' because '" + t.Name + "' is not a reference type.")
End If
End Sub
Public Shared Function ForEachInArr(ByVal ary As System.Array) As System.Collections.IEnumerator
Throw New NotImplementedException
End Function
Public Shared Function ForEachInObj(ByVal obj As Object) As System.Collections.IEnumerator
Throw New NotImplementedException
End Function
Public Shared Function ForEachNextObj(ByRef obj As Object, ByVal enumerator As System.Collections.IEnumerator) As Boolean
Throw New NotImplementedException
End Function
' Create and initialize LoopForObject and CounterObject reference objects
' returns an indication whether the code flow should enter the for-loop statement.
' CounterResult: Common code paths are having Counter and CounterResult as the same object.
' so Counter and CounterResult are casted to Decimal and not to some new private class.
Public Shared Function ForLoopInitObj(ByVal Counter As Object, _
ByVal Start As Object, _
ByVal Limit As Object, _
ByVal StepValue As Object, _
ByRef LoopForResult As Object, _
ByRef CounterResult As Object) _
As Boolean
' set the Counter as Start
Counter = Convert.ToDecimal(DecimalType.FromObject(Start))
'
' insert the loop parameters into LoopForObject
'
Dim lfo As New LoopForObject
' Check the Object types (i.e. string) and convert them to Decimal
lfo.Start = Convert.ToDecimal(DecimalType.FromObject(Start))
lfo.Limit = Convert.ToDecimal(DecimalType.FromObject(Limit))
lfo.StepValue = Convert.ToDecimal(DecimalType.FromObject(StepValue))
' set the out reference
LoopForResult = lfo
' set the out reference
CounterResult = Counter
Return ForNextCheckDec(Convert.ToDecimal(CounterResult), lfo.Limit, lfo.StepValue)
End Function
'
' returns an indication whether the code flow should proceed with the next for-loop iteration.
'
Public Shared Function ForNextCheckDec(ByVal count As Decimal, ByVal limit As Decimal, ByVal StepValue As Decimal) As Boolean
'if StepValue is positive or negative
If StepValue > 0 Then
If count <= limit Then
Return True
Else
Return False
End If
Else
If count >= limit Then
Return True
Else
Return False
End If
End If
End Function
'
' Add a StepValue to the Counter and set its value into CounterResult
' returns an indication whether the code flow should proceed with the next for-loop iteration.
'
Public Shared Function ForNextCheckObj(ByVal Counter As Object, ByVal LoopObj As Object, ByRef CounterResult As Object) As Boolean
' Counter is a CounterObject
Dim d_Counter As Decimal = DecimalType.FromObject(Counter)
Dim lfo As LoopForObject = DirectCast(LoopObj, LoopForObject)
Dim d_CounterResult As Decimal = DecimalType.FromObject(CounterResult)
' increment the counter with a step
d_CounterResult = d_Counter + lfo.StepValue
' set the reference
CounterResult = d_CounterResult
' check if there is another iteration
Return ForNextCheckDec(d_CounterResult, lfo.Limit, lfo.StepValue)
End Function
Public Shared Function ForNextCheckR4(ByVal count As Single, ByVal limit As Single, ByVal StepValue As Single) As Boolean
'if StepValue is positive or negative
If StepValue > 0 Then
If count <= limit Then
Return True
Else
Return False
End If
Else
If count >= limit Then
Return True
Else
Return False
End If
End If
End Function
Public Shared Function ForNextCheckR8(ByVal count As Double, ByVal limit As Double, ByVal StepValue As Double) As Boolean
'if StepValue is positive or negative
If StepValue > 0 Then
If count <= limit Then
Return True
Else
Return False
End If
Else
If count >= limit Then
Return True
Else
Return False
End If
End If
End Function
End Class
End Namespace
|