![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | passing objects as parametes to subs or functions? 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! |
My System Specs![]() |
| | #2 (permalink) |
| | Re: passing objects as parametes to subs or functions? http://www.mna.hkr.se/caspdoc/html/v..._statement.htm And make sure somewhere you define adInt. Tim "James" <noone@xxxxxx> wrote in message news:u90P1hzLJHA.2348@xxxxxx Quote: > 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! > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: passing objects as parametes to subs or functions? 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:ervCw6zLJHA.1308@xxxxxx Quote: > http://www.mna.hkr.se/caspdoc/html/v..._statement.htm > > And make sure somewhere you define adInt. > > Tim > > "James" <noone@xxxxxx> wrote in message > news:u90P1hzLJHA.2348@xxxxxx Quote: >> 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! >> > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: passing objects as parametes to subs or functions? James schrieb: [...] Quote: > 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 )". |
My System Specs![]() |
| | #5 (permalink) |
| | Re: passing objects as parametes to subs or functions? THANK YOU, for a great, detailed explanation. I really appreciate it. "ekkehard.horner" <ekkehard.horner@xxxxxx> wrote in message news:48f78ca9$0$28913$9b4e6d93@xxxxxx-online.net... Quote: > James schrieb: > [...] Quote: >> 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 )". |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Functions and Objects | PowerShell | |||
| Calling external Subs and Functions | VB Script | |||
| passing whole objects | PowerShell | |||
| Objects returned from functions?? | PowerShell | |||
| Creating objects with variable names in functions | PowerShell | |||