Announcement

Collapse
No announcement yet.

Looking for the 'VarType' function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Looking for the 'VarType' function

    It does not seems to exist, this may be handy.

    Today i wanted tot test the parameter passed to a macro.
    The vartype determines the action.

    I only came up using a variant and determine it using the variantvt() call.
    Not so bad but a more native function would be nice.
    hellobasic

    #2
    Not possible. Data cannot be identified as a "type" given only its contents.

    That's why the compiler checks types in procedure header versus call line when compiling.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


      #3
      Not possible. Data cannot be identified as a "type" given only its contents.
      It most certainly is possible - at compile time the compiler will know the variable type of all standard variables (STRING, LONG, SINGLE, etc). So a simple MACRO like this could work:

      Code:
      MACRO SetInt(vVar,nVal)
        Select Case VarType(vVar)
               Case %VT_STRING ' Or whatever the compiler returns for STRING
                    vVar = LTrim$(Str$(nVal))
               Case %VT_L4 ' Or whatever the compiler returns for LONG
                    vVar = nVal
        End Select
      END MACRO
      Where "VarType(vVar)" is replaced by a numeric constant by the compiler upon compilation.

      I can see this being useful in many "in code" situations where the type is ambiguous to the code being called. Of course, it will be no good for external or unknown variable types, external functions, and the like.
      Last edited by Kev Peel; 24 Jun 2008, 08:20 PM.
      kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

      Comment


        #4
        >Not possible. Data cannot be identified as a "type" given only its contents.
        That's not the idea, mentioned a macro, the macro parameter is a known variable for the compiler.
        hellobasic

        Comment


          #5
          >> Not possible. Data cannot be identified as a "type" given only its contents
          >It most certainly is possible - at compile time the compiler will know ....

          At compile time the compiler knows the type.

          Even in your example, the type is known: it's a VARIANT!
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


            #6
            You don't have to use a VARIANT to have a function which processes multiple data types, not knowing until execution time exactly what it is looking at...

            Single Function to process any array type (CC3/Win7+) 12-17-03

            .. instead of using a scalar variable, create a one-element array and call this function.
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


              #7
              So?

              That's for arrays.
              hellobasic

              Comment


                #8
                This shows that any fixed type will become a double:
                Code:
                    Local i As Long
                    Local w As Word    
                    Local v As Variant    
                    i = 1
                    w = 2
                    v = w
                    MsgBox Str$( VariantVT( v ) )
                At this time i can distinguish between variants and their types and the rest is a numeric (assuming long integer up to double).
                It's good enough for me at this time but could be better.
                hellobasic

                Comment


                  #9
                  So?
                  That's for arrays.
                  As I said, create a one element array before you make the call. If you want to pass "X" where X is "anything", you can instead ...
                  Code:
                     X = some complex and no doubt brilliant piece of code. 
                    REDIM Z(0) AS  (type of X) 
                     Z(0) = X 
                    CALL FunctionWhichDoesNotCareWhatTypeOfArray (Z())
                  Your 'FunctionWhichDoesNotCareWhatTypeOfArray' gets the data type and processes element zero.

                  No muss, no fuss, no bother. Piece of cake. Can of corn.

                  MCM
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                    #10
                    You might also consider adding another parameter to the macro, eg. an integer say 1 to 9, specifying all possible data types it might get. That would almost for sure be more efficient and faster because the calling code will know the type already (I think), so no calculation to determine type will have to be made by the macro (beyond eg. SELECT CASE).

                    Comment


                      #11
                      >>Can of corn never heard that one before.

                      Comment


                        #12
                        Originally posted by John Gleason View Post
                        >>Can of corn never heard that one before.
                        Not a baseball fan?

                        A couple of possible sources of the phrase are cited in the definitive "New Dickson Baseball Dictionary." The most accepted: The phrase, first used in 1896, makes reference to a long-ago practice where a grocer would use a stick to tip a can of vegetables off a high shelf, then catch it in his hands or outstretched apron. Another possible source: Such a pop fly is as easy to capture as "corn from a can."
                        James

                        Comment


                          #13
                          Obviously this is staying with arrays...

                          but there is the ARRAYATTR function

                          John
                          John,
                          --------------------------------
                          John Strasser
                          Phone: 480 - 273 - 8798

                          Comment


                            #14
                            Vartype....

                            If VARPTR() and STRPTR() could be changed to return 0
                            when variables AREN'T IN SCOPE....

                            Something This would be possible...

                            Code
                            #COMPILE EXE
                            #DIM ALL

                            FUNCTION VarType(OPTIONAL BYVAL vbyte AS BYTE, _
                            OPTIONAL BYVAL vINTEGER AS INTEGER, _
                            OPTIONAL BYVAL vLONG AS LONG, _
                            OPTIONAL BYVAL vDWORD AS DWORD, _
                            OPTIONAL BYVAL vCURRENCY AS CURRENCY, _
                            OPTIONAL BYVAL vString AS STRING) AS STRING


                            IF VARPTR(vbyte)<>0 THEN FUNCTION = "BYTE" : EXIT FUNCTION
                            IF VARPTR(vINTEGER)<>0 THEN FUNCTION = "INTEGER" : EXIT FUNCTION
                            IF VARPTR(vLONG)<>0 THEN FUNCTION = "LONG" : EXIT FUNCTION
                            IF VARPTR(vDWORD)<>0 THEN FUNCTION = "DWORD" : EXIT FUNCTION
                            IF VARPTR(vCurrency)<>0 THEN FUNCTION = "CURRENCY" : EXIT FUNCTION
                            IF STRPTR(vString)<>0 THEN FUNCTION = "STRING" : EXIT FUNCTION

                            END FUNCTION

                            FUNCTION PBMAIN () AS LONG
                            LOCAL A AS BYTE
                            LOCAL b AS INTEGER
                            LOCAL c AS LONG
                            LOCAL D AS DWORD
                            LOCAL E AS CURRENCY
                            A=1?
                            B=2%
                            C=3&
                            D=4
                            E=5
                            ? VarType(a)
                            ? Vartype(b)
                            ? Vartype(c)
                            ? Vartype(d)
                            ? Vartype(e)
                            END FUNCTION

                            \code
                            Last edited by Nathan Maddox; 27 Jun 2008, 03:00 PM.
                            Nathan Maddox

                            Comment


                              #15
                              That is easily done by using BYREF for the parameters.

                              When new features are suggested like this, the goal is not for 'workarounds' but to determine if other users have a need for the feature as well. Judging by this thread, I'd say not many are interested or have a need
                              kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

                              Comment


                                #16
                                I tried the BYREF before I posted and it would not compile...

                                It hung on ? VarType(b) with a parameter mismatch..
                                Nathan Maddox

                                Comment


                                  #17
                                  The problem is that the OPTIONAL keyword does not mean the parameter must be completely skipped -- all parameters must be kept in order. ie.

                                  Code:
                                  ? Vartype(a)
                                  ? Vartype(,b)
                                  ? Vartype(,,c)
                                  ..etc.
                                  kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

                                  Comment


                                    #18
                                    Looks to me as..."Invalid Parameter" issue

                                    sometimes the obvious is often overlooked

                                    I found something similar (but no crash) between STRINGS vs ASCIIZ and passing a null (although still not sure if a CHR$(0), or $NUL or any other value that is unprintable.

                                    I think in this case...are you expecting a pointer and passing a value? or are you expecting a value, and being passed a pointer???

                                    Its a wicked picket when you inadvertently break the rules
                                    Engineer's Motto: If it aint broke take it apart and fix it

                                    "If at 1st you don't succeed... call it version 1.0"

                                    "Half of Programming is coding"....."The other 90% is DEBUGGING"

                                    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                                    Comment


                                      #19
                                      It's very clear as mentioned in my above post -- the compiler is expecting the BYREF variables as they are declared:

                                      Code:
                                      (BYTE,INTEGER,LONG,DWORD,CURRENCY,STRING)
                                      Now, you can "skip" the parameters marked as OPTIONAL by leaving them blank, and the compiler will pass a NULL pointer (the function will see these params with VARPTR return code zero).

                                      Valid (I'm not using var names to shorten the example):

                                      Code:
                                      FuncName(BYTE)
                                      FuncName(,INTEGER)
                                      FuncName(,,,,,STRING)
                                      Invalid:

                                      Code:
                                      FuncName(,BYTE)
                                      FuncName(INTEGER)
                                      FuncName(STRING)
                                      In brief: as long as the BYREF variables are explicitly declared, nothing short of a parameter override (BYVAL x) will satisfy the compiler's requirement of that parameter. A BYVAL parameter override when calling a function will let you pass whatever value you wish.
                                      Last edited by Kev Peel; 27 Jun 2008, 07:56 PM.
                                      kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

                                      Comment


                                        #20
                                        If VARPTR() and STRPTR() could be changed to return 0
                                        when variables AREN'T IN SCOPE....
                                        ???

                                        Your program will not compile if the object of VARTPR() or STRPTR() is not in scope.

                                        (You might need to be using #DIM ALL to ensure that. But everyone uses #DIM ALL, don't they?)
                                        Michael Mattias
                                        Tal Systems (retired)
                                        Port Washington WI USA
                                        [email protected]
                                        http://www.talsystems.com

                                        Comment

                                        Working...
                                        X
                                        😀
                                        🥰
                                        🤢
                                        😎
                                        😡
                                        👍
                                        👎