Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How exactly TypeOf operator works? 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
568
US
Colleagues,
There's a very helpful built-in function in the VFP: BETWEEN(tArg, tMin, tMax), that evaluates whether the Arg is within the Max and Min limits, and returns Boolean flag.
What I liked the most about this function was that it took parameters of different data types (as long as all three were of the same one).
Now I need to mimic this function in VB .NET (the reasons are multiple, long story). Naturlich, I intend to overload it for Integer, Float, Char and String types (yes, VFP's BETWEEN() can work with that latter type as well!)
I tried to use TypeOf operator, and... see for yourself:

Code:
'====================================================================================================================================
Public Function Between(ByVal tiArg As Integer, ByVal tiMin As Integer, ByVal tiMax As Integer) As Boolean
'====================================================================================================================================
' Purpose       : Checks if given Argument's value is between Min and Max values.
' Description   : 
' Parameters	: 
' Returns	: Yes/No flag as Boolean.
' Side effects  : None.
' Notes         : 1. Generic.
'                 2. Complies with .NET Framework ver. X.X and higher.
'		  3. If any of the arguments is not of type Int - notifies the User and returns False
' Author        : Ilya I. Rabyy
' Revisions     : by Ilya on 2022-08-26 - started 1st draft.
'====================================================================================================================================
Dim llRet As Boolean = True

llRet = TypeOf tiArg Is Integer

If Not llRet Then Return llRet

Return llRet
End Function
'====================================================================================================================================

And the VS marks this tiArg as erring:

2022_08_31_TypeOf_Behavior_1_f9du3k.jpg


Article in ain't no help for yours truly: I can't see how sample there differs from my code syntax-wise...

Could you, please, explain to me what am I doing wrong?

TIA!

Regards,

Ilya
 


TypeOf really checks that an object is compatible with a certain data type, not that a parameter (variable) is a particular type. At the Project level, if you turn Option Strict on, the variable declaration determines the type (i.e., tiArg will always be an Integer in your example). If Option Strict is off, you'll need to check the variable type with TypeName or VarType.

Code:
Dim MyType as string
MyType = TypeName(IntVar)    ' Returns "Integer".

Dim type As VariantType = VarType(value)
If type = VariantType.Integer Then
    'It's an integer
End If


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Thank you, Mark!
I actually discovered this VarType() function after all (as well as VariantType enum thingy) - already used in my "older" program. (Early Alzheimer? Dunno...)
Well, as saying goes, "New is just well forgotten Old". :)
But thank you nonetheless for coming to my "rescue", colleague!

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top