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

 Page 1 of 1 [ 5 post ]

Relevant Pages