Results 1 to 8 of 8

Thread: Get Data Type

  1. #1
    Join Date
    Oct 2004
    Location
    Oxfordshire, UK
    Posts
    89

    Question Unanswered: Get Data Type

    Hi All,

    I'm looking for a way to evaluate a string and return a possible data type in .NET, Classic VB, or VBScript (I'm not fussed which). For example:

    String Passed ADO DataTypeEnum DAO DataTypeEnum

    "12/12/2004" adDate dbDate
    "True" adBoolean dbBoolean
    "G12" adVarChar dbText

    Are there are existing functions/methods that handle this?

  2. #2
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    Quote Originally Posted by MyNewFlavour
    Hi All,

    I'm looking for a way to evaluate a string and return a possible data type in .NET, Classic VB, or VBScript (I'm not fussed which). For example:

    String Passed ADO DataTypeEnum DAO DataTypeEnum

    "12/12/2004" adDate dbDate
    "True" adBoolean dbBoolean
    "G12" adVarChar dbText

    Are there are existing functions/methods that handle this?
    I haven't seen anything like that. It would be difficult, if not impossible, to make it 100 percent error proof.

    In VB, the VAL function will evaluate a numeric argument, but it has quirks. ('17a' would return the number 17.)

    The real difficulty would be in interpreting numbers, though - yuou may have a whole number - how do you know what integer type it SHOULD be, of even if it's a floating point number that happened to be an integer?
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  3. #3
    Join Date
    Aug 2005
    Location
    Sharjah, UAE
    Posts
    25
    Quote Originally Posted by MyNewFlavour
    Hi All,

    I'm looking for a way to evaluate a string and return a possible data type in .NET, Classic VB, or VBScript (I'm not fussed which). For example:

    String Passed ADO DataTypeEnum DAO DataTypeEnum

    "12/12/2004" adDate dbDate
    "True" adBoolean dbBoolean
    "G12" adVarChar dbText

    Are there are existing functions/methods that handle this?
    I'm not sure what you intend to do. But as for the date, there is a function in VB6 that can evaluate whether a string is a date. The IsDate function might be what you need. For example...
    Code:
    If IsDate("12/12/2004") = True Then your result
    ...As for the numbers, there is also a VB6 function called IsNumeric. It also returns a boolean value indicating whether an expression can be evaluated as a number. It returns true if the entire expression is recognized as a number, otherwise it returns false. It also returns false if an expression is a date expression. It also follows the same syntax as IsDate.

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    trojanz is on the money, there are a whole heap of is<datatype> fucntions that will do what you want
    you could then coerce values using the c<datatype> in vb6/vba
    you have to go through the system object to do equivlanets in .NET
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Mar 2006
    Posts
    163
    In VB you could use TypeName, but it really depends on what you are trying to do.

  6. #6
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    The two VB "is" functions that may be of some use are IsNumeric and IsDate. They both return a boolean value.

    Now, if you build an If/Then/ElseIf structure in your sub, you could determine if a value is numeric (but not its type - single, double, integer, long, etc.) or if it's a date.

    Then, with further evaluation, (a Check for "True", "False", "Yes", "No", "On", "Off", etc,) you could get a decent idea as to whether a variable is boolean.

    If all else fail, you've probably got a string type.

    The real issue is that you'll NEVER know, with certainy, that the type chosen by your function is actually correct, except in certain instances.
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  7. #7
    Join Date
    Mar 2006
    Posts
    163
    Like I said you could try TypeName.
    Code:
        x = False
        MsgBox TypeName(x)
        x = 122
        MsgBox TypeName(x)
        x = 122.1
        MsgBox TypeName(x)
        x = "122.1"
        MsgBox TypeName(x)
        x = Date
        MsgBox TypeName(x)

  8. #8
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    The original poster says he's trying to evaluate a string and return the type of data contained within it.

    Unfortunately, TypeName (StringVariableType) will allways return "String". No matter what type of data is contained within it. It is a "string" ...

    FYI: TypeName is typically used inside functions/subs in VB6, as you cannot explicitly overload a procedure. Instead, you CAN declare the argument type as Variant, and then you can then pass almost anything to the sub. However, in order to properly handle the argument, you often use the TypeName function in a CASE or IF construct.
    Code:
    Select Case TypeName (VariantArgument)
      Case Single, Double
        ' Do One Thing
      Case Integer, Long
        ' Do Another
      Case String
        ' Do Something Else
      Case Else
        ' Raise Error
    End Select
    Last edited by loquin; 03-28-06 at 13:51.
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •