What is the difference between using set/invoke and dot notation?
4 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
Daniel Shub
2012-10-2
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 个评论
Image Analyst
2012-10-2
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
2012-10-2
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Timetables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!