Question about a download sample:

Add-in Express™ Support Service
That's what is more important than anything else

Question about a download sample:
How to show an advanced Excel task pane dynamically 
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Andrei,

Thanks for your explanation.

I think that I have added a few questions while you were typing your answers.

Would you mind to look again at these questions?

Leon
Posted 06 Feb, 2019 07:16:16 Top
Andrei Smolin


Add-in Express team


Posts: 16956
Joined: 2006-05-11
Leon Lai Kan writes:
. . . you are casting what into what?


The declaration of TryCast suggests that I cast taskPaneInstance to the type ADXExcelTaskPane1.

- taskPaneInstance is a variable referencing an object of type AddinExpress.XL.ADXExcelTaskpane; that type is declared in AddinExpress.XL.2005.dll.
- ADXExcelTaskPane1 is a class (and type) declared in your project.
- I assume that ADXExcelTaskPane1 descends from (=inherits) AddinExpress.XL.ADXExcelTaskpane; if this assumption is wrong, TryCast returns Nothing.

If I miss some questions, please point me to them.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 06 Feb, 2019 07:27:38 Top
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Andrei,

Thanks again.

I need to find my way through this maze of programming a COM add-in.

I "invented" a theory (or call it a mnemonic) to find my way.

I regard AddinExpress.XL.ADXExcelTaskPane as the GENERIC task pane which is INBUILT in Add-In Express.

I regard MyAddin1.ADXExcelTaskPane1 as the ACTUAL task pane which I build using the Designer.

Everything is very clear to me if I use this "mnemonic" and the foll. code makes sense:
TryCast(task[PaneInstance1, ADXExcelTaskPane1)

It is casting the GENERIC Task Pane to my ACTUAL Task Pane.

Whether this is the real reason, or just a mnemonic is another thing. But it helps me navigate through the complicated codes.





Yes, you probably missed this question:
Apart from Task Panes, are there other objects that exist in >=2 versions (Generic / Actual) that we must convert using TryCast?
Some examples?


Leon
Posted 06 Feb, 2019 07:49:16 Top
Andrei Smolin


Add-in Express team


Posts: 16956
Joined: 2006-05-11
The mnemonic used is okay if it lets you understand the things better.

Note that you may be required to invent something else to describe this inheritance line: a geometrical figure - rectangle - rounded rectangle. Or, a quadruped - ... - cat in my example above.

Leon Lai Kan writes:
Some examples?


Declare a class descending from any other class and you'll get your generic and actual types. Say, at https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/inheritance-basics, see how BonusPayroll is declared.

Ah, you should also know that objects of any type (including your custom types) descend from System.Object. This is why your type contains e.g. the ToString() method.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 06 Feb, 2019 08:01:06 Top
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Thanks, Andrei

Great Help!

Leon
Posted 07 Feb, 2019 01:05:12 Top
Andrei Smolin


Add-in Express team


Posts: 16956
Joined: 2006-05-11
You are welcome!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Feb, 2019 04:21:58 Top
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Hi, Andrei

I am bothering you again :(

Again it's about the Private Property TaskPane():

Private ReadOnly Property TaskPane() As ADXExcelTaskPane1

    Get
       Dim taskPaneInstance1 As AddinExpress.XL.ADXExcelTaskPane = Nothing

       taskPaneInstance1 = AdxExcelTaskPanesCollectionItem1.TaskPaneInstance

       If taskPaneInstance1 Is Nothing Then
           taskPaneInstance1 = AdxExcelTaskPanesCollectionItem1.CreateTaskPaneInstance()
       End If

       Return TryCast(taskPaneInstance1, ADXExcelTaskPane1) '// Original Code as per Sample download

      'Return taskPaneInstance1           '//  Alternative code by Leon
            
    End Get

End Property



TryCast is still playing hide and seek in my mind!

The sample works perfectly with the original code containing TryCast:
Return TryCast(taskPaneInstance1, ADXExcelTaskPane1)


But if I replace that code with the following (which does not use TryCast) , it works just as fine!
Return taskPaneInstance1


So, what's the use of TryCast here?

Why should you cast a GENERIC task pane into an ACTUAL task pane when you could use the GENERIC task pane directly?

Is taskPaneInstance1 really a GENERIC task pane? or is it the ACTUAL task pane?


Sorry to be a nuisance. I am just trying to understand! Maybe I am missing something.

Leon
Posted 07 Feb, 2019 05:09:07 Top
Andrei Smolin


Add-in Express team


Posts: 16956
Joined: 2006-05-11
Hello Leon,

First off, note that the TaskPane property returns ADXExcelTaskPane1, not ADXExcelTaskPane. If you set Option Strict On, you'll see the following compile-time error:

BC30512 Option Strict On disallows implicit conversions from 'ADXExcelTaskPane' to 'ADXExcelTaskPane1'.

I strongly suggest that you set Option Strict On in your project, in menu Tools | Options | Projects and Solutions | VB Defaults.

You've found that you can pass any of the related types with Option Strict Off and this looks natural to you. This works because behind the scenes, the compiler performs an implicit cast for you. I warn you that this is the source of most difficult issues. They occur on this pattern: you write something in code and the compiler, assuming it is more clever than you, changes what you've just done, you CANNOT see the change and you find the issue at the run-time. You should know that the cost of an error depends on the stage when it was detected: the most cheap errors are compile-time ones. Ergo: let the compiler find them for you.

Setting Option Strict On makes you formulate your intentions explicitly. The more symbols to type, the less time to spend on debugging. Look at how much time you've spent on this property. Don't think this is because you learn the language! I assume this time is spent mostly because of Option Strict set to Off!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Feb, 2019 06:30:44 Top
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Andrei,

Thanks a lot for reminding me again about the necessity of setting Option Strict On.

I twist my ears 360 degrees! Will never make such mistake again!


Please look at these 2 lines:
Dim taskPaneInstance1 As AddinExpress.XL.ADXExcelTaskPane = Nothing
taskPaneInstance1 = AdxExcelTaskPanesCollectionItem1.TaskPaneInstance



The 1st line dims taskPaneInstance1 as a GENERIC task pane. Right?

But does the 2nd line not change it into an ACTUAL task pane?
(because TaskPaneInstance represents the ACTUAL task pane) Right / Wrong?

Or is it the case that because we dim it as Generic, it remains a Generic task pane? Right / Wrong?

Leon
Posted 07 Feb, 2019 07:30:30 Top
Andrei Smolin


Add-in Express team


Posts: 16956
Joined: 2006-05-11
Leon,

AdxExcelTaskPanesCollectionItem1.TaskPaneInstance returns an object of the AddinExpress.XL.ADXExcelTaskPane type. Since that object is *your* pane, you can cast it to the type of your pane (ADXExcelTaskPane1).

There's no change at all. A variable (e.g. taskPaneInstance1) is a small memory area containing a reference to a bigger memory area that contains the real object (the actual task pane instance information).

You look at the same star (the bigger area) using different filters or telescopes (types) to see (use) different aspects (properties/methods) of it. Replacing a filter or telescope (casting to another type) doesn't change the star (the bigger area).

Casting the variable to a related type simply informs the compiler what set of properties/methods (a type is simply a set of properties/methods) to use when accessing the bigger area. You can create variables of different types (say Object, ADXExcelTaskPane and ADXExcelTaskPane1) and use them to access the bigger area.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Feb, 2019 07:59:33 Top