473,473 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

typeof question

MM
Very basic question:

string mystr

how do you test if it is a string?

Something like

if (mystr.GetType() == "System.String") <<< but this doesn't work.

Thanks for pointing me in the right direction.
Nov 17 '05 #1
6 7220
Lookup the "AS" keyword. Example stright out of MSDN.

public static void Main()
{
object [] myObjects = new object[6];
myObjects[0] = new MyClass1();
myObjects[1] = new MyClass2();
myObjects[2] = "hello";
myObjects[3] = 123;
myObjects[4] = 123.4;
myObjects[5] = null;

for (int i = 0; i < myObjects.Length; ++i)
{
string s = myObjects[i] as string;
Console.Write ("{0}:", i);
if (s != null)
Console.WriteLine ( "'" + s + "'" );
else
Console.WriteLine ( "not a string" );
}
}
}

Jason Newell, MCAD
Software Engineer
MM wrote:
Very basic question:

string mystr

how do you test if it is a string?

Something like

if (mystr.GetType() == "System.String") <<< but this doesn't work.

Thanks for pointing me in the right direction.

Nov 17 '05 #2

if(str.GetType().Equals(typeof(System.String)))
{
//true
}

HTH
Erick Sgarbi

-----Original Message-----
From: MM [mailto:mm@newsgroups.com]
Posted At: Tuesday, 2 August 2005 8:23 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: typeof question
Subject: typeof question

Very basic question:

string mystr

how do you test if it is a string?

Something like

if (mystr.GetType() == "System.String") <<< but this doesn't work.

Thanks for pointing me in the right direction.

Nov 17 '05 #3

"MM" <mm@newsgroups.com> wrote in message
news:ub**************@tk2msftngp13.phx.gbl...
Very basic question:

string mystr

how do you test if it is a string?

Something like

if (mystr.GetType() == "System.String") <<< but this doesn't work.

Thanks for pointing me in the right direction.


string s = "My String";
bool b = true;

if (s is string) <<< works for me, returns true.
if (b is string) <<< works for me, returns false.
if (b is bool) <<< works for me, returns true.

HTH,
Mythran
Nov 17 '05 #4
Erick Sgarbi <no***********@csharpbox.com> wrote:
if(str.GetType().Equals(typeof(System.String)))
{
//true
}


Note that that will only work for "precise" matches. While it doesn't
matter for String (which is a sealed class anyway), that wouldn't help
if you wanted to know if a reference was a reference to an instance of
a Stream, and didn't care what kind of Stream it was. That's where
"is" (or "as") come in:

if (foo is Stream)
{
....
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Jason Newell <no****@nospam.com> wrote:
Lookup the "AS" keyword. Example stright out of MSDN.


Using "as" just to check whether or not something is an instance of a
type isn't as straightforward as using "is". "as" is usually used when
you then want to use the cast version of the reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
I concur.

Jason Newell
Jon Skeet [C# MVP] wrote:
Jason Newell <no****@nospam.com> wrote:
Lookup the "AS" keyword. Example stright out of MSDN.

Using "as" just to check whether or not something is an instance of a
type isn't as straightforward as using "is". "as" is usually used when
you then want to use the cast version of the reference.

Nov 17 '05 #7

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

Similar topics

3
by: James Marshall | last post by:
I need to detect the type of an object, more than just "object" as typeof gives us. I'm writing a general handler that accepts a variety of objects and properties, and acts accordingly depending...
4
by: pn | last post by:
Hi when comparing a Type object to several built-in types, do you recommend to use Type.GetTypeCode() instead of typeof() ? Is there a BIG performance difference Example if (type ==...
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
by: Muckeypuck | last post by:
hello, i would like to write a function that takes a webcontrol type as a parameter and returns an array of controls based on the type some thing like: ...
4
by: EManning | last post by:
Using A2003. I've got an option group that has a number of check boxes. I have coding to clear the option group if the user wishes to cancel their choice. This coding also clears the rest of the...
20
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
11
by: Einar Værnes | last post by:
Hi. I am trying to programatically decide which type a new object should have, but the typeof-function is apparently not the answer, as the following code will not compile. class...
20
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof()...
6
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
i'm trying to modify some code to accept an enum then use reflection to fill a dropdown list. If i name the type directly, replace enm with the real name of the enum it works, no suprise. Question...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.