Solved

Global Subs and Functions VB.NET

Posted on 2014-03-21
5
311 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
LOL - I responded too fast.  I'll give that a try.
0
 
LVL 1

Author Comment

by:slightlyoff
Comment Utility
Perfect!  Thank you so much :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now