i have this code below
if a >= 0 and a <= 100 then
'do something
end if
is it posible to write like this
if a >= 0 < 100 then
'do something
endif
is there a similar way?
i have this code below
if a >= 0 and a <= 100 then
'do something
end if
is it posible to write like this
if a >= 0 < 100 then
'do something
endif
is there a similar way?
yes.do it like the following...
then 100 =< and 0 => a If
'don;t do anything
if end
this like write to posiible is is
Note:-
Execuse my rather weak English,but not my logic!!!
Right there are 2 ways of doing this.
The first way is to use two If statements as follows:
If a >= 0 Then
If a <= 100 Then
'do something
End If
End If
The other method which uses fewer lines of code is to include an AND statement:
If a >= 0 And a <= 100 Then
'do something
End If
Hope this helps
I think the clause you're looking for is:
If 0<= a <= 100 then
i.e. 0 is less than or equal to "a" which is less than or equal to 100
VB.net should be able to handle this although you may have to use parenthesis:
If (0<= a <=100) then
AND, ANDALSO, OR and ORELSE are used for testing two conditions but "0<=a<=100" is one logical condition
G waddell forgive me if im wrong but A being greater than 0 and a being less than 100 is two conditions.
VB.net should be able to handle this although you may have to use parenthesis: If (0<= a <=100) then AND, ANDALSO, OR and ORELSE are used for testing two conditions but "0<=a<=100" is one logical condition
This will compile and run if "Option Strict" is off (one of the many reasons to always set Option Strict On), but it will always equate to True.
"0 < a" will be evaluated first and result in a boolean value; this value will be converted to a numeric value of either 0 (false) or -1 (true) and the compared against "< 100". Both 0 and -1 are less than 100, hence the true result.
So the short version is
If 0 <= a <= 100 Then
is equivalent to
If (0 <= a) <= 100 Then
which is NOT the same as
If 0 <= a And a <= 199 Then
So the short version is If 0 <= a <= 100 Then is equivalent to If (0 <= a) <= 100 Then which is NOT the same as If 0 <= a And a <= 199 Then
Correct. The first conparison evaluates to a boolean result. It is no different than:
Dim b as Boolean = 0 < a
If b < 100 Then 'again assuming Option Strict Off
or
If True < 100 Then
or
If False < 100 Then
For those interested, here are some "Between" extensions from one of my pet projects. Beware that these have not been fully tested yet.
Module myExtensions
#Region "Numeric ValueType Between Methods"
'ToDo: Finish stress testing Between. Verify no loss of magnitude for conversion to Double or Decimal.
'Reference: Widening and Narrowing Conversions
' http://msdn.microsoft.com/en-us/library/k1e94s7e.aspx
''' <summary>Not Valid for nonnumeric value types including Boolean and user defined Structures. Will throw runtime error on nunnumerics.</summary>
<System.Runtime.CompilerServices.Extension()> _
Public Function IsBetween(ByVal value As ValueType, ByVal Start As ValueType, ByVal [End] As ValueType) As Boolean
If Not CorrectOrder(value, Start, [End]) Then
Throw New ArgumentException("Start Value must be <= End Value")
End If
If ValidCompareType(value.GetType) AndAlso _
ValidCompareType(Start.GetType) AndAlso _
ValidCompareType([End].GetType) Then
If UseDecimal(value) Then
Return CDec(Start) < CDec(value) AndAlso CDec(value) < CDec([End])
Else
Return CDbl(Start) < CDbl(value) AndAlso CDbl(value) < CDbl([End])
End If
Else
Throw New ArgumentException("Invalid comparison type")
End If
End Function
''' <summary>Not Valid for nonnumeric value types including Boolean and user defined Structures. Will throw runtime error on nunnumerics.</summary>
<System.Runtime.CompilerServices.Extension()> _
Public Function IsBetweenInclusive(ByVal value As ValueType, ByVal Start As ValueType, ByVal [End] As ValueType) As Boolean
If Not CorrectOrder(value, Start, [End]) Then
Throw New ArgumentException("Start Value must be <= End Value")
End If
If ValidCompareType(value.GetType) AndAlso _
ValidCompareType(Start.GetType) AndAlso _
ValidCompareType([End].GetType) Then
If UseDecimal(value) Then
Return CDec(Start) <= CDec(value) AndAlso CDec(value) <= CDec([End])
Else
Return CDbl(Start) <= CDbl(value) AndAlso CDbl(value) <= CDbl([End])
End If
Else
Throw New ArgumentException("Invalid comparison type")
End If
End Function
''' <summary>Not Valid for nonnumeric value types including Boolean and user defined Structures. Will throw runtime error on nunnumerics.</summary>
<System.Runtime.CompilerServices.Extension()> _
Public Function IsBetweenIncludeStart(ByVal value As ValueType, ByVal Start As ValueType, ByVal [End] As ValueType) As Boolean
If Not CorrectOrder(value, Start, [End]) Then
Throw New ArgumentException("Start Value must be <= End Value")
End If
If ValidCompareType(value.GetType) AndAlso _
ValidCompareType(Start.GetType) AndAlso _
ValidCompareType([End].GetType) Then
If UseDecimal(value) Then
Return CDec(Start) <= CDec(value) AndAlso CDec(value) < CDec([End])
Else
Return CDbl(Start) <= CDbl(value) AndAlso CDbl(value) < CDbl([End])
End If
Else
Throw New ArgumentException("Invalid comparison type")
End If
End Function
''' <summary>Not Valid for nonnumeric value types including Boolean and user defined Structures. Will throw runtime error on nunnumerics.</summary>
<System.Runtime.CompilerServices.Extension()> _
Public Function IsBetweenIncludeEnd(ByVal value As ValueType, ByVal Start As ValueType, ByVal [End] As ValueType) As Boolean
If Not CorrectOrder(value, Start, [End]) Then
Throw New ArgumentException("Start Value must be <= End Value")
End If
If ValidCompareType(value.GetType) AndAlso _
ValidCompareType(Start.GetType) AndAlso _
ValidCompareType([End].GetType) Then
If UseDecimal(value) Then
Return CDec(Start) < CDec(value) AndAlso CDec(value) <= CDec([End])
Else
Return CDbl(Start) < CDbl(value) AndAlso CDbl(value) <= CDbl([End])
End If
Else
Throw New ArgumentException("Invalid comparison type")
End If
End Function
Private Function CorrectOrder(ByVal value As ValueType, ByVal Start As ValueType, ByVal [End] As ValueType) As Boolean
If UseDecimal(value) Then
Return CDec(Start) <= CDec([End])
Else
Return CDbl(Start) <= CDbl([End])
End If
End Function
Private Function ValidCompareType(ByVal t As Type) As Boolean
Select Case Type.GetTypeCode(t)
Case TypeCode.Int16, TypeCode.Int32, TypeCode.Int64, _
TypeCode.UInt16, TypeCode.UInt32, TypeCode.UInt64, _
TypeCode.Single, TypeCode.Double, _
TypeCode.Byte, TypeCode.SByte, _
TypeCode.Decimal
Return True
Case Else
Return False
End Select
End Function 'ValidCompareType
Private Function UseDecimal(ByVal value As ValueType) As Boolean
Select Case Type.GetTypeCode(value.GetType)
Case TypeCode.Int16, TypeCode.Int32, TypeCode.Int64, _
TypeCode.UInt16, TypeCode.UInt32, TypeCode.UInt64, _
TypeCode.Single, TypeCode.Double, _
TypeCode.Byte, TypeCode.SByte
Return False
Case TypeCode.Decimal
Return True
End Select
End Function
#End Region '"Numeric ValueType Between Methods"
End Module ' MyExtensions
Example usage:
Dim b As Int32 = 5
If b.IsBetween(0, 5) Then 'false
'0 < b < 5
Else
Stop
End If
If b.IsBetweenInclusive(0, 5) Then 'true
'0 <= b <= 5
Stop
End If
If b.IsBetweenIncludeStart(0, 5) Then 'false
'0 <= b < 5
Else
Stop
End If
If b.IsBetweenIncludeEnd(0, 5) Then 'true
'0 < b <= 5
Stop
End If
If 0 <= a <= 100 Then
This compares 0 and a, which gives a boolean result. This boolean is then compared to 100, which it will be less than. Therefore the condition is always true and will always be executed, no matter what the value of a is.
If 0 <= a AndAlso a <= 100 Then
This compares 0 with a. If it is false, stop right there; it does not matter what the second comparison is, this is false. Only if a is positive, then does it make the second comparison. This use of AndAlso is more prominant when the second comparison is a more complicated operation.
I stand corrected, although I did say Should Why? because if someone were to write 0<= a <= 100 in then that is a single condition i.e. a is a positive value that is less than or equal to 100.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.