473,320 Members | 1,746 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,320 software developers and data experts.

Does typeof or is work when dealing with an interface?

If I'm using DictionaryBase derived object lists and I iterate through them
with the IDictionary interface can I still check the object type by calling
typeof()?

btw I'm is using .NET 1.1

Thanks,
Jim

ex.
bool FindSpectialItem(string key, DictionaryBase[] MyDictionaryArray)
{
foreach(IDictionary dict in MyDictionaryArray)
{
bool found = false;
DerivedDictionaryA derivedDict = new DerivedDictionaryA ();

if(dict is DerivedDictionaryA) //or if(derivedDict.GetType() ==
typeof(DerivedDictionaryA))
{
//found a certain type
//look for key
found = true
}
else
continue;
}

return found;
}
Jan 30 '07 #1
4 1468
Hi,

Of course, typeof ALWAYS return the type of the reference
--
Ignacio Machin
machin AT laceupsolutions com
"Jim H" <ji**@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
| If I'm using DictionaryBase derived object lists and I iterate through
them
| with the IDictionary interface can I still check the object type by
calling
| typeof()?
|
| btw I'm is using .NET 1.1
|
| Thanks,
| Jim
|
| ex.
| bool FindSpectialItem(string key, DictionaryBase[] MyDictionaryArray)
| {
| foreach(IDictionary dict in MyDictionaryArray)
| {
| bool found = false;
| DerivedDictionaryA derivedDict = new DerivedDictionaryA ();
|
| if(dict is DerivedDictionaryA) //or if(derivedDict.GetType() ==
| typeof(DerivedDictionaryA))
| {
| //found a certain type
| //look for key
| found = true
| }
| else
| continue;
| }
|
| return found;
| }
|
|
Jan 30 '07 #2
Thanks!

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi,

Of course, typeof ALWAYS return the type of the reference
--
Ignacio Machin
machin AT laceupsolutions com
"Jim H" <ji**@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
| If I'm using DictionaryBase derived object lists and I iterate through
them
| with the IDictionary interface can I still check the object type by
calling
| typeof()?
|
| btw I'm is using .NET 1.1
|
| Thanks,
| Jim
|
| ex.
| bool FindSpectialItem(string key, DictionaryBase[] MyDictionaryArray)
| {
| foreach(IDictionary dict in MyDictionaryArray)
| {
| bool found = false;
| DerivedDictionaryA derivedDict = new DerivedDictionaryA ();
|
| if(dict is DerivedDictionaryA) //or if(derivedDict.GetType() ==
| typeof(DerivedDictionaryA))
| {
| //found a certain type
| //look for key
| found = true
| }
| else
| continue;
| }
|
| return found;
| }
|
|


Jan 30 '07 #3
Hello Jim,

Yes, you can use typeof to get Type instance from both class type or
interface, interface is just one of the .NET object types. Also, another
means to check whether an object is of a given type (in C#) is using the
"as" operator:

TypeName typedobj = UncastedObject as TypeName;

if(typedobj != null)
{

...........

}

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 31 '07 #4
On Jan 30, 12:45 pm, "Jim H" <j...@nospam.nospamwrote:
if(dict is DerivedDictionaryA) //or if(derivedDict.GetType() ==
typeof(DerivedDictionaryA))
Be aware, however, that the two tests do different things.

Assuming that DerivedDictionaryA is a class, then:

dict is DerivedDictionaryA

will be true if dict is a DerivedDictionaryA or any class derived from
DerivedDictionaryA.

dict.GetType() == typeof(DerivedDictionaryA)

will be true only if dict is a DerivedDictionaryA.

Jan 31 '07 #5

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

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
4
by: Uchiha Jax | last post by:
Hello everyone, I am a plenty silly person who is trying to learn .NET remoting through trial and error (all articles I read are going over my head at the moment (mostly) so I thought i'd give...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
7
by: Joshua Frank | last post by:
Hi All, How can you tell if an object supports an interface? I've got a situation where I need to tell if an object implements Iterator, but there doesn't seem to be any operator like typeof...
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...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
7
by: Peter Morris | last post by:
interface Interface1 { } interface Interface2 : Interface1 { } class A : Interface2 {
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.