Results 1 to 6 of 6
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Looping Variables (VB6 or .Net)

    I have been asked to find out if its possible to loop all of the variables in a function, however, so far I haven't found anything to indicate its possible.

    A simplified example would be:

    Public function Test( var1 as string, var2 as string, var3 as string)
    For each var in variables
    Msgbox Var
    next var
    end function

    Im a bit short on proper details so I only have the concept to go on at the moment. Just wondered if that is possible?

    Regards,

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 Posts

    Re: Looping Variables (VB6 or .Net)

    As far as I know, that is not possible, but you could define the arguments as a ParamArray:

    Function Test(ParamArray args())
    Dim i As Integer
    For i = LBound(args) To UBound(args)
    MsgBox args(i)
    Next i
    End Function

    You can specify any number of arguments when calling the procedure or function

    Call Test(3)

    Call Test("Phil", 5, "Jeary")

  3. #3
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Looping Variables (VB6 or .Net)

    From the example that I was given that answers it perfectly thanks Hans.

    I've since found out the full story and I think the guy is trying to be overdynamic.

    Its something that goes in between a website and a database (not exactly sure what its doing, but that doesnt matter for this explanation). He has an ever growing list of functions that have a number of variables passed to them. All of the functions need to compile their variables into an array and pass it to another function. My colleague was hoping to be able to loop the variables into an array rather than do the following for each function:

    dim arr() as string
    arr(1) = var1
    arr(2) =var2
    arr(3)= var 3
    etc

    I suggested making the initial functions each use an array instead of seperate variables and his reply was that programmers doing development on it wouldnt be able to see what variables were supposed to be being passed i.e. public function test(firstname as string, surname as string, etc as string)

    He's going to write each variable to an array manually rather than loop it - he just thought he'd check it wasnt possible first. Thanks again.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Looping Variables (VB6 or .Net)

    You know, that doesn't make much sense. Once the variables are passed as an array, his desired programmer interface goes up in smoke. <img src=/S/confused.gif border=0 alt=confused width=15 height=20>
    Charlotte

  5. #5
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Looping Variables (VB6 or .Net)

    I think the original functions with all of the variables do a few other things as well as put them in an array and pass it to another function.
    It took me ages just to get the details above out of him, im not entirely sure he has put any planning into whatever he's doing!
    I've let him know that his original plan isn't possible and that it might be an idea to at least make a flow chart to map where data is going.

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    5,942
    Thanks
    0
    Thanked 94 Times in 90 Posts

    Re: Looping Variables (VB6 or .Net)

    He could try passing an object or UDT if that makes sense for what he is doing? Then his code can do whatever it needs to before passing the object/UDT on to the next part of the program.
    Regards,
    Rory
    Microsoft MVP - Excel.

Posting Permissions

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