Struggling with first Add-in - Using Global Variables

Struggling with first Add-in - Using Global Variables

Anonymous
Not applicable
632 Views
3 Replies
Message 1 of 4

Struggling with first Add-in - Using Global Variables

Anonymous
Not applicable
Hi all,

I've been working on converting a VBA macro to an Add-In (my first). I thought all was going well, but I'm struggling to share the InventorApplication object within my add-in...

All the googling I've done suggests the most suitable way is to declare it as a global variable. So I followed the work flow on this page: (http://visualbasic.about.com/od/usevb6/l/blfaq0013a.htm) and created a module to declare my global variables. I've then referenced this module back in the original class, but when I do, I get a message saying "modDeclarations is a type and cannot be used as an expression"... So obviously I'm missing something.

So here's what I've got:
Code:
Imports Inventor
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Windows.Forms

Namespace HDALLabelMaker
    <ProgIdAttribute("HDAL Label Maker.StandardAddInServer"), _
    GuidAttribute("57440e51-bcc8-4abb-a1f0-09493c3981de")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        ' Inventor application object.
        'Public m_inventorApplication As Inventor.Application
        Private m_ClientID As String
        Private WithEvents oButtonActivate As Inventor.ButtonDefinition

#Region "ApplicationAddInServer Members"

        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate

            ' This method is called by Inventor when it loads the AddIn.
            ' The AddInSiteObject provides access to the Inventor Application object.
            ' The FirstTime flag indicates if the AddIn is loaded for the first time.

            ' Initialize AddIn members.
            modDeclarations()
            m_inventorApplication = addInSiteObject.Application

 



and my modDeclarations looks like this:

Code:
Public Module modDeclarations
    Public m_inventorApplication As Inventor.Application
End Module

 

I've also attached my full project if anyone is interested.

If anyone has a good example of using a global variable to access the InventorApplication I'm sure I could figure it out.

Any help would be greatly appreciated. I really can't proceed without solving this one...

 

0 Likes
633 Views
3 Replies
Replies (3)
Message 2 of 4

TerryWen
Alumni
Alumni

i think first you should declare your global variable as shared

 

Public Module modDeclarations
    Public Shared m_inventorApplication As Inventor.Application
End Module

 

when you use it

 

' Initialize AddIn members.
modDeclarations.m_inventorApplication = addInSiteObject.Application

0 Likes
Message 3 of 4

Anonymous
Not applicable
Thanks for the tip... Forgive me for asking, but what's the difference between declaring it as 'Public' and declaring it as 'Public Shared'?
0 Likes
Message 4 of 4

TerryWen
Alumni
Alumni

Gerrard,

 

Shared means this variable is a global, and have only one instance. so it can be accessed without an instance of the class. in this case, you can access it through class directly, such as MyClass.MyVariable.

 

If you only declare a variable without Shared, that means this variable is a member of a class instance. it's not global (every instance of the class has a variable), and only can be accessed with an instance of the class. In this case, you need create an instance of the class first, and then access it, such as MyClassInstance.MyVariable (MyClassInstance is an instance of class MyClass).

 

Hope i explained it clearly.

0 Likes