Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

polymorphism sub

Posted on 1999-07-14
12
Medium Priority
?
171 Views
Last Modified: 2013-11-25
is it possible to create two identical subs with different arguments ? how ?
0
Comment
Question by:yaelt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 20 total points
ID: 1524887
No
(sorry)
0
 
LVL 1

Expert Comment

by:michelv
ID: 1524888
Well, maybe with ParamArray(), don't know exactly what you want...

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524889
Yes, it is possible to have one subroutine with optional parameters and parameter arrays, but that is different. What yaelt is asking is whether he can have several subroutines, all with the same name, but with different arguments:

Private Sub MyLittleSub(MyParameter As String)

...

Private Sub MyLittleSub(MyOtherParameter As Long, AndASecond As Boolean)

The answer to that is no, I'm afraid.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:ameba
ID: 1524890
see also  http://www.insteptech.com/VBOO.htm

' ---------------------------------------------------------------
'  The first step is to define the interface (set of properties and methods)
'  You do this by creating a class module with only the property and method
'    declarations (no code).
' ---------------------------------------------------------------
'  class Name = IMoreInfo
Option Explicit

Public Sub MyInfo(pNumber As Long, pSource As String, pDescription As String)
'
End Sub


' ---------------------------------------------------------------
'  The second step is to implement this interface in another class
' ---------------------------------------------------------------
'  class Name = CCustomer   (class that implements IMoreInfo)
Option Explicit
Implements IMoreInfo
Private m_Number As Long
Private m_Source As String
Private m_Description As String

Private Sub Class_Initialize()
    m_Source = "a source"
    m_Description = "a description"
End Sub

Public Sub MyInfo(pNumber As Long)
    m_Number = pNumber
    MsgBox m_Number, vbOKOnly, "Set info"
End Sub

Private Sub IMoreInfo_MyInfo(pNumber As Long, pSource As String, pDescription As String)
    m_Number = pNumber
    m_Source = pSource
    m_Description = pDescription
    MsgBox m_Number & vbCrLf & m_Source & vbCrLf & m_Description, vbOKOnly, "Set more info"
End Sub


' ---------------------------------------------------------
' test form
Option Explicit
Dim objCust As CCustomer

Private Sub Form_Click()
    Dim objMoreInfo As IMoreInfo

    Call objCust.MyInfo(5555)
   
    ' access the IMoreInfo interface for the CCustomer class
    Set objMoreInfo = objCust
    Call objMoreInfo.MyInfo(7777, "a source", "a description")
End Sub

Private Sub Form_Load()
    Set objCust = New CCustomer
End Sub

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524891
ameba, you're talking about VB's polymorphism. yaelt is talking about functional polymorphism which I believe is available in some other programming languages but not in VB.
0
 
LVL 15

Expert Comment

by:ameba
ID: 1524892
It can be a workaround for his
>two identical subs with different arguments
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524893
You mean by having two classes that implement the same interface and have the subroutines in question as "extra" routines?
0
 
LVL 15

Expert Comment

by:ameba
ID: 1524894
Hmm,
' original interface
Public Sub MyInfo(pNumber As Long)

For additional MyInfo() sub, he'll need a class (IMoreInfo),
a sub
Private Sub IMoreInfo_MyInfo(...)
and Implements statement.


Of course, he cannot have:
Private Sub MyInfo(arg1)
Private Sub MyInfo(arg1, arg2)
in the same class.
0
 

Expert Comment

by:zeuskrell
ID: 1524895
The best way to adapt a single Function or SUB to multiple uses is with the OPTIONAL keyword parameter passing and perhaps unspecified datatypes such as
"Variablename AS Variant".  
Clever use of these will allow for some very flexible calls to a single routine including calling a host of different subsequent SUB's from that one flexible SUB which would effectively perform in the same way as would be accomplished with numerous like named subs or polymorphing.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524896
Well spoken
0
 

Expert Comment

by:zeuskrell
ID: 1524897
Private Function POLYTEST(IDNUM as Variant, Optional FirstName = "John" , Optional LastName = "Doe") as Boolean

Perhaps a better Polymorhping-Like use for the Function/Sub call is to use the PARAMARRAY option which will allow you to specify like this

Private Function POLYTEST(PARAMARRAY TESTME()) as Boolean

Then the calling function would call the Polytest function like this

RetVal = PolyTest(Var1, Var2, Var3, Var4, VarX)  
and the function will automatically assign all entries however many are passed to the TESTME() Array.

You could use the VARTYPE function to determine the nature of each entries data type assuming it was a variant array of mixed items I'm assuming they should all return differing values..
0
 
LVL 15

Expert Comment

by:ameba
ID: 1524898
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question