Code Organization:  utility methods in a complex class

Posted on 2006-04-13
Last Modified: 2012-05-05
I've got a complex class object that has "primary" methods and "utility" methods.

I'd like to separate out the utility methods so I don't see them in the VS2005 drop down of methods for the class.

I don't want to have to instantiate a separate class to get at the utility methods, and the utility methods need to access a set of private variables for the class.

I was thinking of doing a sub class "MU" that would inherit the main class, and calling the utilities from that, as in:

Me.MU.Util1   or   MyClass.MU.Util1

instead of

Me.Util1    or   MyClass.Util1

But that doesn't seem to work.

Any ideas on how to accomplish the result I'm looking for?    Or reasons why it's not possible?

Question by:codequest
    LVL 34

    Assisted Solution

    by:Brian Crowe
    create a class with shared methods as your utility class.  That way you don't have to instantiate it.
    LVL 2

    Author Comment

    Thanks for the input.  I've done that in other instances.  Can that class inherit from the "main" class, in order to get at the private variables?
    LVL 4

    Accepted Solution

    No, it can't, because shared Methods can only access shared attributes and variables.
    You can then declare your variables as Friend and pass the instance of the class containing these variables to the shared methods of your Utility class.

    Your Utility class :

    Public NotInheritable Class Utility

    ' To force the use as a utility class
    Private Sub New
    End Sub

    Public Shared Sub Test(instance as primaryClass)
        ' myVariable declared as Friend in the PrimaryClass
        instance.myVariable = 4
    End Sub

    End Class

    LVL 2

    Author Comment

    Thanks for input.   I can see there are tradeoffs here.  

    One thing I did do was rename the methods to start with "Ut_" so that they sort to the bottom of the methods list.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    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…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now