473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

What is syntax to test if Object is a String?

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?)
--
Joe Fallon


Nov 20 '05 #1
12 49132
* "Joe Fallon" <jf******@nospamtwcny.rr.com> scripsit:
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)
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
Always figure it out 5 seconds after posting!! <g>

If Object.GetType Is GetType(String) Then

--
Joe Fallon


"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?)
--
Joe Fallon

Nov 20 '05 #3
On Wed, 22 Oct 2003 16:39:56 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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
....

Nov 20 '05 #4
If TypeOf Object Is String Then

Perfect!
Thanks!
--
Joe Fallon

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bn************@ID-208219.news.uni-berlin.de...
* "Joe Fallon" <jf******@nospamtwcny.rr.com> scripsit:
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)
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #5
"Joe Fallon" <jf******@nospamtwcny.rr.com> schrieb
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
--
Armin

Nov 20 '05 #6
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ek*************@TK2MSFTNGP10.phx.gbl...
Always figure it out 5 seconds after posting!! <g>

If Object.GetType Is GetType(String) Then

--
Joe Fallon


"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?)
--
Joe Fallon


Nov 20 '05 #7
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 [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ek*************@TK2MSFTNGP10.phx.gbl...
Always figure it out 5 seconds after posting!! <g>

If Object.GetType Is GetType(String) Then

--
Joe Fallon


"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?)
--
Joe Fallon



Nov 20 '05 #8

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?)
--
Joe Fallon

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.
Nov 20 '05 #9
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
--
Joe Fallon

"Gary Owsiany" <ga*****@swbell.net> wrote in message
news:Qp****************@newssvr24.news.prodigy.com ...

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?)
--
Joe Fallon

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.

Nov 20 '05 #10
"Joe Fallon" <jf******@nospamtwcny.rr.com> schrieb
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?
--
Armin

Nov 20 '05 #11
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:uE*************@TK2MSFTNGP10.phx.gbl... 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
--
Joe Fallon

"Gary Owsiany" <ga*****@swbell.net> wrote in message
news:Qp****************@newssvr24.news.prodigy.com ...

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?)
--
Joe Fallon

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.


Nov 20 '05 #12
Everyone,

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

Appreciate the advice!
--
Joe Fallon

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:OX**************@TK2MSFTNGP09.phx.gbl...
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:uE*************@TK2MSFTNGP10.phx.gbl...
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
--
Joe Fallon

"Gary Owsiany" <ga*****@swbell.net> wrote in message
news:Qp****************@newssvr24.news.prodigy.com ...

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
> 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?)
> --
> Joe Fallon
>
>
>
>
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.



Nov 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brenda | last post by:
We are currently switching to DB2/AIX. I am modifying my sqrs to work on this new platform. (We are currently on Oracle). I am having a problem with an sqr that has a reference to a variable for...
1
by: Jean Stax | last post by:
Hi ! A couple of pretty basic questions: Value types: As far as I understand, when I create value type without "new" syntax the object is considered as unutilized. Consequently, I have to...
1
by: Jean Stax | last post by:
Hi ! A couple of pretty basic questions: Value types: As far as I understand, when I create value type without "new" syntax the object is considered as unutilized. Consequently, I have to...
6
by: SevDer | last post by:
Is there a way to test guid string? I want to do it without try catch block to save on performance. Thanks in advance. -- SevDer
3
by: MMiGG | last post by:
Hi Our project need parse JAVA serialized object string in C, has any library? Thanx
13
by: titan nyquist | last post by:
How do you test a string to see if it contains special characters? I want to ensure that any names typed into my form has only letters (and maybe allow a dash and an apostrophe). I can loop...
0
by: Java25 | last post by:
I am trying to insert a date to my access database and i get the error below. please help: Syntax error in string in query expression ''Fri Jun 08 10:49:00 CAT 2007)'. I only want in the...
4
by: srinathvs | last post by:
Hi, I have an access db that I am trying to query from a vb6 program. I've the following code: Dim sSQLQuery As String sSQLQuery = "SELECT * FROM TblData WHERE ID = " & Chr(39) & ID &...
5
by: rolltide | last post by:
I've seen many similar threads, but despite repeated efforts I cannot figure out my problem. I am running Access 2003, VB 6.5, Office XP Pro. Code excerpt is below (you can see where I've tried...
2
by: David Knock | last post by:
I am a noob and was wondering how to fix this problem Error: syntax error before string constant I am trying to write the simple hello world code here it is #include "stdlib.h" int main ()...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.