Click here to Skip to main content
15,911,715 members
Home / Discussions / C#
   

C#

 
GeneralMS Messsage Queue Pin
Login GoodSites8-Apr-04 19:38
Login GoodSites8-Apr-04 19:38 
GeneralRe: MS Messsage Queue Pin
Mazdak9-Apr-04 1:56
Mazdak9-Apr-04 1:56 
General.exe but no .dll Pin
Anonymous8-Apr-04 16:59
Anonymous8-Apr-04 16:59 
GeneralRe: .exe but no .dll Pin
Dave Kreskowiak8-Apr-04 17:31
mveDave Kreskowiak8-Apr-04 17:31 
GeneralRe: .exe but no .dll Pin
leppie9-Apr-04 6:59
leppie9-Apr-04 6:59 
GeneralChecking an objects inheritance Pin
cnurse8-Apr-04 15:25
cnurse8-Apr-04 15:25 
GeneralRe: Checking an objects inheritance Pin
jqd20018-Apr-04 18:32
jqd20018-Apr-04 18:32 
GeneralRe: Checking an objects inheritance Pin
Heath Stewart9-Apr-04 4:40
protectorHeath Stewart9-Apr-04 4:40 
You're comparing apples and oranges, there. pTable is an instance, and the typeof operator will always return a Type, so the two would never match unless pTable was a variable which held a Type.

If you want to check is something is an instance of a particular type, simply use the is operator:
if (pTable is DataTable)
{
  // ...
}
You can also use something like this, although it requires more code and more instructions (when compiled to IL):
if (pTable != null)
{
  if (pTable.GetType() == typeof(DataTable))
  {
    // ...
  }
}
There's other ways of doing this, but you don't have to take inheritance into account. If CustomerTable inherits form DataTable, then any instance is a type of CustomerTable, DataTable, MarshalByValueComponent, and - as with everything in .NET - Object. It's also an instance of any interfaces that any classes up the inheritence hierarchy implement.

This isn't true going down the hierarchy, though. For example, if you instantiate a new instance of a DataTable, then it is not an instance of a CustomerTable.

Also keep in mind that it's not the declaration that counts, its the definition. For example:
object o = new DataTable("Example");
While o is declared as an object, it's still an instance of a DataTable. Calling o.GetType will return System.Data.DataTable. This is polymorphism.

 

Microsoft MVP, Visual C#
My Articles
GeneralInheriting attributes Pin
cnurse8-Apr-04 15:18
cnurse8-Apr-04 15:18 
GeneralRe: Inheriting attributes Pin
Heath Stewart9-Apr-04 4:33
protectorHeath Stewart9-Apr-04 4:33 
GeneralC# - Self documenting code Pin
cnurse8-Apr-04 15:13
cnurse8-Apr-04 15:13 
GeneralRe: C# - Self documenting code Pin
Heath Stewart9-Apr-04 4:27
protectorHeath Stewart9-Apr-04 4:27 
GeneralRunning Hello World examples, but MessageBoxIcon Enumeration[s] don't provide unique icons. Pin
Bupkus8-Apr-04 13:31
Bupkus8-Apr-04 13:31 
GeneralRe: Running Hello World examples, but MessageBoxIcon Enumeration[s] don't provide unique icons. Pin
Colin Angus Mackay8-Apr-04 14:12
Colin Angus Mackay8-Apr-04 14:12 
GeneralRe: Running Hello World examples, but MessageBoxIcon Enumeration[s] don't provide unique icons. Pin
Heath Stewart9-Apr-04 4:22
protectorHeath Stewart9-Apr-04 4:22 
GeneralCLients Communication without server Pin
Member 7326718-Apr-04 13:18
Member 7326718-Apr-04 13:18 
GeneralRe: CLients Communication without server Pin
Colin Angus Mackay8-Apr-04 14:20
Colin Angus Mackay8-Apr-04 14:20 
GeneralRe: CLients Communication without server Pin
Heath Stewart9-Apr-04 4:11
protectorHeath Stewart9-Apr-04 4:11 
GeneralRe: CLients Communication without server Pin
Heath Stewart9-Apr-04 4:18
protectorHeath Stewart9-Apr-04 4:18 
GeneralCollectionEditor Pin
Jamie Nordmeyer8-Apr-04 12:49
Jamie Nordmeyer8-Apr-04 12:49 
GeneralRe: CollectionEditor Pin
Colin Angus Mackay8-Apr-04 14:30
Colin Angus Mackay8-Apr-04 14:30 
GeneralRe: CollectionEditor Pin
dacris8-Apr-04 18:03
dacris8-Apr-04 18:03 
GeneralRe: CollectionEditor Pin
Heath Stewart9-Apr-04 4:01
protectorHeath Stewart9-Apr-04 4:01 
GeneralRe: CollectionEditor Pin
Heath Stewart9-Apr-04 4:06
protectorHeath Stewart9-Apr-04 4:06 
GeneralRe: CollectionEditor Pin
Jamie Nordmeyer9-Apr-04 5:12
Jamie Nordmeyer9-Apr-04 5:12 

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.