Check if an object is treatable as string/char

T

tommaso.gastaldi

Hi,

does anybody know a speedy analog of IsNumeric() to check for
strings/chars. I would like to check if an Object can be treated as a
string before using a Cstr(), clearly avoiding the time and resource
consuming Try... Catch, which in iterative processing is totally
unacceptable.

-tom
 
M

Mattias Sjögren

does anybody know a speedy analog of IsNumeric() to check for
strings/chars. I would like to check if an Object can be treated as a
string before using a Cstr(), clearly avoiding the time and resource
consuming Try... Catch, which in iterative processing is totally
unacceptable.

I didn't think CStr would throw any exception. It tries to convert
whatever you pass it to a string (by calling ToString if needed). But
if you want to check if something is a string you use

If TypeOf obj Is String Then ...

or possibly the TryCast operator.


Mattias
 
T

tommaso.gastaldi

Hi Mattias, thanks for the reply. Cstr() does raise exceptions.

I will explain again my question. Consider the following example:

Dim AnyObject As Object = New TimeSpan(23)
Dim AnyObject2 As Object = 34.5D

For i As Integer = 0 To 5

Try
Dim s As String = CStr(AnyObject)
MsgBox("Conversion1 ok")
Catch ex As Exception
MsgBox("Conversion1 failed")
End Try

Try
Dim s As String = CStr(AnyObject2)
MsgBox("Conversion2 ok")
Catch ex As Exception
MsgBox("Conversion2 failed")
End Try

Next


I would like to find something that let me recognize when a conversion
to string is possible (the object may be anything) without using:

Try
Catch ex As Exception
End Try

This is possible for instance when one want to recognize if something
is convertible to a number :

If IsNumeric(AnyObject) Then
Dim Number As Double = CDbl(AnyObject)
End If

Note that using IsNumeric instead of any Try Catch is highly advisable
and incomparably faster.

My question : what is the corresponing to check id an obj is
convertible to string:

If IsConvertibleoString(AnyObject) Then
Dim Text As String = CStr(AnyObject)
End If

that is : what could the function IsConvertibleoString(AnyObject as
Object) be (clearly, something fast, not using TryCatch) ??


-tom

Mattias Sjögren ha scritto:
 
J

Jay B. Harlow [MVP - Outlook]

Tom,
A conversion to string is *always* possible as you simply need to call
Object.ToString!

Instead of:
Dim s As String = CStr(AnyObject)
Use:
Dim s As String = AnyObject.ToString

However converting to a string is not neccessarily the same as getting the
string representation of an object...

For i As Integer = 0 To 5
What the fudge! Is a for loop really necessary to show the example??? IMHO I
really don't think so! :-|


Remember that CStr is short hand for CType(..., String). Remember that CType
is Convert type.

In VS2005 (.NET 2.0) you can overload CType on your own types. If a type has
CType overloaded for String, the CStr will use it.

Decimal is "special" in that VB offers a built-in conversion for it.
TimeSpan is not considered as special so the VB compiler looks for an
overloaded CType method to call.

The "problem" with writing a IsConvertibleToString function is knowing which
types have a built-in conversion & which use an overloaded CType operator...

Also CStr behaves differently when given an object parameter verses a
strongly typed parameter. (which IMHO feels like a bug).

For example:

Public Class Something

' VB 2005 syntax
Public Shared Narrowing Operator CType(ByVal aSomething As
Something) As String
Return aSomething.ToString()
End Operator

End Class

' this statement will fail:
Dim AnyObject As Object = New Something()

' this statement will succeed
Dim AnyObject As New Something()


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Hi Mattias, thanks for the reply. Cstr() does raise exceptions.

I will explain again my question. Consider the following example:

Dim AnyObject As Object = New TimeSpan(23)
Dim AnyObject2 As Object = 34.5D

For i As Integer = 0 To 5

Try
Dim s As String = CStr(AnyObject)
MsgBox("Conversion1 ok")
Catch ex As Exception
MsgBox("Conversion1 failed")
End Try

Try
Dim s As String = CStr(AnyObject2)
MsgBox("Conversion2 ok")
Catch ex As Exception
MsgBox("Conversion2 failed")
End Try

Next


I would like to find something that let me recognize when a conversion
to string is possible (the object may be anything) without using:

Try
Catch ex As Exception
End Try

This is possible for instance when one want to recognize if something
is convertible to a number :

If IsNumeric(AnyObject) Then
Dim Number As Double = CDbl(AnyObject)
End If

Note that using IsNumeric instead of any Try Catch is highly advisable
and incomparably faster.

My question : what is the corresponing to check id an obj is
convertible to string:

