Global Subs and Functions VB.NET

Posted on 2014-03-21
Medium Priority
Last Modified: 2014-03-21
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!!!
Question by:slightlyoff
  • 3
  • 2
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39945217
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  = )
LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 39945222
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.


Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    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.

Author Comment

ID: 39945224
Thanks for your help!  I guess I'll just get used to typing it out then.  Thank you!

Author Comment

ID: 39945226
LOL - I responded too fast.  I'll give that a try.

Author Comment

ID: 39945231
Perfect!  Thank you so much :)

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…
Did you know PowerShell can save you time with SaaS platforms? Simply leverage RESTfulAPIs to build your own PowerShell modules. These will kill repetitive tickets and tabs, using the command Invoke-RestMethod. Tune into this webinar to learn how…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question