What is syntax to test if Object is a String?

J

Joe Fallon

I would like to know the syntax to check that an Object is a String.

If it was a number test I might use IsNumeric.
But there is no function: IsString (is there?)
 
H

Herfried K. Wagner [MVP]

* "Joe Fallon said:
I would like to know the syntax to check that an Object is a String.

If it was a number test I might use IsNumeric.
But there is no function: IsString (is there?)

\\\
Dim o As Object = 2
Dim p As Object = "Hello"
Dim q As Object = Me
MsgBox(TypeOf o Is String)
MsgBox(TypeOf p Is String)
MsgBox(TypeOf q Is String)
///
 
A

Armin Zingler

Joe Fallon said:
I would like to know the syntax to check that an Object is a
String.

If it was a number test I might use IsNumeric.
But there is no function: IsString (is there?)

You didn't supply details, so I guess the answer is

If typeof o is string then

end if
 
A

_Andy_

I would like to know the syntax to check that an Object is a String.

If it was a number test I might use IsNumeric.
But there is no function: IsString (is there?)

If TypeOf something Is String Then
....
 
J

Joe Fallon

Always figure it out 5 seconds after posting!! <g>

If Object.GetType Is GetType(String) Then
 
J

Jay B. Harlow [MVP - Outlook]

Joe,
I would recommend what Herfried showed:

If Typeof obj Is String Then

The difference being is that TypeOf will succeed for derived types, where as
GetType matches the type exactly. For String not much of a difference, for
other types that you derive from, such as Control it can be a world of
difference.

Dim c As Control
c = New TextBox

If TypeOf c Is Control Then
' True : TextBox inherits from Control
End If

If c.GetType() Is GetType(Control) Then
Else
' False : c is actually a TextBox
End If

Hope this helps
Jay
 
J

Joe Fallon

Jay,
Thanks.
I did switch my code to:
If Typeof obj Is String Then

because it seemed "more elegant".

But now I know it is also "safer".

Good advice. Appreciate it!
--
Joe Fallon


Jay B. Harlow said:
Joe,
I would recommend what Herfried showed:

If Typeof obj Is String Then

The difference being is that TypeOf will succeed for derived types, where as
GetType matches the type exactly. For String not much of a difference, for
other types that you derive from, such as Control it can be a world of
difference.

Dim c As Control
c = New TextBox

If TypeOf c Is Control Then
' True : TextBox inherits from Control
End If

If c.GetType() Is GetType(Control) Then
Else
' False : c is actually a TextBox
End If

Hope this helps
Jay
 
G

Gary Owsiany

Joe Fallon said:
I would like to know the syntax to check that an Object is a String.

If it was a number test I might use IsNumeric.
But there is no function: IsString (is there?)
In Object-oriented programming, type-checking is considered a "poor
practice". Depending upon what you are trying to accomplish, consider using
polymorphism to accomplish your task. Then, instead of "testing" to see
what object you are using, the object itself will know the correct response
for the message you are sending to it.

Gary O.
 
J

Joe Fallon

I have an argument that takes an Object.
But in the special case when the Object is a String I need to do something
extra.

So this is perfect for my needs:
If TypeOf something Is String Then
 
A

Armin Zingler

Joe Fallon said:
I have an argument that takes an Object.
But in the special case when the Object is a String I need to do
something extra.

So this is perfect for my needs:
If TypeOf something Is String Then

How is something declared?
 
J

Jay B. Harlow [MVP - Outlook]

Joe,
But in the special case when the Object is a String I need to do something
extra.
Depending on how your function is structured you can use overloading in this
case also.

Public Sub DoSomething(ByVal x As String)
' special processing for a string
DoSomething(DirectCast(x, Object))
' more special processing for a string
End Sub

Public Sub DoSomething(ByVal x As Object)
' processing for a generic object
End Sub

The "DoSomething(DirectCast(x, Object))" forces the object version of the
function to be invoked.

Else where in your code when you:

Dim s as String
DoSomething(s)

The String version will be called. Where as when you:

Dim s As Object
DoSomething(s)

The Object version will be called.

Overloading in just another version of polymorphism that is useful.

However there are times when you do need to use If Typeof, which is why its
available also. As Gary stated, its good to limit its use...

Hope this helps
Jay
 
J

Joe Fallon

Everyone,

I changed it to use the overloaded methods.
It is much cleaner that way.

Appreciate the advice!
--
Joe Fallon



Jay B. Harlow said:
Joe,
But in the special case when the Object is a String I need to do something
extra.
Depending on how your function is structured you can use overloading in this
case also.

Public Sub DoSomething(ByVal x As String)
' special processing for a string
DoSomething(DirectCast(x, Object))
' more special processing for a string
End Sub

Public Sub DoSomething(ByVal x As Object)
' processing for a generic object
End Sub

The "DoSomething(DirectCast(x, Object))" forces the object version of the
function to be invoked.

Else where in your code when you:

Dim s as String
DoSomething(s)

The String version will be called. Where as when you:

Dim s As Object
DoSomething(s)

The Object version will be called.

Overloading in just another version of polymorphism that is useful.

However there are times when you do need to use If Typeof, which is why its
available also. As Gary stated, its good to limit its use...

Hope this helps
Jay
 

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

Similar Threads


Top