Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating class problem 2

Status
Not open for further replies.

ecucurella

Programmer
Feb 17, 2003
34
0
0
ES
Hi all,
I am creating ny own class and I need the same method with the same name but with diferent parameters.
Can I do it in visual Basic?
For example:

...
Public Sub PrintToLog(LineLog As LineLogType, Connection As ConnectionType)
...
Public Sub PrintToLog(LineLog As LineLogType)
...
Public Sub PrintToLog()
...


Thank you in advance,
 
From what I understand VB5/6 won't allow you to do overloading, which is what you are trying to do. (right?) Although VB.Net will.
 
For the example you give, consider the use of Optional parameters. Check them out in VB Help.
 
Optional parameters will default to their initialized value, string to vbnullstring, numeric to 0, boolean to false, etc. You can override that by assigning a value if the parameter is not present. Here is an example:

Sub OptionalParms(Optional lng1 As Long, _
Optional lng2 As Long = 35, _
Optional str1 As String, _
Optional str2 As String = "Some Value", _
Optional bln1 As Boolean, _
Optional bln2 As Boolean = True)
End Sub

Sub TestIt
Call OptionalParms()
Call OptionalParms(5, 16, "Str1", "Str2", True, False)
End Sub

Run TestIt in single step mode and see how they work.

Good Luck!

Have a great day!

j2consulting@yahoo.com
 
Make the parameters all variants, and the only thing you need to check is IsMissing...
 
strongm,

I may be wrong but why would you use isMissing()? what if the var was initialised?

ex


public sub mysub(optional myVar as Variant = vbnullString)
select case myVar
case vbNullString
case else
end select
end sub


regards,
nicsin
 
Why use a variant if you're initialising to a string? In fact, why use a variant at all if you don't have to?

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"A computer program does what you tell it to do, not what you want it to do." -- Greer's Third Law
 
Sorry, previous post was missing a nicsin at the start, and a vbCrLf or two.

<Slaps wrist>

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
Andy,

I suppose strongm suggested variant so that the sub will be more flexible, ie work with integers, strings etc. The initialisation to a string was arbitrate. so = vbNullString and case vbNullString[/blue] would be the same as = 0 and case 0[/blue]. It is just a reference for a useless parametre.
 
>a useless parametre

Hence IsMissing; we don't have to pass parameters we are not interested in at all, no default values, no initialisation...
 
nicsin

> public sub mysub(optional myVar as Variant = vbnullString)

This is what I was taking exception to. Why bother creating a variant when you're going to make it into string? Seems utterly pointless to me.

Mind you, I'm an obsessive Type declarer.

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
Ok I get it. so it is cheaper to call isMissing than initialise the variable, am I right?

sorry for the trouble...

regards,
nicsin
 
nicsin

I'm not so sure about that, but it just seems logically pointless to me to create one thing only to turn it into another immediately afterward. One doesn't normally buy a blue car just to spray it red when one could've bought a red car.

Maybe strongm will set us right...

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
I think the reason Strongm is using variants is that IsMissing doesn't work with &quot;simple&quot; data types.

mmilan

Ps. Bally genious that Strongm is... I remember him from nearly two years back when I was posting to this forum...
 
sorry AndyWatt I hadn't seen you last posting. i agree with you on that and personaly I have never used the variant type although I believe there is a very good reason for its existence . So in my program I would either write public sub mysub(optional myVar as String = vbnullString) or public sub mysub(optional myVar as Integer = 0) and so on. Hope that clears my view.
 
nicsin

Sure does. [smile]

I think I've used Variant once so I could pass a Control Array.

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
One possible use of a variant for a string parameter would be to differentiate between a passed null string and and parameter not being passed.

I ran into this situation a while ago where sometimes the current string value needed to be cleared by passing it vbNullString. An optional string parameter will automatically initialize to vbNullString so how do you determine which case you are dealing with? I made it an optional variant and checked IsMissing.

I always enjoy the discussion on these forums!

Have a great day!

j2consulting@yahoo.com
 
Here's a simple example to illustrate what I'm getting at:

Option Explicit

Private Sub Command1_Click()
Debug.Print MultiThing(22 / 7); &quot;: &quot;; TypeName(MultiThing(22 / 7))
Debug.Print MultiThing(1, 2, &quot;spoon&quot;); &quot;: &quot;; TypeName(MultiThing(1, 2, 3))
Debug.Print MultiThing(4, , 1); &quot;: &quot;; TypeName(MultiThing(1, 2, 3))
Debug.Print MultiThing(&quot;Fred plus &quot;, &quot;Jim&quot;); &quot;: &quot;; TypeName(MultiThing(&quot;Fred&quot;, &quot;Jim&quot;))
End Sub


' Overloaded function
' pass a: return a
' pass a and b: return a + b (+ operator is already overloaded by VB to deal with strings and numerics)
' pass a and c: return a-c
' pass a, b and c: concatenate strings a, b and c

' In reality, once we've used IsMissing to determine which particular 'function' we want to call, we might use TypeName or varType function
' to further refine what we do (and, of course, to trap errors)
Private Function MultiThing(Optional a As Variant, Optional b As Variant, Optional c As Variant) As Variant
If IsMissing(b) And IsMissing(c) Then
MultiThing = a ' just a simple thing
ElseIf IsMissing(c) Then
MultiThing = a + b ' just a simple thing
ElseIf IsMissing(b) Then
MultiThing = a - c ' just a simple thing
Else
MultiThing = a & b & c ' just a simple thing
End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top