?
Solved

Global Subs and Functions VB.NET

Posted on 2014-03-21
5
Medium Priority
?
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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.

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

743 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