Results 1 to 4 of 4

Thread: Function variable to be seperate for each call....

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Function variable to be seperate for each call....

    Hi,

    Lets say i have:

    Private Function TestFunction (byval VariableA as SomeClass)
    Static VariableB as integer

    VariableB = variableA.position
    Return Nothing
    End Function

    I then do:

    For i = 1 to 100
    TestFunction(InputA)
    Next

    So i used static so variableB still has value from previous loop.

    This is no problems, but if i want to do

    For i = 1 to 100
    TestFunction(InputA)
    TestFunction(InputB)
    TestFunction(InputC)
    Next

    It uses the same VariableB to get .position from InputA, InputB and InputC so at the end of the loop, it is VariableB = InputC.

    How can i make it use a seperate VariableB for each function call?

    So at end of loop,

    Function call 1 has VariableB = inputA.position
    Function call 2 has VariableB = inputB.position
    Function call 2 has VariableB = inputC.position

    So how can i make each TestFunctionB line seperate from the others?

    Um.. I am not sure if i make any sense but I am in a hurry right now. If it needs to be explained clearer please reply and I try make more sense.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Function variable to be seperate for each call....

    You can't have your cake and eat it too. If you have one function then you have one Static variable. A single variable can't have three different values. If you want multiple values then you need multiple variables, so you need multiple functions.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Function variable to be seperate for each call....

    Ok then,
    I guess I have to change my code.

    Thanks for the reply

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Function variable to be seperate for each call....

    Code:
    Public Function TestFunctionA (byval VariableA as SomeClass)
        Static VariableB as integer
        Return TestFunction(VariableA,  VariableB)
    End Function
    
    Public Function TestFunctionB (byval VariableA as SomeClass)
        Static VariableB as integer
        Return TestFunction(VariableA,  VariableB)
    End Function
    
    Public Function TestFunctionC (byval VariableA as SomeClass)
        Static VariableB as integer
        Return TestFunction(VariableA,  VariableB)
    End Function
    
    Private Function TestFunction (byval VariableA as SomeClass, ByRef VariableB as integer)
        'function body goes here
        VariableB = variableA.position
        Return Nothing
    End Function
    Code:
    For i = 1 to 100
    TestFunctionA(InputA)
    TestFunctionB(InputB)
    TestFunctionC(InputC)
    Next
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width