• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Code Organization: utility methods in a complex class

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?

  • 2
2 Solutions
Brian CroweDatabase AdministratorCommented:
create a class with shared methods as your utility class.  That way you don't have to instantiate it.
codequestAuthor Commented:
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?
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

codequestAuthor Commented:
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.

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now