tags:

views:

179

answers:

3

I am still developing this function, but here is what I am intending it to do. This function will accept an Object, then try to determine its type. There are a specific set of types I am looking for: Integer, Boolean, Date, String. What I have so far is pretty privative, but it seems to be working so far:

Private Function DataType(ByVal entry As Object) As ValueType
    Try
        If IsNumeric(entry) Then
            If Integer.Parse(entry) Then
                Return ValueType.Number
            End If
        End If
    Catch
    End Try

    Try
        If Boolean.Parse(entry) Then
            Return ValueType.Boolean
        End If
    Catch
    End Try

    Try
        If Not Date.Parse(entry) = Nothing Then
            Return ValueType.Date
        End If
    Catch
    End Try

    Return ValueType.Text
End Function
+4  A: 

I would suggest using TypeOf instead of Parse or TryParse

  If  TypeOf entry Is Integer Then ...
Jeremy
This has a very different effect!
Dario
Different than what? The method Anders is using returns a ValueType not the actual value. So Parse and TryParse are useless.
Jeremy
Try it out - TypeOf CObj("42") Is Integer yields false!
Dario
He doesn't check what type it is but to which type it can be converted.
Dario
+2  A: 

You'll have to decide whether you will accept "42" to be an integer. The object type is still String!

The try-blocks and additional checking with IsNumeric can be removed anyway - Just use the TryParse-Functions

    Dim IntResult As Integer

    If Integer.TryParse("42", IntResult) Then
        ' Parsing succeeded - Result is stored in IntResult '
    Else
        ' Failed! '
    End If

When the types can be checked at compile-time, you could use parameter overloading.

Dario
Excellent! Like you said in Jeremy's post, TypeOf <obj> Is <type> does not work how I want. Please correct me if I am wrong, but does the TryParse have an internal Try..Catch method? If that is the case, is it redundant to wrap the test in my own Try...Catch?
Anders
There is no Try..Catch at all ;-) The way of telling you that the cast failed is not throwing an exception but returning `False` - This brings also some speed improvement (Exceptions are slow).
Dario
I was on the right track then with my thoughts :D thanks again!
Anders
A: 

I am a C# developer and I don't fully unterstand your code, but I would use a dictionary to map between the type of an object and something else - I assume you return a enum value. Here is a C# example. In real code the dictionary should probably not be build on every method call.

public enum ValueType
{
    Unknown, Number, Boolean, Date, String
}

public static ValueType DataType(Object o)
{
    Dictionary<Type, ValueType> map =
        new Dictionary<Type, ValueType>
            {
                {typeof (Int32), ValueType.Number},
                {typeof (Int64), ValueType.Number},
                {typeof (Decimal), ValueType.Number},
                {typeof (Single), ValueType.Number},
                {typeof (Double), ValueType.Number},
                {typeof (Boolean), ValueType.Boolean},
                {typeof (DateTime), ValueType.Date},
                {typeof (String), ValueType.String}
            };

    if ((o == null) || (!map.ContainsKey(o.GetType())))
    {
        return ValueType.Unknown;
    }
    else
    {
        return map[o.GetType()];
    }
}

I had a second look and it looks like you are trying to determine the type of information stored in a string by parsing it - in this case the above won't help.

Daniel Brückner