controlling scope 
Author Message
 controlling scope

I have a subroutine (for example) ...

        sub foo(stuff)

        a = left (stuff, 2)
        b = right (stuff, 2)

        end sub

How do I limit the scope of a and b to the sub?

How do I get the foo() to return a value such as an array containing a
and b? eg ...

        x = sub foo(instuff)

Is a function what I really want?

Thanks,
Scott



Sat, 14 Dec 2002 03:00:00 GMT  
 controlling scope
function foo(stuff)

Dim a,b ' these have local scope...

a = left (stuff, 2)
b = right (stuff, 2)

foo = "whatever you want to return...:

end function

--
Michael Harris
MVP Scripting


I have a subroutine (for example) ...

sub foo(stuff)

a = left (stuff, 2)
b = right (stuff, 2)

end sub

How do I limit the scope of a and b to the sub?

How do I get the foo() to return a value such as an array containing a
and b? eg ...

x = sub foo(instuff)

Is a function what I really want?

Thanks,
Scott



Sat, 14 Dec 2002 03:00:00 GMT  
 controlling scope

Quote:

> function foo(stuff)

> Dim a,b ' these have local scope...

So it's automagical just by declaring within the function, guess I had
just assumed a more complicated proceedure.

Quote:
> a = left (stuff, 2)
> b = right (stuff, 2)

> foo = "whatever you want to return...:

> end function

Yes! Function is what I needed after all. Thanks Michael.
Quote:
> --
> Michael Harris
> MVP Scripting



> I have a subroutine (for example) ...

> sub foo(stuff)

> a = left (stuff, 2)
> b = right (stuff, 2)

> end sub

> How do I limit the scope of a and b to the sub?

> How do I get the foo() to return a value such as an array containing a
> and b? eg ...

> x = sub foo(instuff)

> Is a function what I really want?

> Thanks,
> Scott



Sat, 14 Dec 2002 03:00:00 GMT  
 controlling scope
I have a function (example) ...

--
stuff = "whatever"
new_stuff = foo (stuff)

function foo (tmp_stuff)

dim a, b             ' scope of a, b limited to the function

a = left (stuff, 2)
b = right (stuff, 2)

foo = a & b
end function
--

But tmp_stuff is not local to foo, and in fact persisted in a very
unexpected way. Generally not a big deal unless successive foo()'s are
made from within another function. Is there a way to make tmp_stuff
local? Or should I always plan on a set of temporaries to use and
destroy on every call to function x?

Thanks,
Scott

Quote:

> function foo(stuff)

> Dim a,b ' these have local scope...

> a = left (stuff, 2)
> b = right (stuff, 2)

> foo = "whatever you want to return...:

> end function

> --
> Michael Harris
> MVP Scripting



> I have a subroutine (for example) ...

> sub foo(stuff)

> a = left (stuff, 2)
> b = right (stuff, 2)

> end sub

> How do I limit the scope of a and b to the sub?

> How do I get the foo() to return a value such as an array containing a
> and b? eg ...

> x = sub foo(instuff)

> Is a function what I really want?

> Thanks,
> Scott



Mon, 16 Dec 2002 03:00:00 GMT  
 controlling scope
You never used tmp_stuff inside foo().  As posted, you just used the global stuff variable.

In any case, tmp_stuff (as a parameter in the function statement's parameter list) is just a
reference to whatever the function was with.  By default, VBScript passes by reference (ByRef) just
like VB/VBA.  That means whatever change you make to tmp_stuff inside foo() is actually changing
whatever variable was passed (stuff in this case).  To prevent changes to tmp_stuff from affecting
the variable passed, you pass by value (ByVal).

An example,

stuff = "foo"
msgbox foo(stuff) & " " & stuff

stuff = "bar"
msgbox bar(stuff) & " " & stuff

function foo(tmp_stuff)

  tmp_stuff = ucase(tmp_stuff)
  foo = tmp_stuff

end function

function bar(byval tmp_stuff)

  tmp_stuff = ucase(tmp_stuff)
  bar = tmp_stuff

end function

--
Michael Harris
MVP Scripting


I have a function (example) ...

--
stuff = "whatever"
new_stuff = foo (stuff)

function foo (tmp_stuff)

dim a, b             ' scope of a, b limited to the function

a = left (stuff, 2)
b = right (stuff, 2)

foo = a & b
end function
--

But tmp_stuff is not local to foo, and in fact persisted in a very
unexpected way. Generally not a big deal unless successive foo()'s are
made from within another function. Is there a way to make tmp_stuff
local? Or should I always plan on a set of temporaries to use and
destroy on every call to function x?

Thanks,
Scott



Mon, 16 Dec 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Controlling scope of VBA commands

2. Form Level Control Scope

3. Data Control Scope

4. CONTROLs Scope Question

5. Custom control - scope/results pane similar to Outlook

6. MsScript Control and Scope variables

7. Demos for gpib control of a HP-scopes

8. Scope Problem in ActiveX Control

9. Scope problem with Access2K and multiple forms

10. scope violation

11. Scope of database and workspace objects?

12. Tabledef object variable lost out of scope

 

 
Powered by phpBB® Forum Software