If IsConvertibleoString(AnyObject) Then
Dim Text As String = CStr(AnyObject)
End If

that is : what could the function IsConvertibleoString(AnyObject as
Object) be (clearly, something fast, not using TryCatch) ??


-tom

Mattias Sjögren ha scritto:
 
T

tommaso.gastaldi

Jay B. Harlow [MVP - Outlook] ha scritto:
Tom,
A conversion to string is *always* possible as you simply need to call
Object.ToString!

Instead of:
Dim s As String = CStr(AnyObject)
Use:
Dim s As String = AnyObject.ToString


Hi Jay, thanks for the contribution. I may be missing something, but I
still feel I did not solve my original problem.

I want something which works for *any* object.

For instance:

Dim a As Object = New Byte() {}
Dim s1 As String = CStr(a)
'a does not have ToString methods

Dim MyObj As New MyObj
Dim s2 As String = CStr(MyObj.anything)
'MyObj.anything does not have ToString methods

and not all have ToString.

However converting to a string is not neccessarily the same as getting the
string representation of an object...

that's not important for my problem. I am only concerned about the fact
that Cstr() would work without exception. I dont care about the string
which comes out.
What the fudge! Is a for loop really necessary to show the example??? IMHO I
really don't think so! :-|

Yes. The idea was to give an idea of the intended framework. If there
wasn't some big iteration involved I would just use Try...Catch, as the
fact that it is slow would not be important.
....
The "problem" with writing a IsConvertibleToString function is knowing which
types have a built-in conversion & which use an overloaded CType operator....

Function IsConvertibleToString() as Boolean

I would like something which work with any Object and, at the same
time, is not so devastating like Try ... Catch. Just the analog of
IsNumeric().

It is not very clear to me why the language does not readily provide it
(I may just be missing it ! ). This seems a basic need.

I have seen also there is a Converter class. But could not figure out a
way to use it for this problem.
Also CStr behaves differently when given an object parameter verses a
strongly typed parameter. (which IMHO feels like a bug).

For example:

Public Class Something

' VB 2005 syntax
Public Shared Narrowing Operator CType(ByVal aSomething As
Something) As String
Return aSomething.ToString()
End Operator

End Class

' this statement will fail:
Dim AnyObject As Object = New Something()

' this statement will succeed
Dim AnyObject As New Something()

mmm ...I am missing your point here (?). (First statement seems
legal).


-tom
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Hi Mattias, thanks for the reply. Cstr() does raise exceptions.

I will explain again my question. Consider the following example:

Dim AnyObject As Object = New TimeSpan(23)
Dim AnyObject2 As Object = 34.5D

For i As Integer = 0 To 5

Try
Dim s As String = CStr(AnyObject)
MsgBox("Conversion1 ok")
Catch ex As Exception
MsgBox("Conversion1 failed")
End Try

Try
Dim s As String = CStr(AnyObject2)
MsgBox("Conversion2 ok")
Catch ex As Exception
MsgBox("Conversion2 failed")
End Try

Next


I would like to find something that let me recognize when a conversion
to string is possible (the object may be anything) without using:

Try
Catch ex As Exception
End Try

This is possible for instance when one want to recognize if something
is convertible to a number :

If IsNumeric(AnyObject) Then
Dim Number As Double = CDbl(AnyObject)
End If

Note that using IsNumeric instead of any Try Catch is highly advisable
and incomparably faster.

My question : what is the corresponing to check id an obj is
convertible to string:

If IsConvertibleToString(AnyObject) Then
Dim Text As String = CStr(AnyObject)
End If

that is : what could the function IsConvertibleToString(AnyObject as
Object) be (clearly, something fast, not using TryCatch) ??


-tom

Mattias Sjögren ha scritto:
I didn't think CStr would throw any exception. It tries to convert
whatever you pass it to a string (by calling ToString if needed). But
if you want to check if something is a string you use

If TypeOf obj Is String Then ...

or possibly the TryCast operator.


Mattias
 
G

Guest

Tom,
I am going to contribute my 2 cents worth here. You say that "and not
all have ToString", which I believe is incorrect. All objects inherit from
System.obect and therefore have a "ToString" method. Now the real question
is what do you mean by "convertable to string"? The developer of an Employee
class may decide to override the ToString method and return the employees
name. But I do not think that this is the same thing as 'convertable to a
string'. I beleive that what you really want to know is whether the object
is in fact a 'value type', but that is just a guess on my part. In addition
to the value types, certainly the string type is convertable. I do not know
if there is an easy way to determine if the object is in fact a value type or
not. Guess the next thing is to find out if my guess is correct or not.
--
Terry


