Ask Your Question

How do I determine an object's type? [closed]

asked 2017-02-12 19:04:43 +0100

donedwards gravatar image

I'm writing a function which, for one of its parameters, could sensibly take any of several types of object. E.G. a spreadsheet, range, or cell. However the different object types need slightly different handling.

How do I determine the type of an object at runtime? "Object" won't do; I need to know whether it's an object of THIS type or an object of THAT type.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2020-09-24 14:03:14.302589

1 Answer

Sort by » oldest newest most voted

answered 2017-02-12 20:50:26 +0100

Lupp gravatar image

Look for the properties 'SupportedServiceNames' (an array) and/or 'ImplementationName'. Mostly the first variant will offer what you need.

edit flag offensive delete link more


Okay, that answers the initial question but... looking for a list of ImplementationNames I found a page discussing a (former) bug, where several people expressed that it's a poor coding practice to compare ImplementationName against a string literal. Other than find/generate an object of the type I'm interested in (which in some cases could be expensive or problematic) and compare the two objects' ImplementationNames, (and/or whole arrays of service names) what do I compare against?

donedwards gravatar imagedonedwards ( 2017-02-13 01:52:55 +0100 )edit

You will use services and thus need to know if a specific service is supported by an object.
What we call a spreadsheet or a single cell, now pOject simply, both also answer true to the question pObject.SupportsService("") like a generic cell range does. Since access to properties (even if an abbreviating notation exists) is provided by services that's enough.
Best read the famous texts by Andrew Pitonyak.

Lupp gravatar imageLupp ( 2017-02-13 13:58:04 +0100 )edit

Writing code for more than one kind of objects is a well established practise - assuming that it only depends on services supported by all of them.
Please note that SupportsService() is working case sensitive.
Distinction by ImplementationName is doubtabel.
If urgently needed you should realize that, e.g, a cell will support the SheetCellRange service, but "" is only supported by single cells.

Lupp gravatar imageLupp ( 2017-02-13 14:03:28 +0100 )edit

Question Tools

1 follower


Asked: 2017-02-12 19:04:43 +0100

Seen: 766 times

Last updated: Feb 12 '17