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,
 
Thank you for all your answers.
Finally I do it with optional parameters asigning them a default value.
:)
 
>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.

There are many little "tricks" in doing this (In VB 5&6 at least).

A string or object argument can still be passed as a string or object, instead of a variant, and then checked to see if it was initialized at all.

You could also just optionally pass
actual Dates and Numbers ByVal as strings,
and strings ByVal or ByRef,
and Objects as Objects ByVal or ByRef,
except arrays and UDT, which would be passed as Variants
and still test if any of these were passed at all.

Then you can test if the string/object was passed by checking if the arguments were initialized at all...!
 
Hello CCLINT,

I've done more VBA than VB so if I am missing something here, please let me know. Let's say I have a parm defined as Optional str As String and an empty string is a valid value in some cases. How do I differentiate between an empty string being passed in as an argument and the parm being initialized to an empty string by the code if it was not passed as an argument.

Here is what I typically do in a case as above:

Sub OptTest(Optional pvar As Variant)
If Not IsMissing(pvar) Then
'Do something
End If
End Sub

Is there a better way that allows me to maintain my desired variable typing?

Thanks!

Have a great day!

j2consulting@yahoo.com
 
SBendBuckeye,

you can always write:


Sub OptTest(Optional pvar As String = &quot;<aValueThatWillNeverBePassed>&quot;)
If pvar <> &quot;<aValueThatWillNeverBePassed>&quot; Then
'Do something
End If
End Sub


but again I guess it is the same thing... :)

regards,
nicsin
 
Sub TestA()
Dim MyString As String
MyString = &quot;&quot;
Call TestXA(MyString)
End Sub
Sub TestA2()
Dim MyString As String
'Do not assign MyString a value
Call TestXA(MyString)
End Sub

Public Sub TestXA(ByRef MyStringArg As String)
If StrPtr(MyStringArg) Then
Debug.Print &quot;Initialized (something - even possibly &quot;&quot; - was passed - but NOT a vbNullString).&quot;
Else
Debug.Print &quot;Not Initialized&quot;
End If
End Sub

This is effective when a passed variable was initialized or not.
BUT, if the variable hasn't been initialized, and still passed, it will also be caught as &quot;'Not Initialized&quot;.
This is only a drawback if you are passing ByRef and want the unintialized variable to be set to a value with-in the proceedure, and the new value it was set to is to be also &quot;seen&quot; by the caller as in the following case:

Sub TestB()
Dim MyString As String
'Do not assign MyString a value
Call TestXB(MyString)

'Because the variable was passed ByRef, if it's value is changed in the proceedure, the calling proceedure will see the change
Debug.Print MyStringArg
End Sub
Public Sub TestXB(ByRef MyStringArg As String)
MyStringArg = &quot;Coming back initialized&quot;
End Sub
 
Thanks, CCLINT, I learned something new already today! That was a very helpful explanation.

I don't have access to VB at home, can I infer that there are a whole series of ptr functions (eg LngPtr, DblPtr which are similar to StrPtr)?


Have a great day!

j2consulting@yahoo.com
 
No. Just VarPtr() StrPtr() and ObjPtr()
Therefore, in my previous post, I said to pass the numeric/date variables ByVal as a String:

Sub TestC()
Dim dNumber As Double
'Number var already initialized
Call TestNumVar (dNumber)
Call TestNumVar (&quot;ABC&quot;)
Call TestNumVar
End Sub

Public Sub TestNumVar(Optional ByVal SomeNumber As String)
If IsNumeric(SomeNumber) Then
Debug.Print &quot;It's a number&quot;
ElseIf StrPtr(SomeNumber) Then
Debug.Print &quot;Something was passed and it's not a number - raise an error&quot;
Else
Debug.Print &quot;Nothing Passed at all&quot;
End If
End Sub
 
Thanks again CCLINT. Since both IsMissing and StrPtr require a function call, is this basically a 6 of one, half a dozen of another personal style situation or is one clearly the preferred method in VB?


Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top