2

I hope this isn't a stupid question, I can't find a reasonable answer on google.

I'm starting a project which only contains one class file. I will be turning the class file into a dll at the end. I understand that another app normally makes calls to the dll once it's referenced in the project. I need the dll to run a sub inside of it on load like a normal mybase.load sub. This sub needs to execute only once on load to populate some variables. I don't want to have to call the sub from the main app. The rest of the functions/subs in the dll will be called from the main app when needed. Please don't respond with register them globally under the class, I need a sub or function.

If there isn't such a sub how would I go about creating a function/sub that preforms an onload?

Thanks. :)

Hope I'm making sense. Thanks for your response.

user1432290
  • 151
  • 1
  • 3
  • 12
  • possible duplicate of [Initialize library on Assembly load](http://stackoverflow.com/questions/459560/initialize-library-on-assembly-load) – Konrad Rudolph Jun 24 '12 at 13:53
  • Not a stupid question by any means, but apparently not possible. Note that classical C DLLs *do* have a special function that is invoked when the DLL are loaded and unloaded. But you cannot create such a DLL in .NET, and they are pretty restricted anyway. – Konrad Rudolph Jun 24 '12 at 13:54
  • Thanks for the read. A little disappointing as I wanted to run stuff before any calls from the main app.. :/ – user1432290 Jun 24 '12 at 14:15
  • 6
    You can create a static class constructor with `Shared Sub New`. It is guaranteed to run before any of your class methods, including the constructor. Whether it is suitable is entirely unclear. – Hans Passant Jun 24 '12 at 14:26
  • @Hans, surely this is an answer, and a valid one at that. However, if everybody posts answers as comments, it renders the Unanswered filter redundant. – David Osborne Jul 05 '12 at 08:16

1 Answers1

0
Shared Sub New()

on your class.

Another option is to have a private class inside your class and initialise it with a member variable:

Public Class MyLibraryClass
  Private mobjSettings As New SettingsClass

  Public Function SampleLibraryFunction() As String
    Return mobjSettings.SettingsProperty
  End Function

  Private Class SettingsClass
    Friend SettingsProperty As String
    Sub New()
      'initialise
      SettingsProperty = "This is a test"
    End Sub
  End Class

End Class
SSS
  • 4,807
  • 1
  • 23
  • 44