Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

passing objects as parametes to subs or functions?

560 views
Skip to first unread message

James

unread,
Oct 15, 2008, 10:39:46 PM10/15/08
to
can I do this and if so, how?

I tried something like this.. just enough to show structure here
-------------------------------------
Class MyClass

Function DoSomething(oDataForMyClass)
'assume cmd is an ADODB.Command object
cmd.Parameters.Append oDataForMyClass.TestParameter 'I get a type
mismatch error here in the real code?
End Function

End Class


Class DataForMyClass

private pTestParameter

Private Sub Class_Initialize
Set pTestParameter = CreateObject("ADODB.Parameter")
pTestParameter.Type = adInt
End Sub

Property Let TestParameter(value)
pTestParameter.Value = value
End Property

Property Get TestParameter
TestParameter = pTestParameter 'shouldn't this return an
ADODB.Parameter object?
End Property

End Class
------------------------------------------

I get a type mismatch when I go to use what I thought should be an
ADODB.Parameter object. A propery 'get' is like a function right, it returns
something... can't it return an object? I need enlightenment on this
desperately!


Tim Williams

unread,
Oct 15, 2008, 11:24:35 PM10/15/08
to
http://www.mna.hkr.se/caspdoc/html/vbscript_set_statement.htm

And make sure somewhere you define adInt.

Tim

"James" <no...@nowhere.com> wrote in message
news:u90P1hzL...@TK2MSFTNGP05.phx.gbl...

James

unread,
Oct 16, 2008, 11:56:15 AM10/16/08
to
thank you Tim! I was aware of it when creating original variables but didn't
even think to do it when assigning the existing object to a new variable...
Thank you very much.

I have a side question for you that is somewhat related:

When an object is passed into a function or sub is it always passed byref?
In other words are objects (even your own classes) exceptions to the use of
byref or byval functionality in that they can only be passed byref and
therefore are?


"Tim Williams" <timjwilliams at gmail dot com> wrote in message
news:ervCw6zL...@TK2MSFTNGP02.phx.gbl...

ekkehard.horner

unread,
Oct 16, 2008, 2:49:12 PM10/16/08
to
James schrieb:
[...]

> I have a side question for you that is somewhat related:
>
> When an object is passed into a function or sub is it always passed byref?
> In other words are objects (even your own classes) exceptions to the use of
> byref or byval functionality in that they can only be passed byref and
> therefore are?
[...]
Per default, all VBScript variables are passed by reference (allowing
access to the variable (content) of the caller. This can be changed by
using the keyword "ByVal" in the sub/function definition.

Object variables hold a reference to the (data of the) object. So
passing an object per value still gives the called sub/function access
to the object('s data) - you can't prevent the callee from doing nasty
things to your object. But if you pass this reference per reference,
the called sub/function may even change the caller's variable('s content):

Class cPerson
Private m_sName
Private m_nAge
Private Sub Class_Initialize()
WScript.Echo "*", "Creating a cPerson object"
End Sub
Public Function init( sName, nAge )
m_sName = sName
m_nAge = nAge
Set init = Me
End Function
Public Default Property Get data()
data = m_sName & " (" & m_nAge & ")"
End Property
Public Sub birthday()
m_nAge = m_nAge + 1
End Sub
End Class ' cPerson

Sub birthday( ByVal oP0, ByVal oP1 )
oP0.birthday
oP1.birthday
End Sub

Sub swapPerVal( ByVal oP0, ByVal oP1 )
Dim oTmp
Set oTmp = oP0
Set oP0 = oP1
Set oP1 = oTmp
oP0.birthday
End Sub

Sub swapPerRef( oP0, oP1 )
Dim oTmp
Set oTmp = oP0
Set oP0 = oP1
Set oP1 = oTmp
oP0.birthday
End Sub

Dim oAdam : Set oAdam = New cPerson.init( "Adam", 20 )
Dim oEve : Set oEve = New cPerson.init( "Eve" , 20 )
WScript.Echo 0, oAdam, oEve
birthday oAdam, oEve
WScript.Echo 1, oAdam, oEve
swapPerVal oAdam, oEve
WScript.Echo 2, oAdam, oEve
swapPerRef oAdam, oEve
WScript.Echo 3, oAdam, oEve

output:

=== ObjRefVal: objects - reference/value ==
* Creating a cPerson object
* Creating a cPerson object
0 Adam (20) Eve (20)
1 Adam (21) Eve (21)
2 Adam (21) Eve (22)
3 Eve (23) Adam (21)
=== ObjRefVal: 0 done (00:00:00) ==========


"Sub swapPerVal( ByVal oP0, ByVal oP1 )" will change Eve via the
(in effect local) variable oP0, but to really swap oAdam and oEve you
need "Sub swapPerRef( oP0, oP1 )".

James

unread,
Oct 16, 2008, 5:17:13 PM10/16/08
to
THANK YOU, for a great, detailed explanation. I really appreciate it.

"ekkehard.horner" <ekkehar...@arcor.de> wrote in message
news:48f78ca9$0$28913$9b4e...@newsspool1.arcor-online.net...

0 new messages