473,805 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(compar e)
theArray(compar e) = theArray(compar e + 1)
theArray(compar e + 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 1447
fripper <fr*****@insigh tbb.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.co m>
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
1527
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 the base class, each MUST have a property, but one defines it as OracleType, one as SqlType and one as DB2Type, for example. I know i can just declare it as Object, and then have the classes each do typechecks on value assignment, but this is...
7
239
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 access directly without setting up includes or controls. I know that you can put global variable ( for application and sessions) and code to manipulate them in the global.asax file, but what about functions? Thanks,
7
3005
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
4652
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 to a specific XSD that is defined. Right now when I declare XmlDocument as my argument, it puts the old xml:any type in. How do I change that to reflect the XSD that I'm looking for? Thanks for any Help!
1
280
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 have two arrays . 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...
12
2085
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! Thanks!
4
1263
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 several routines that are identical, except for the object type and database action specific to that particular class, such as "Public ReadOnly Property Item(ByVal SystemID As Integer) As CEmployee" or "Public Shared Function Save(ByVal TheItem As...
0
1422
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 do it. I do this by having one main subroutine, called by the user, which opens and prepares all database connections and other necessary public variables, and calls a second sub with a folder object containing the selected outlook folder as a call...
10
2916
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 normal #include and #virtual don't seem to be working remotely. I have also tried:
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.