Vb.Net Class file on load sub? - vb.net

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

Related

Run a function or create a class application at startup

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

Force Full Path For Method

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

VB.NET Plugin Architecture

Hello I'm working on using the interface class in vb.net to make a plugin architecture. So far I've not found a step by step tutorial for a beginner. But I've gotten as far as this:
Main App
Public Class PluginHandler
Interface IApplications
Sub ChangeForms()
End Interface
Public Shared Sub GetDLLFromDir(ByVal TheDir)
For Each dll As String In System.IO.Directory.GetFiles(TheDir, "*.dll")
LoadPlugin(dll)
Next
End Sub
Public Shared Sub LoadPlugin(ByVal ThePlugin)
Dim Asm = Assembly.LoadFile(ThePlugin)
Dim type As Type = Asm.GetType("TestPlugin.Class_TestPlugin")
Dim method As MethodInfo = type.GetMethod("ChangeForms")
method.Invoke(Nothing, Nothing)
End Sub
End Class
TestPlugin.vb
Public Class Class_TestPlugin
Implements Plugin_Application.PluginHandler.IApplications
Sub ChangeForms() Implements Plugin_Application. _
PluginHandler.IApplications.ChangeForms
Dim NewForm As New Form_Test
NewForm.Show()
End Sub
End Class
My issue is it says for the method to invoke --> Non-static method requires a target.
I seen on another forum that the method may not be found. I found it says the methods name and void. But I'm not sure what to do. If somebody can mod my code to work or give me some ideas to make my code work. Thanks :)
Here is a link to my test project folder: Link
You have to create an object the method should be called on:
Dim ctor = type.GetConstructor({}) 'no parameters for constructor
Dim obj = ctor.Invoke({})
method.Invoke(obj, {})

Using a custom DLL in Silverlight

I'm testing and trying to understand how to create a DLL and call a function from it in my Silverlight project. I'm getting an exception: Unable to find an entry point named 'WriteTextFile' in DLL 'C:\temp\TestDLL.dll'. So I'm doing something wrong.
Here is my code for the very simple DLL:
Imports System.IO
Public Class Class1
Private Shared dir As String = "C:\TEMP"
Private Shared file As String = "TestDLL.txt"
Public Shared Sub WriteTextFile()
Using wr As New StreamWriter(System.IO.Path.Combine(dir, file))
wr.WriteLine("Call to function WriteTextFile()")
End Using
End Sub
End Class
And this is what I'm doing in my Silverlight:
Imports System.Runtime.InteropServices
Partial Public Class MainPage
Inherits UserControl
<DllImport("C:\temp\TestDLL.dll")> _
<AllowReversePInvokeCalls()> _
Friend Shared Sub WriteTextFile()
End Sub
Public Sub New()
InitializeComponent()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
WriteTextFile()
End Sub
End Class
I need some pointing to the right direction here :)
The AllowReversePInvokeCalls part is just something I tried out after doing some reading around the web but it didn't change anything. Also I've tried to set the EntryPoint in the DLLImport but that doesn't do anything either.
Edit:
I tried this DLL and pinvoke in regular WPF application and I get the same error so this is not related to Silverligth. Maybe in my DLL then?
Ok, so it looks like this can't be done simply because using pinvoke requires a standard dll and I can't do those with VB .NET. So I'll need to try something else :)
If anyone has more information about this and like to share it I would appreciate that. Or correct me if I've understood the issue wrong.

Calling a procedure within another 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