Jay B. Harlow [MVP - Outlook] ha scritto:
Tom,
A conversion to string is *always* possible as you simply need to call
Object.ToString!

Instead of:
Dim s As String = CStr(AnyObject)
Use:
Dim s As String = AnyObject.ToString


Hi Jay, thanks for the contribution. I may be missing something, but I
still feel I did not solve my original problem.

I want something which works for *any* object.

For instance:

Dim a As Object = New Byte() {}
Dim s1 As String = CStr(a)
'a does not have ToString methods

Dim MyObj As New MyObj
Dim s2 As String = CStr(MyObj.anything)
'MyObj.anything does not have ToString methods

and not all have ToString.

However converting to a string is not neccessarily the same as getting the
string representation of an object...

that's not important for my problem. I am only concerned about the fact
that Cstr() would work without exception. I dont care about the string
which comes out.
What the fudge! Is a for loop really necessary to show the example??? IMHO I
really don't think so! :-|

Yes. The idea was to give an idea of the intended framework. If there
wasn't some big iteration involved I would just use Try...Catch, as the
fact that it is slow would not be important.
....
The "problem" with writing a IsConvertibleToString function is knowing which
types have a built-in conversion & which use an overloaded CType operator....

Function IsConvertibleToString() as Boolean

I would like something which work with any Object and, at the same
time, is not so devastating like Try ... Catch. Just the analog of
IsNumeric().

It is not very clear to me why the language does not readily provide it
(I may just be missing it ! ). This seems a basic need.

I have seen also there is a Converter class. But could not figure out a
way to use it for this problem.
Also CStr behaves differently when given an object parameter verses a
strongly typed parameter. (which IMHO feels like a bug).

For example:

Public Class Something

' VB 2005 syntax
Public Shared Narrowing Operator CType(ByVal aSomething As
Something) As String
Return aSomething.ToString()
End Operator

End Class

' this statement will fail:
Dim AnyObject As Object = New Something()

' this statement will succeed
Dim AnyObject As New Something()

mmm ...I am missing your point here (?). (First statement seems
legal).


-tom
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Hi Mattias, thanks for the reply. Cstr() does raise exceptions.

I will explain again my question. Consider the following example:

Dim AnyObject As Object = New TimeSpan(23)
Dim AnyObject2 As Object = 34.5D

For i As Integer = 0 To 5

Try
Dim s As String = CStr(AnyObject)
MsgBox("Conversion1 ok")
Catch ex As Exception
MsgBox("Conversion1 failed")
End Try

Try
Dim s As String = CStr(AnyObject2)
MsgBox("Conversion2 ok")
Catch ex As Exception
MsgBox("Conversion2 failed")
End Try

Next


I would like to find something that let me recognize when a conversion
to string is possible (the object may be anything) without using:

Try
Catch ex As Exception
End Try

This is possible for instance when one want to recognize if something
is convertible to a number :

If IsNumeric(AnyObject) Then
Dim Number As Double = CDbl(AnyObject)
End If

Note that using IsNumeric instead of any Try Catch is highly advisable
and incomparably faster.

My question : what is the corresponing to check id an obj is
convertible to string:

If IsConvertibleToString(AnyObject) Then
Dim Text As String = CStr(AnyObject)
End If

that is : what could the function IsConvertibleToString(AnyObject as
Object) be (clearly, something fast, not using TryCatch) ??


-tom

Mattias Sjögren ha scritto:
does anybody know a speedy analog of IsNumeric() to check for
strings/chars. I would like to check if an Object can be treated as a
string before using a Cstr(), clearly avoiding the time and resource
consuming Try... Catch, which in iterative processing is totally
unacceptable.

I didn't think CStr would throw any exception. It tries to convert
whatever you pass it to a string (by calling ToString if needed). But
if you want to check if something is a string you use

If TypeOf obj Is String Then ...

or possibly the TryCast operator.


Mattias
 
T

tommaso.gastaldi

Hi Terry,

OK I got the substance of what You, Jay and Mattias are actually
suggesting, and I think that it solves completely my problem.

You are actually saying that using *always* ToString(), instead of
Cstr(), there is *never* the problem that an exception can be thrown
(unless the Obj has not been instantiated, which can anyway be checked
early by "Is Nothing")

Yes, "if the object is in fact a value type" can be checked by
IsValueType.

Class MyObj
Public anything As New Object
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim MyObj As New MyObj
MsgBox(MyObj.anything.ToString())
MsgBox(MyObj.anything.GetType.IsValueType)
End Sub

Thanky you *VERY* much all. Now I am much more clear on the matter....
you have been very helpful.

