Results 1 to 4 of 4
  1. #1

    Challenge to All the Programmers of this forum

    Hi All ,

    Here comes a task that made many into a baffled status ...

    here is the question :

    1) creating dynamic arrays like a1(0) a1(1)
    a1(2).....followed by a2(0) a2(1) ...... till say a10(2)

    2) each one should hold any values may be a same value etc.........

    this is an ******s requirement .... the problem is how many arrays we are going to declare is unknown & how many array variables we are going to create is also unknown ....

    can anyone reply for this .......?



  2. #2
    Senior Member
    Join Date
    Sep 2004
    Toronto, Canada

    Re: Challenge to All the Programmers of this forum

    Use redim preserve to increase the number of arrays required dynamically. If this is a 2 dimensional array, you might not be able to redim the first dimension, so transpose the array (swapping rows and columns) and then redim it to change it, and then transform it back to put it back the way it was.

    Read the help on redim, as it tells you all you need to know. You'll have to write your own code to do the transpose, but it's pretty easy.


    [ 03-21-2006, 06:31 AM: Message edited by: Aracknid ]
    Current AUT running test scripts on TP 6.3 build 183, SQL 2000 DB.
    Legacy AUT running on TP 5.6.0 Build 81 and TP 6.2 build 449 with SQL 2000 DB.

    Test Environment:
    TP/IE Clients on VMWare ESX 4.1 server, AUT Servers on VMWare ESX 3.5 server.

    Application Under Test (AUT):
    Client/Server based, IE 7/8 interface with SQL 2005/2008 DB.

    Automating with TP since August 2004.

  3. #3

    Re: Challenge to All the Programmers of this forum

    Hi Aracknid,

    I don't know whether u have pointed out the variable declaration or not ..... its a1(0) ...then a2(0) .... do u think is this possible to declare ...that too dynamically .... moreover we have already used redim ... but this will not work .....



  4. #4

    Re: Challenge to All the Programmers of this forum

    > how many arrays we are going to declare is unknown & how many array variables we are going to create is also unknown ....

    The simplest answer is: No, you can't do that. But what you need is actually possible [img]images/icons/wink.gif[/img]

    There really isn't a useful way to declare a different number of variables like that, but that's ok, because you wouldn't be able to reference them anyway (you would end up with huge blocks of if statements every where, testing to see if a variable even existed).

    THE GOOD NEWS, as Arachnid alluded to, is that you don't really want to do that anyway. The language writers thought of this scenario, and they created the dynamic array.

    Lets name the arrays "Datarray". The number of array variables we need is some number N, and we are given an array "sizes" with N integers in it, where each integer is the size of that array. So Datarray3 has a size of sizes(3) .... ok?

    I already explained you can't actually do: Datarray3 ... so the solution is to use dynamic arrays. You can do this several ways:

    1) make an array of arrays
    2) use a single multidimensional array
    3) use a Dictionary (of arrays)
    4) use a VBA.Collection

    I suggest you consult the VBA Language Refference... but here's one way, using collections:

    Sub Main()
    Dim N As Integer: N = 5
    ReDim sizes(N) As Integer

    'make up some random sizes between 1 and 100
    For X = 0 To N - 1
    sizes(X) = Int((100 - 2) * Rnd + 1)
    Next X

    'Now declare our collection of arrays:
    'NOTICE: we never declare the SIZE of DatArray
    Dim DatArray As New Collection

    'Initialize them to the appropriate sizes:
    For nI = 0 To N - 1
    ReDim DisArray(0 To sizes(nI))
    'Add it to our collection with a key of "nI"
    DatArray.Add DisArray, Str(nI)
    Next nI

    'Some fun to show how these work:
    'Print out the array sizes:
    For Each someArray In DatArray
    Debug.Print ("This is an array: " & IsArray(someArray))
    Debug.Print ("It has several (" & UBound(someArray) & ") items.")
    Next someArray

    'Determine how many arrays there are (remember, this is dynamic)
    Debug.Print "There are " & DatArray.Count() & " arrays."
    N = DatArray.Count()

    'Add another array, just for fun:
    Dim anarray(0 To 5) As String
    anarray(0) = "UNO"
    DatArray.Add anarray, "fiddler"

    ' Pick on a random array I:
    Dim I As Integer: I = Int((N - 1) * Rnd)
    Debug.Print "Array " & I & " has room for " & UBound(DatArray(Str(I))) & " items"
    ' Or a specific array: "fiddler" which we know has 5 items:
    Debug.Print "Array 'fiddler' has room for five (" & UBound(DatArray("fiddler")) & ") items"

    ' Put some data in it
    ' Note, this is ugly:
    Dim temp() As String
    ' get whatever might be in there
    temp = DatArray("fiddler")
    'temp(0) = "one"
    temp(1) = "two"
    temp(2) = "three"
    temp(3) = "four"
    temp(4) = "five"
    ' replace it with our new values
    DatArray.Remove "fiddler"
    DatArray.Add temp, "fiddler"

    ' Of course, if 'DatArray("fiddler")' wasn't an array, this would blow up
    For Each txt In DatArray("fiddler")
    Debug.Print txt

    End Sub
    Many a man's tongue broke his nose. -- Seumas MacManus



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
BetaSoft Inc.
All times are GMT -8. The time now is 07:41 PM.

Copyright BetaSoft Inc.