What is the difference between using set/invoke and dot notation?

4 visualizaciones (últimos 30 días)
Joel
Joel el 2 de Oct. de 2012
I've had to work with an Excel automation server a few times and I'm confused about the difference between using set or invoke and using dot notation.
for example the following two lines appear to function identically:
Workbook = Excel.Workbooks.Open(fileName);
Workbook = invoke(Excel.Workbooks, 'open', fileName);
Same with these two lines as well:
Sheets.Item('TestSheet').Rows(1).font.bold = 1;
set(Sheets.Item('TestSheet').Rows(1).font,'bold', 1)
What is difference. Why would I use one method over the other?

Respuestas (1)

Daniel Shub
Daniel Shub el 2 de Oct. de 2012
The processing is slightly different with the two methods, but in general, they tend to produce the same result.
Workbook = invoke(Excel.Workbooks, 'open', fileName);
Determines the class of Excel.Workbooks and then calls the invoke method of that class and passes it Excel.Workbooks, 'open', and fileName.
Workbook = Excel.Workbooks.Open(fileName);
Determines the class of Excel.Workbooks and then calls the subsref method of that class and passes it all sort of information. The subsref method then usually will call invoke(Excel.Workbooks, 'open', fileName).
If the class is using the built-in subsref function, the two behave nearly identically. The overhead of the built-in subsref is small. If the class has an overloaded subsref, you can get all sorts of odd behavior. Further, in my experience, the overhead of an overloaded subsref method is quite large.
  2 comentarios
Image Analyst
Image Analyst el 2 de Oct. de 2012
I'd been wondering that myself. So it kind of sounds like the direct way of calling is simpler and preferable to the invoke way. If there's any advantage to using invoke(), please let us know.
Daniel Shub
Daniel Shub el 2 de Oct. de 2012
I probably should have added in the answer that I am not sure if activeX objects behave like other MATLAB objects. For standard OOP I think the direct call is better (e.g., invoke). If there is an overloaded SUBSREF method the direct call will skip the call to |subsref which can/might save time. That said, if my class needs an overloaded SUBSREF, I tend to try and run away.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by