Click here to Skip to main content
16,004,944 members
Home / Discussions / C#
   

C#

 
GeneralRe: comparing objects Pin
Heath Stewart17-Jun-04 1:54
protectorHeath Stewart17-Jun-04 1:54 
GeneralRe: comparing objects Pin
Heath Stewart17-Jun-04 1:49
protectorHeath Stewart17-Jun-04 1:49 
GeneralRe: comparing objects Pin
saud_a_k17-Jun-04 2:03
saud_a_k17-Jun-04 2:03 
GeneralRe: comparing objects Pin
Heath Stewart17-Jun-04 2:10
protectorHeath Stewart17-Jun-04 2:10 
GeneralRe: comparing objects Pin
saud_a_k17-Jun-04 2:41
saud_a_k17-Jun-04 2:41 
GeneralRe: comparing objects Pin
Heath Stewart17-Jun-04 3:00
protectorHeath Stewart17-Jun-04 3:00 
GeneralRe: comparing objects Pin
saud_a_k17-Jun-04 18:21
saud_a_k17-Jun-04 18:21 
GeneralRe: comparing objects Pin
Heath Stewart17-Jun-04 18:38
protectorHeath Stewart17-Jun-04 18:38 
Use is, not obj.GetType.Equals when you want to determine if an object is a type of something. So instead of using instanceOf in Java, you use is in C#. The statement I gave similar to above is only similar in concept. This results in many more instructions than the two to three instructions required to use is. What's so hard about understanding this?
if (obj is SomeClass) 
{
  // ...
}
I mean, it's practically the same as using instanceOf in Java!

If you want to determine if an object implements an interface, you must get the Type by calling GetType on the object, then search for the interfaces. A class can never be an instance of an interface, since an interface is abstract and cannot be instantiated, so is or Type.Equals cannot be used.
Type t = obj.GetType();
if (t.GetInterface("ISomeInterface"))
{
  // ...
}
There's a couple other ways of doing this as well, such as calling Type.GetInterfaces and enumerating the Type array, which is actually better because then you compare actual types instead of strings, since a class could implement two interfaces named "ISomeInterface" but that are different types (different namespaces and/or different assemblies).


Microsoft MVP, Visual C#
My Articles
GeneralRe: comparing objects Pin
saud_a_k17-Jun-04 18:52
saud_a_k17-Jun-04 18:52 
GeneralRe: comparing objects Pin
saud_a_k17-Jun-04 2:06
saud_a_k17-Jun-04 2:06 
GeneralProblems DWebBrowserEvents2_DocumentCompleteEvent Event Pin
lags200517-Jun-04 1:14
lags200517-Jun-04 1:14 
GeneralRe: Problems DWebBrowserEvents2_DocumentCompleteEvent Event Pin
Heath Stewart17-Jun-04 1:42
protectorHeath Stewart17-Jun-04 1:42 
GeneralRe: Problems DWebBrowserEvents2_DocumentCompleteEvent Event Pin
lags200517-Jun-04 2:06
lags200517-Jun-04 2:06 
GeneralRe: Problems DWebBrowserEvents2_DocumentCompleteEvent Event Pin
Heath Stewart17-Jun-04 2:14
protectorHeath Stewart17-Jun-04 2:14 
GeneralThreads Pin
bouli17-Jun-04 1:03
bouli17-Jun-04 1:03 
GeneralRe: Threads Pin
Corinna John17-Jun-04 1:32
Corinna John17-Jun-04 1:32 
GeneralLog URL's visited Pin
Stuggo17-Jun-04 1:00
Stuggo17-Jun-04 1:00 
GeneralRe: Log URL's visited Pin
Heath Stewart17-Jun-04 1:39
protectorHeath Stewart17-Jun-04 1:39 
GeneralEvents Pin
bouli17-Jun-04 1:00
bouli17-Jun-04 1:00 
GeneralRe: Events Pin
Heath Stewart17-Jun-04 1:34
protectorHeath Stewart17-Jun-04 1:34 
GeneralRe: Events Pin
Corinna John17-Jun-04 1:47
Corinna John17-Jun-04 1:47 
GeneralRe: Events Pin
Heath Stewart17-Jun-04 1:50
protectorHeath Stewart17-Jun-04 1:50 
GeneralRe: Events Pin
bouli17-Jun-04 1:56
bouli17-Jun-04 1:56 
GeneralRe: Events Pin
bouli17-Jun-04 1:48
bouli17-Jun-04 1:48 
GeneralRe: Events Pin
Heath Stewart17-Jun-04 1:52
protectorHeath Stewart17-Jun-04 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.