I have a few modules and classes that I need to init themselves once added to the project.
I'm looking for a method mechanism to call a routine(or something) during startup, without explicitly calling it.
I tried the following, with the hope that being MyInit public, will be initialized upon start.
Optimization is preventing creation of MyInit until the first reference.
Partial Public Module InitModule
public MyInit As New MyStart()
Public Class MyStart
Public Sub New()
Debug.Writeline("Yes, init is executed")
End Sub
End Class
End Module
I have a few modules that can be added/linked to multiple projects; when included they provide trace, code verification, version control etc. The idea is that to add or remove, the main code need no changes.
It doesn't matter is if it's a class, sub or function: I want this to run without explicit call.
Thanks #Jimi for his hint
On each module I can add this partial snippet, extending MyApplication class and using the Startup event to do the initialization.
This can be repeated multiple times and me.Startup will be cascaded as needed.
The drawback: MyApplication is available on windows forms only.
#if TARGET="winexe" then
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As ApplicationServices.StartupEventArgs) Handles Me.Startup
My.Application.Log.WriteEntry($"Application start {My.Computer.Clock.ToString}")
' init module stuff
'...
End Sub
End Class
End Namespace
#end if
Update
I found a very easy and straightforward code that works with all targets and not just winexe (forms)
The idea is to force the creation of the class at initialization:
friend _startup_init as new startup_init with {.name="test"}
friend class startup_init
public name as string
public sub new()
'... do here your init,
' in my case a few addhandler to hook the master process
end sub
end class
Related
First of all: I have 3 interfaces, each has some class holding required assembly & license data,the first two interfaces are implemented by the next one(s): ISolution --> IExtension --> IExportPlugin, and their objects are inherited from the base class - Solution.
ISolution can be implemented by any application, at least to provide licensing features.
IExtension is a base for the IExport, IImport, IService etc - I planned a lot of derivatives, if it will be neccessary, the license checking is required for every type of extension (plugin).
Each derivative class initiates the calling LicenseCheck sub in New() of base class, which shows frmActivate dialog if the license not found.
So, in inheritance, it shows ACTIVATE three times!!!.
Say, please, can I prevent this window showing, except the last time, when IExportPlugin created?
Yes, I can add optional parameter 'OmitLicenseChecking', but it`s not the best idea due to security reasons.
Every program (exe or dll) must incapsulate it without any possibility to avoid checking. E.g. Checking is a must and must be hidden.
Thanks, friends.
And sorry for my English))
A guess based on the information provided
Public MustInherit Class Solution
Public Shared Lock As New Object
Public Shared Shown As Boolean
Public DoShow As Boolean
Public Sub New()
SyncLock Lock
If Not Shown Then
Shown = True
Me.DoShow = True
Else
Me.DoShow = False
End If
End SyncLock
End Sub
End Class
Public Class SolutionA : Inherits Solution
Public Sub New()
If Me.DoShow Then
Stop
End If
End Sub
End Class
Public Class SolutionB : Inherits Solution
Public Sub New()
If Me.DoShow Then
Stop
End If
End Sub
End Class
Public Class SolutionC : Inherits Solution
Public Sub New()
If Me.DoShow Then
Stop
End If
End Sub
End Class
To test, where the Stop represents the dialog,
Dim foo3 As New SolutionC
Dim foo2 As New SolutionB
Dim foo1 As New SolutionA
I'm not sure whether that's the best title but what I'm trying to do is set certain Subs and Functions to be only accessible from other functions by qualifying the exact location.
For example, I have a module called modShared.
In this module there is a function called LogForm which returns a Form.
In other areas of my code I have functions where the name begins with Log so I want the LogForm function to only be accessible and only appear on Intellisense when I type in modShared.LogForm and not just LogForm.
Is this possible because it would help me immensely?
Thanks
Just change your module to a static class and apply the Shared modifier to every method and you should be good to go.
Public NotInheritable Class modShared
Private Sub New() 'Prevent initialization.
End Sub
Public Shared Function LogForm() As Form
'Do stuff...
End Function
End Class
One way of doing this is placing your modules inside a Namespace
Namespace UtilityMethods
Module modShared
Public sub LogForm()
'Code Here
End sub
End Module
End Namespace
And you could use this code by either calling:
UtilityMethods.LogForm
or
Namespace UtilityMethods
Public Class MyClass
Public sub ClassMethod
LogForm()
end sub
End Class
End Namespace
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.
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
I've created an add-in for outlook 2010. I have a ribbon that has a button on it. When you click that button, I want it to call a procedure in the ThisAddIn.vb.
There are two files: ThisAddin.vb and Ribbon.vb.
I've tried several things to no avail. I've also set all the procedures to public.
Call Testing123()
Call ThisAddIn.Testing123()
Etc
How do I properly call this procedure?
****Ribbon1.vb****
Imports Microsoft.Office.Tools.Ribbon
Public Class MyOutlookTab
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
Call Testing123()
End Sub
End Class
***ThisAddIn.vb***
Public Class ThisAddIn
Public Sub Testing123()
System.Windows.Forms.MessageBox.Show("It Works!")
End Sub
End Class
The problem is that you are trying to reference class methods without creating a class.
You have three options to make this work:
1) Convert ThisAddIn to a Module. Then there won't be any issues accessing the Testing123 method as you currently have it.
2) Convert ThisAddin.Testing123 to a Shared method, i.e.:
Public Shared Sub Testing123()
Then you would access as follows:
Call ThisAddin.Testing123()
3) Create an instance of the ThisAddIn class prior to using its methods:
Dim oAddIn As New ThisAddIn
Call oAddIn.Testing123()
Update
It appears that addins are treated differently that standard classes.
This MSDN article contains specific implementation guidance for accessing AddIn functionality from other types of solutions.
Based on this article, you need to take a couple of additional steps:
1) Create an interface to expose the functionality from your AddIn:
<ComVisible(True)> _
Public Interface IAddInUtilities
Sub Testing123()
End Interface
2) Add a utilities class to your addin project:
<ComVisible(True)> _
<ClassInterface(ClassInterfaceType.None)> _
Public Class AddInUtilities
Implements IAddInUtilities
Public Sub Testing123() Implements IAddInUtilities.Testing123
System.Windows.Forms.MessageBox.Show("It Works!")
End Sub
End Class
3) Add the following to ThisAddIn to expose the utilities to external callers:
Private utilities As AddInUtilities
Protected Overrides Function RequestComAddInAutomationService() As Object
If utilities Is Nothing Then
utilities = New AddInUtilities()
End If
Return utilities
End Function
4) I am a little unclear on the exact syntax needed for the last step since I don't have automation installed in office, but you will need to do something along these lines:
' OutlookTest should be changed to the name of the project ThisAddIn is in
Dim addIn As Office.COMAddIn = Globals.ThisAddIn.Application.COMAddIns.Item("OutlookTest")
Dim utilities As OutlookTest.IAddInUtilities = TryCast( _
addIn.Object, OutlookTest.IAddInUtilities)
utilities.Testing123()
Thanks for everyones comments but I found the solution in an example here: http://msdn.microsoft.com/en-us/library/ee620548.aspx where they talk about adding a ribbon to the meeting request (2/3's of the way down).
It's actually quite simple. You call the procedure using the "Global"
Globals.ThisAddIn.Testing123()
Nothing else is needed.
You have to create a new instance of the class before you can call it in vb.net!
So something like should allow you to call it..
Public Class MyOutlookTab
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
Dim testing As New ThisAddIn()
Call testing.Testing123()
End Sub
End Class
I have a two windows forms classes, a base class and a derived class. The base class has an event handler which handles ValueChanged on some component. I have also written a different event handler for the same event on the derived class.
When I create an instance of the derived class and fire the event, I find that both event handlers run (the base class one and then the derived class one). But I want only the handler in the derived class to run.
Is this possible and if so how do I do it?
(This is .NET 3.5)
Thanks!
Edit: Here is what the code looks like (can't post the actual code):
Public Class BaseForm
Inherits System.Windows.Forms.UserControl
(Windows Form Designer Generated Code)
Private WithEvents myControl As New SomeOtherControl
Protected value As String
Private Sub myControl_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myControl.ValueChanged
value = SomeLogic()
End Sub
End Class
Public Class DerivedForm
Inherits BaseForm
Private WithEvents myControl As New SomeOtherControl
Private Sub myControl_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myControl.ValueChanged
value = myControl.Value
End Sub
End Class
You can attach multiple handlers to one event so what you are seeing is by design (you have two handlers attached to the same event so they both fire).
That being said there are ways to accomplish what you are trying to do. One way is to move the code out of your base class handler into an overridable method, and then in the derived class override the method. So your base class handler only has 1 line of code,calling the new method you made. If you inherit from the class it will call your overridden method instead.
Public Class BaseForm
Private Sub myControl_ValueChanged() Handles myControl.ValueChanged
DoSomeLogic()
End Sub
Protected Overridable Sub DoSomeLogic()
'original logic here
End Sub
End Class
Public Class ChildForm
Inherits BaseForm
Protected Overrides Sub DoSomeLogic()
'other logic here
End Sub
End Class
I don't believe you can control that.
If it were me, I think I'd intercept the event in the base class, as you are doing, then define ANOTHER event from the base class and raise that event FROM the event handler in the base class.
on return, if the event was handled (presumably by your derived class), then just exit the base event handler, otherwise, proceed with the base event handling.