Solved

Global Subs and Functions VB.NET

Posted on 2014-03-21
5
327 Views
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!!!
0
Comment
Question by:slightlyoff
  • 3
  • 2
5 Comments
 
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  = )
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 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.

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
 
LVL 1

Author Comment

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

Author Comment

by:slightlyoff
ID: 39945226
LOL - I responded too fast.  I'll give that a try.
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 39945231
Perfect!  Thank you so much :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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