Subroutines with Varying Argument Classes

F

fripper

I have had some experience with VB .Net but I am having difficulty coming
to grips with how to set up a subroutine to accept varying classes for its
arguments. An example will help to explain. I have two arrays [call them A
and B]. The elements of array A are records whose fields are defined with a
Public Structure block (call it RecTypeA) and the elements of array B are
records defined by a different Structure block (RecTypeB). Now, each
structure has a field named Dater ... which is a date. At one point in the
program I want to sort the elements of A by the Dater field and at another
point I want to sort array B by it's Dater field. I would like to do this
with a single sort subroutine. My difficulty has to do with how to
structure the sort routine such that it will accept either a RecTypeA array
or a RecTypeB array. Here, for example, is a simple sort routine (DateSort)
that will sort array A (of type RecTypeA) but how can I set things up so
that DateSort will work with either type RecTypeA or RecTypeB as the
argument. Is there some way to pass to the sort routine the class of its
arguments so that it can dynamically handle both?

Public Sub DateSort(ByRef theArray() As RecTypeA, ByVal SizeAs Short)
Dim pass, compare As Short
Dim hold As RecTypeA
Dim date1 As Date
Dim date2 As Date
Dim val1 As String
Dim val2 As String

For pass = 1 To (Size - 1) ' Sort date (descending
order)
For compare = 1 To (Size - 1)
date1 = CDate(theArray(compare).Dater)
date2 = CDate(theArray(compare + 1).Dater)
If date1 < date2 Then
hold = theArray(compare)
theArray(compare) = theArray(compare + 1)
theArray(compare + 1) = hold
End If
Next compare
Next pass
End Sub


I hope I have made myself clear here. Thanks for whatever advice (or
reference) you can provide.
 
J

Jon Skeet [C# MVP]

fripper said:
I have had some experience with VB .Net but I am having difficulty coming
to grips with how to set up a subroutine to accept varying classes for its
arguments. An example will help to explain. I have two arrays [call them A
and B]. The elements of array A are records whose fields are defined with a
Public Structure block (call it RecTypeA) and the elements of array B are
records defined by a different Structure block (RecTypeB). Now, each
structure has a field named Dater ... which is a date. At one point in the
program I want to sort the elements of A by the Dater field and at another
point I want to sort array B by it's Dater field. I would like to do this
with a single sort subroutine.

Make them both implement an interface which has a Dater property (not
field), and have that property in both of your other types. Then you
can just refer to the interface instead of the specific implementation.
However, this works a lot more smoothly (in terms of arrays) if your
types are classes, rather than structures.

By the way, there's no need to pass your array by reference in the
method that you've shown - arrays are reference types anyway. See
http://www.pobox.com/~skeet/csharp/parameters.html for more information
on passing by value or by reference - it's in C#, but the same applies
to VB.NET.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top