• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

Global Subs and Functions VB.NET

I have a couple of functions I would like to be available across my classes and forms.
Error Logging, Progress Logging, Send Mail and a Message Box helper for debugging.

I know I can define these on my main form called "Main" and then call them in a class as Main.SubName() - but being a lazy bugger, I want to be able to just call them as SubName() without the main form name.

I created a new class here's my code:
Public Class myHelpers
   
 'Message Box Helper - for debugging 
    Public Shared Sub mbHelper(ByVal lineNumber As Integer, ByVal Task As String, ByVal message As String, ByVal additionalInfo As String)
        Dim title As String = "Debug Information"
        Dim messageText As String = "Line Number: " & lineNumber & vbCrLf & "Task: " & Task & vbCrLf & "Message: " & message & vbCrLf & "Additional Info: " & additionalInfo
        MsgBox(messageText, MsgBoxStyle.OkOnly, title)

    End Sub

End Class

Open in new window


In my forms and classes I imported myHelpers - but that did not work, mbHelper is not recognized.

Any ideas what I'm missing?  Sorry if this is a simple problem - but I really appreciate the help!!!
0
slightlyoff
Asked:
slightlyoff
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
In .NET there is no such concept as "global functions"--static methods are the closest equivalent. Even though the old VB6-style functions like Mid, InStr, and MsgBox are available in .NET, really they are just syntactic sugar, and their usages gets mapped into actual class/methods (Strings.Mid, Strings.InStr, Interaction.MsgBox). I'm afraid you won't get to be *that* lazy in .NET  = )
0
 
käµfm³d 👽Commented:
Actually, I focused too much on the .NET and not enough on the VB. I forgot that you can put your functions into a module, and they should be readily available. Just make sure they are marked Public.

e.g.

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DoSomething()
    End Sub
End Class

Module Module1
    Public Sub DoSomething()

    End Sub
End Module

Open in new window


Even doing this, under the hood your module gets turned into a NotInheritable class. So functionally you are not gaining anything, but syntactically you will achieve your goal.
0
 
slightlyoffAuthor Commented:
Thanks for your help!  I guess I'll just get used to typing it out then.  Thank you!
0
 
slightlyoffAuthor Commented:
LOL - I responded too fast.  I'll give that a try.
0
 
slightlyoffAuthor Commented:
Perfect!  Thank you so much :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now