PC Review


Reply
Thread Tools Rate Thread

Creating a static function

 
 
=?Utf-8?B?QnJpdGU=?=
Guest
Posts: n/a
 
      28th Mar 2007
I'm not sure if i'm doing something that doesn't work, but why can't i seem
to get a static function to work the same as a static sub.

If i run the following code a few times, and i always use the number 1 as x,
i would think that the function should retain the previous value of "test",
and then just increment it by 1, but this isn't happening. "Test" always
returns as 1. I want this to work as a counter that other subs will pull from.

Static Function test(x As Integer) As Integer
test = test + x
End Function


thanks
 
Reply With Quote
 
 
 
 
Tim Williams
Guest
Posts: n/a
 
      28th Mar 2007
There's a good example in the VBA Help.
Static applies to variables, not to Functions.

--
Tim Williams
Palo Alto, CA


"Brite" <(E-Mail Removed)> wrote in message news:F50A3D38-8DED-453F-BBFB-(E-Mail Removed)...
> I'm not sure if i'm doing something that doesn't work, but why can't i seem
> to get a static function to work the same as a static sub.
>
> If i run the following code a few times, and i always use the number 1 as x,
> i would think that the function should retain the previous value of "test",
> and then just increment it by 1, but this isn't happening. "Test" always
> returns as 1. I want this to work as a counter that other subs will pull from.
>
> Static Function test(x As Integer) As Integer
> test = test + x
> End Function
>
>
> thanks



 
Reply With Quote
 
=?Utf-8?B?SmF5?=
Guest
Posts: n/a
 
      28th Mar 2007
Hi Brite -

A preserved variable can't have the same name as the function. In other
words, all variables are preserved in a Static function (as you noted) except
for the variable bearing the function's name.

Try it this way:
Static Function test(x As Integer) As Integer
test = stat + x
stat = test
End Function

--
Jay


"Brite" wrote:

> I'm not sure if i'm doing something that doesn't work, but why can't i seem
> to get a static function to work the same as a static sub.
>
> If i run the following code a few times, and i always use the number 1 as x,
> i would think that the function should retain the previous value of "test",
> and then just increment it by 1, but this isn't happening. "Test" always
> returns as 1. I want this to work as a counter that other subs will pull from.
>
> Static Function test(x As Integer) As Integer
> test = test + x
> End Function
>
>
> thanks

 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      28th Mar 2007
My mistake - does also apply to procedure declaration

Tim

"Tim Williams" <timjwilliams at gmail dot com> wrote in message
news:(E-Mail Removed)...
> There's a good example in the VBA Help.
> Static applies to variables, not to Functions.
>
> --
> Tim Williams
> Palo Alto, CA
>
>
> "Brite" <(E-Mail Removed)> wrote in message
> news:F50A3D38-8DED-453F-BBFB-(E-Mail Removed)...
>> I'm not sure if i'm doing something that doesn't work, but why can't i
>> seem
>> to get a static function to work the same as a static sub.
>>
>> If i run the following code a few times, and i always use the number 1 as
>> x,
>> i would think that the function should retain the previous value of
>> "test",
>> and then just increment it by 1, but this isn't happening. "Test" always
>> returns as 1. I want this to work as a counter that other subs will pull
>> from.
>>
>> Static Function test(x As Integer) As Integer
>> test = test + x
>> End Function
>>
>>
>> thanks

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a static class Tina Microsoft C# .NET 5 13th May 2006 09:35 AM
Creating a static Messages Viewer superseed@gmail.com Microsoft C# .NET 2 15th Nov 2005 02:32 PM
Is This Safe? Static Class Reference In Static Function Amy L. Microsoft C# .NET 3 23rd Feb 2005 05:10 PM
Add Event Handler Dynamically, but assigning function name at run-time, not to static function Jose Suero Microsoft ASP .NET 2 26th Dec 2003 08:40 PM
Static vs Non-Static Function Performance Steve - DND Microsoft C# .NET 22 12th Nov 2003 01:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:41 PM.