Ask Your Question
0

How do I determine an object's type?

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

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 close merge delete

1 Answer

Sort by » oldest newest most voted
0

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

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

Comments

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 +0200 )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("com.sun.star.sheet.SheetCellRange") 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 +0200 )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 "com.sun.star.sheet.SheetCell" is only supported by single cells.

Lupp gravatar imageLupp ( 2017-02-13 14:03:28 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 300 times

Last updated: Feb 12 '17