Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • 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?


0
codequest
Asked:
codequest
  • 2
2 Solutions
 
Brian CroweCommented:
create a class with shared methods as your utility class.  That way you don't have to instantiate it.
0
 
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?
 
0
 
GlomCommented:
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

Bye
0
 
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.

Grazie!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

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