Where do I put my constants?

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

Where do I put my constants?
In a VB.NET XLL 
Henri Pellemans


Guest


Hi at ADX,

Perhaps this is a very easy question, but where do I put my constants in a VB.NET XLL add-in?

For updating purposes I would like to put the constants as close as possible to my UDFs. See the code below:


#Region " Define your UDFs in this section "
    'The container for user-defined functions (UDFs). 
    'Every UDF is a public static (Public Shared in VB.NET) method that returns a value of any base type: string, double, integer.
    Friend Class XLLContainer
        'Required by Add-in Express. Please do not modify this method.
        Friend Shared ReadOnly Property _Module() As Henri.XLLModule
            Get
                Return CType(AddinExpress.MSO.ADXXLLModule.CurrentInstance, Henri.XLLModule)
            End Get
        End Property

        Const myConstant As Integer = 120

#Region " Sample function "
Posted 07 Mar, 2013 08:48:53 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hi Henri,

At http://www.add-in-express.com/forum/read.php?FID=5&TID=4512, there's this code fragment:

' Additional class with constants   
Friend Class Constant   
    'window messages   
    Public Shared ReadOnly WM_USER As Integer = 1024   
    'custom   
    Public Shared ReadOnly WM_TEST As Integer = WM_USER + 2000   
End Class 


It can be used as follows:

If e.Message = Constant.WM_TEST Then   
    txtLabel.Text = "hello"   
End If   


Is this what you are looking for?


Andrei Smolin
Add-in Express Team Leader
Posted 11 Mar, 2013 11:04:50 Top
Henri Pellemans


Guest


Hi Andrei,

Your suggestion to use a class for my constants is great!

I placed the Constant class in the XLLModule as follows:


Imports ...
Imports ...
...

Friend Class Constant
...
End Class

Public Class XLLModule
...
End Class


Now the constants are easy to find in case of updates, and I can use them also in the AddinModule [in the same project].

Thank you very much,

Henri
Posted 12 Mar, 2013 06:29:52 Top
Henri Pellemans


Guest


The solution seemed to work perfectly, but I just got the following warning when I switched to the XLL Design tab:


The class XLLModule can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.

After some trial and error I moved the Constant Class into the XLLContainer. In this way the constants are still available in my AddinModule [in the same project]. In the AddinModule I have the following code, so I can use my UDFs and constants in the AddinModule


Imports Henri.XLLModule.XLLContainer


The code in the XLLModule is as follows:


...
#Region " Define your UDFs in this section "
    'The container for user-defined functions (UDFs). 
    'Every UDF is a public static (Public Shared in VB.NET) method ...
    Friend Class XLLContainer
        'Required by Add-in Express. Please do not modify this method.
        Friend Shared ReadOnly Property _Module() As Henri.XLLModule
            Get
                Return CType(AddinExpress.MSO.ADXXLLModule.CurrentInstance, Henri.XLLModule)
            End Get
        End Property
        Friend Class Constant
            Public Shared ReadOnly maxLife As Integer = 120
            Public Shared ReadOnly maxCalc As Integer = maxLife * 2
        End Class
#Region " Sample function "
        Public Shared Function AllSupportedExcelTypes(ByVal arg As Object) As String
...
Posted 16 Mar, 2013 07:10:44 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hi Henri,

You can also move this class after the add-in module's class:

Public Class AddinModule
    Inherits AddinExpress.MSO.ADXAddinModule
...
End Class

Friend Class Constant   
    Public Shared ReadOnly maxLife As Integer = 120   
    Public Shared ReadOnly maxCalc As Integer = maxLife * 2   
End Class



Andrei Smolin
Add-in Express Team Leader
Posted 18 Mar, 2013 05:35:52 Top