-tom

Terry ha scritto:
Tom,
I am going to contribute my 2 cents worth here. You say that "and not
all have ToString", which I believe is incorrect. All objects inherit from
System.obect and therefore have a "ToString" method. Now the real question
is what do you mean by "convertable to string"? The developer of an Employee
class may decide to override the ToString method and return the employees
name. But I do not think that this is the same thing as 'convertable to a
string'. I beleive that what you really want to know is whether the object
is in fact a 'value type', but that is just a guess on my part. In addition
to the value types, certainly the string type is convertable. I do not know
if there is an easy way to determine if the object is in fact a value type or
not. Guess the next thing is to find out if my guess is correct or not.
--
Terry


Jay B. Harlow [MVP - Outlook] ha scritto:
Tom,
A conversion to string is *always* possible as you simply need to call
Object.ToString!

Instead of:
Dim s As String = CStr(AnyObject)
Use:
Dim s As String = AnyObject.ToString


Hi Jay, thanks for the contribution. I may be missing something, but I
still feel I did not solve my original problem.

I want something which works for *any* object.

For instance:

Dim a As Object = New Byte() {}
Dim s1 As String = CStr(a)
'a does not have ToString methods

Dim MyObj As New MyObj
Dim s2 As String = CStr(MyObj.anything)
'MyObj.anything does not have ToString methods

and not all have ToString.

However converting to a string is not neccessarily the same as getting the
string representation of an object...

that's not important for my problem. I am only concerned about the fact
that Cstr() would work without exception. I dont care about the string
which comes out.
For i As Integer = 0 To 5
What the fudge! Is a for loop really necessary to show the example???IMHO I
really don't think so! :-|

Yes. The idea was to give an idea of the intended framework. If there
wasn't some big iteration involved I would just use Try...Catch, as the
fact that it is slow would not be important.
....
The "problem" with writing a IsConvertibleToString function is knowing which
types have a built-in conversion & which use an overloaded CType operator....

Function IsConvertibleToString() as Boolean

I would like something which work with any Object and, at the same
time, is not so devastating like Try ... Catch. Just the analog of
IsNumeric().

It is not very clear to me why the language does not readily provide it
(I may just be missing it ! ). This seems a basic need.

I have seen also there is a Converter class. But could not figure out a
way to use it for this problem.
Also CStr behaves differently when given an object parameter verses a
strongly typed parameter. (which IMHO feels like a bug).

For example:

Public Class Something

' VB 2005 syntax
Public Shared Narrowing Operator CType(ByVal aSomething As
Something) As String
Return aSomething.ToString()
End Operator

End Class

' this statement will fail:
Dim AnyObject As Object = New Something()

' this statement will succeed
Dim AnyObject As New Something()

mmm ...I am missing your point here (?). (First statement seems
legal).


-tom
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Hi Mattias, thanks for the reply. Cstr() does raise exceptions.

I will explain again my question. Consider the following example:

Dim AnyObject As Object = New TimeSpan(23)
Dim AnyObject2 As Object = 34.5D

For i As Integer = 0 To 5

Try
Dim s As String = CStr(AnyObject)
MsgBox("Conversion1 ok")
Catch ex As Exception
MsgBox("Conversion1 failed")
End Try

Try
Dim s As String = CStr(AnyObject2)
MsgBox("Conversion2 ok")
Catch ex As Exception
MsgBox("Conversion2 failed")
End Try

Next


I would like to find something that let me recognize when a conversion
to string is possible (the object may be anything) without using:

Try
Catch ex As Exception
End Try

This is possible for instance when one want to recognize if something
is convertible to a number :

If IsNumeric(AnyObject) Then
Dim Number As Double = CDbl(AnyObject)
End If

Note that using IsNumeric instead of any Try Catch is highly advisable
and incomparably faster.

My question : what is the corresponing to check id an obj is
convertible to string:

If IsConvertibleToString(AnyObject) Then
Dim Text As String = CStr(AnyObject)
End If

that is : what could the function IsConvertibleToString(AnyObject as
Object) be (clearly, something fast, not using TryCatch) ??


-tom

Mattias Sjögren ha scritto:

does anybody know a speedy analog of IsNumeric() to check for
strings/chars. I would like to check if an Object can be treated as a
string before using a Cstr(), clearly avoiding the time and resource
consuming Try... Catch, which in iterative processing is totally
unacceptable.

I didn't think CStr would throw any exception. It tries to convert
whatever you pass it to a string (by calling ToString if needed). But
if you want to check if something is a string you use

If TypeOf obj Is String Then ...

or possibly the TryCast operator.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top