473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Subroutines with Varying Argument Classes


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.


Sep 14 '05 #1
1 1427
fripper <fr*****@insightbb.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eidolon | last post by:
I am looking to have a base class which provides that all inherited classes MUST have some property, but that they can each define it as whatever type they want. So maybe three class inherit from...
7
by: tshad | last post by:
Is there a way to set up global subs and functions? I just want to put some functions (such as bittest, bitclear, bitset - I know there are already bitarray functions) that all my screens can...
7
by: Microsoft | last post by:
I'm not sure where to physically place my subroutines in vb.net I get namespace and not declared errors... Imports System Imports System.Management Public Class Form1
4
by: MattBell | last post by:
I've tried to search for an answer to this without much success, and I think it's probably a common thing to do: I have a web service I want to accept an XmlDocument as an argument which conforms...
1
by: fripper | last post by:
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...
12
by: dima | last post by:
Hello All! Does anybody know anything about type-safe implementation of functions with varying number of arguments? I know one old solution - elipsis (stdarg,h). But it is NOT type-safe! ...
4
by: Confused Newbie | last post by:
I'm converting an app written in VB 2003 to 2005 and need advice for how to deal with this situation: The app has a number of "manager" classes that handle the data access. They all have...
0
by: BlackMustard | last post by:
Hi! I'm working on a macro for Microsoft Outlook that's supposed to export emails from a user-picked folder to an Access database, in a slightly more specific way than the standard wizard way to...
10
by: liz0001 | last post by:
Hi, I need to access an .asp subroutine file from a subdomain. i.e. the website is at www.domain.com and I want to access the same subroutines on the subdomain mobile.domain.com. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.