Beginner looking for suggestions.
I began tryng to learn vb I found I needed a toolbox of utility functions to
take care of repetitive tasks without repeating code ad infinitum,
and to simplity the code in the project at hand.
such as stringfuncs filefuncs sortfuncs etc.
initially i started them in std modules, stringfuncs.bas etc
Then i started learning about classes so I put them in classes,
stringfuncs.cls etc
Now I realize i'm not really using these classes correctly.
I thought of them as repositories of reusable code, encapsulating the
implementation of the funciton and simplifying the code in the main
application.
But i see that it's awkward to have to instantiate a class just to use a
simple function or sub.
Also I see that for instance my FileFuncs.cls references SortFuncs.cls so
they're not free standing.
If I include the filefuncs.cls in a project I have to remember to put the
sortfunc class in also even if im not explicitly using it.
if a filefuncs.cls can return a list of files in a folder
I personally always want to see them sorted alphabetically so i include that
in the method.
That requires use of a sorting func which lives in the sortfuncs.cls
I could copy that one function and put it in the filefuncs class but then
I'm back to multiple copies of a function and lose the theory of code reuse.
since i'm a beginnner i don't really know the best way to go about handling
the above general situation
any suggestions welcome.
Thanks
Mark
Jeff Johnson [MVP: VB] - 28 Nov 2004 23:33 GMT
> But i see that it's awkward to have to instantiate a class just to use a
> simple function or sub.
Look at the GlobalMultiUse attribute for classes and consider putting all
the classes into a single DLL. Then you only have to reference that DLL from
each project where you need at least one of those functions.
MP - 30 Nov 2004 15:47 GMT
> > But i see that it's awkward to have to instantiate a class just to use a
> > simple function or sub.
>
> Look at the GlobalMultiUse attribute for classes and consider putting all
> the classes into a single DLL. Then you only have to reference that DLL from
> each project where you need at least one of those functions.
Thanks for the pointer Jeff,
I'm reading that in the msdn and trying to grasp it.
The difference between explicit creation and implicit creation of the
objects was throwing me at first but I think I'm getting the basic idea.
So, since this is the only suggestion, I assume this is the <standard / only
/ best> way people handle their 'toolbox' functions?
And the references to the small overhead on each function call is not a
significant aspect?
Thanks again,
Mark
Mike D Sutton - 29 Nov 2004 11:50 GMT
> Beginner looking for suggestions.
> I began tryng to learn vb I found I needed a toolbox of utility functions to
[quoted text clipped - 25 lines]
> the above general situation
> any suggestions welcome.
Classes should only be used when you're encapsulating some kind of data within the object, standalone methods should IMO be left in
standard code modules. For instance if you have a FileFuncs class then you may want a method on there which goes and gets all the
files in a directory and stores them internally, then a Sort method on the class which sorts that data (still inside the class) and
finally an accessor that provides indexed access to the list of files.
A module based solution on the other hand would need one method that returns an array of all the files in a folder, then a second
function to sort an array of strings (more generic than putting the same functionality right into the first method) if you want you
could always write a wrapper function which returns a sorted list of files by calling both functions and returning the result.
If the sorting code resides in a different module than file listing code and you don't want to keep the reference between them then
you may want to look at using conditional compile arguments (Project -> Properties -> Make -> Conditional Compilation Arguments),
something like:
'*** modFiles.bas
Public Function GetFiles(ByRef inFolder As String) As String()
...
End Function
#If (UseModSort) Then ' Include method to get sorted file list
Public Function GetSortedFiles(ByRef inFolder As String) As String()
Dim RetArr() As String
RetArr() = GetFiles(inFolder)
Call SortStringArr(RetArr)
GetSortedFiles = RetArr
End Function
#End If
'***
'*** modSort.bas
Public Function SortStringArr(ByRef inArr() As String) As Boolean
...
End Function
'***
When you include the modSort module in your code then define the UseModSort conditional compile argument (UseModSort=1) and you'll
get access to the additional function in modFiles.
Using the class based approach you can do the same thing, keep the functionality in the module but add it to the class when it's
available:
'*** clsFiles.cls
Private m_Files() As String
Private m_NumFiles As Long
Public Property Get FileName(ByVal inIdx As Long) As String
If ((inIdx >= 0) And (inIdx < m_NumFiles)) Then _
FileName = m_Files(inIdx) ' Return file
End Property
Public Function GetFiles(ByRef inPath As String) As Long
... Populate m_Files() and m_NumFiles here..
End Function
#If (UseModSort) Then
Public Sub Sort() ' Include sort functionality where available
Call SortStringArr(m_Files())
End Sub
#End If
'***
Hope this helps,
Mike
- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/