How to create a dinamic FOR... TO... NEXT... Statement?! Please help!

skyyakal

Regular
Joined
Oct 3, 2003
Messages
74
Hallo everybody!

I have arrays whose number is varying depending on the values, that an users enters on a form.

I need a recombination of all values saved in these arrays, which would mean....

Example: If there are 3 Arrays with the length of 5 Values, 3 Values and 2 Values, I need to generate the following code:
Code:
FOR array1=0 To 5
FOR array2=0 To 3
FOR array3=0 To 2
...
NEXT
NEXT
NEXT

Is there any Method that builds/writes VB code?

I thought of the following solution:
Generating the VB code as Text and write it to a file. Then include this file at the appropriate place in my programm. But here I cannot find the include method. Would anyone know, what is a syntax for an "INCLUDE"-Method?


Thanks for any comments!!!
skyyakal
 
You don't need a code generator, you can can do this with some logic and a couple of nested loops.
Can you spell out what the user can select and how things are presented.
 
hi,
I'm not sure I've understand you, but instead of 5, 3 and 2, why don't you use the length property of Array ?

For array1 = 0 to myArray1.Length - 1
...
...
Next array 1
 
Hallo Robby!
Thank you for your help.

Here is my simplified code.

Code:
    Structure CardValueStructure
        Dim Value() As Integer
        Dim Scenario_ID() As Integer
    End Structure
Shared CardValueArray(0) As CardValueStructure

Farhter the array CardValueArray is Redimed, as well as the arrays inside of it.
example:
Code:
ReDim Preserve CardValueArray(NumberOfCards)
...
ReDim Preserve CardValueArray(COUNTER).Value(Counter2)
ReDim Preserve CardValueArray(COUNTER).Scenario_ID(Counter2)


Now I would like to recombine all the values, which are saved in

ReDim Preserve CardValueArray(COUNTER).Value(Counter2)
ReDim Preserve CardValueArray(COUNTER).Scenario_ID(Counter2)


which means I should create COUNTER number of FOR... TO... NEXT loops.

like (ATTENTION!!!: in the farther code N=COUNTER):

Code:
y0=CardValueArray(0).Value.GetUpperBound(0)-1
y1=CardValueArray(1).Value.GetUpperBound(0)-1
y2=CardValueArray(2).Value.GetUpperBound(0)-1
y3=CardValueArray(3).Value.GetUpperBound(0)-1
...
yN=CardValueArray(N).Value.GetUpperBound(0)-1



FOR x0=0 TO y0
FOR x1=0 TO y1
FOR x2=0 TO y2
FOR x3=0 TO y3
...
FOR xN=0 TO yN

...
'Write values to a file: 
CardValueArray(0).Value(X0)
CardValueArray(1).Value(X1)
CardValueArray(2).Value(X2)
CardValueArray(3).Value(X3)
...
CardValueArray(N).Value(XN)



Next xN
...
Next x3
Next x2
Next x1
Next x0

Would appreciate farther help!
Sincerely,
skyyakal
 
Hallo Rekam!
Please pay attention to my code above. You will find the answer there.

Thank you for your assistance in any way!!!
 
As both rekam and Robby have said before - you don't need to dynamically create the for next statements that way, you could use the .Length property of the arrays to write the loop.
Or even use a For ... Each loop.

Visual Basic:
dim card as CardValueStructure
for each card in CardValueArray
    dim i as integer
    for each i in card.Value
         'Write i to file
     next
next

...or something similar.

Also if you are just saving these values to a file you may want to investigate Serialization in the framework - could save you a hell of a lot of time and effort.

something similar to the following would work for serialization
Visual Basic:
Dim fs As IO.FileStream = IO.File.OpenWrite("c:\test.txt")
Dim fmt As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter    'replace with SoapFormatter for SOAP output.

fmt.Serialize(fs, CardValueArray)

or even have a look at XMLSerialization as another alternative

edit: some bad typos and also gave credit to one wrong person as well ;)
 
Last edited:
Hallo PlausiblyDamp,
your first code doesn't fit to my need. I need to write the Values simultaniosly just as in the following code (see above please):

Code:
'Write values to a file: 
CardValueArray(0).Value(X0)
CardValueArray(1).Value(X1)
CardValueArray(2).Value(X2)
CardValueArray(3).Value(X3)
...
CardValueArray(N).Value(XN)

I don't know what serialization is... I will check it and write.
Thank you very much for your suggestions!
 
So it should be the value of the first item in the first array element in CardValueArray, then the first item in the second element in the CardValueArray - right so far?
Does this mean though you will be writing some elements out multiple times? If you are doing this within a series of nested loops some values are going to be written multiple times

e.g. in your sample above - every value in the x3 loop will be written one per x2 loop - which will be executed one per x1 loop which also gets executed once every x0 loop!

if x0, x1, and x2 only go to 5 each then x3 will be executed 125 times!!!!

Could you provide a sample of the data and what you would like the output to resemble, it may clear things up a bit.
 
Hallo PlausiblyDump!
SERIALIZATION doesn't work in that case. I need rather something like PARALLELIZATION, but didn't find anything like that in VB .NET

please pay attention to my post from 11-02-2003 01:29 PM

here is the middle part modified a little bit for better understanding, what results I would like to have in the file.

Code:
y0=CardValueArray(0).Value.GetUpperBound(0)-1
y1=CardValueArray(1).Value.GetUpperBound(0)-1
y2=CardValueArray(2).Value.GetUpperBound(0)-1
y3=CardValueArray(3).Value.GetUpperBound(0)-1
...
yN=CardValueArray(N).Value.GetUpperBound(0)-1



FOR x0=0 TO y0
FOR x1=0 TO y1
FOR x2=0 TO y2
FOR x3=0 TO y3
...
FOR xN=0 TO yN


'Write values to a file: 
WriteLine.Writer(CardValueArray(0).Value(x0) & ";" & CardValueArray(1).Value(x1) & ";" & CardValueArray(2).Value(x2) & ";" & CardValueArray(3).Value(x3) & ";" & ... CardValueArray(N).Value(xN))


Next xN
...
Next x3
Next x2
Next x1
Next x0

PLEASE PAY ATTENTION!!!
1. which values are written in each line!!!
2. N is dynamic in CardValueArray(N) !!!
3. The length of each Value() array is DIFFERENT for each N

Please just check the following code once more.
Imagine you have N number of cards. Each card has Y number of different values. Now I need to write the recombination of all possible card values in each single line of a text file.
I can achive that constructing N number of FOR... TO... NEXT... loops. The TO Value of each loop is equal to Y value (the number of card values). N and each Y are different when the software starts.

Here is the modified structure of the code I would like to implement:
Code:
    Structure CardValueStructure
        Dim Value() As Integer
    End Structure
Shared CardValueArray(0) As CardValueStructure


Farhter the array CardValueArray is Redimed, as well as the arrays inside of it.

Code:
ReDim Preserve CardValueArray(NumberOfCards)
...
ReDim Preserve CardValueArray(COUNTER).Value(Counter2)


Now I would like to recombine all the values, which are saved in

ReDim Preserve CardValueArray(COUNTER).Value(Counter2)

in rows. Each row contains a recombination

which means I should create COUNTER number of FOR... TO... NEXT loops.

like (ATTENTION!!!: in the farther code N=COUNTER):


Code:
y0=CardValueArray(0).Value.GetUpperBound(0)-1
y1=CardValueArray(1).Value.GetUpperBound(0)-1
y2=CardValueArray(2).Value.GetUpperBound(0)-1
y3=CardValueArray(3).Value.GetUpperBound(0)-1
...
yN=CardValueArray(N).Value.GetUpperBound(0)-1



FOR x0=0 TO y0
FOR x1=0 TO y1
FOR x2=0 TO y2
FOR x3=0 TO y3
...
FOR xN=0 TO yN

'Write values to a file: 
WriteLine.Writer(CardValueArray(0).Value(x0) & ";" & CardValueArray(1).Value(x1) & ";" & CardValueArray(2).Value(x2) & ";" & CardValueArray(3).Value(x3) & ";" & ... CardValueArray(N).Value(xN))



Next xN
...
Next x3
Next x2
Next x1
Next x0


I hope I expessed myself in a good way.
Thank you for your further comments
 
in your post 11-02-2003 09:42 PM

YOU ARE COMPLITELY RIGHT PlausiblyDump!!!!

:-)



Thank you very much!!!
Please give me further suggestions, so I could look for a solution parallely!
 
Again you don't need the dynamic for next loop, creating and nesting to that degree will cause the inner most loop to be executed several times.

Visual Basic:
dim writer as StreamWriter
'Set up writer to point to correct file.
dim card  as CardValue
for each card in CardValueArray

dim i as integer
dim s as string

    for each i in card.value
        s= s & i.ToString() & ";"
    next
    writer.WriteLine (s)
next

Wouldn't that write out all values for a given card in a single line?
Or do you mean every single combination of every single card and value for the entire CardValueArray?
 
Last edited:
Hallo PlausiblyDamp,
now you are going the wrong direction.
I don't need all values of a card in a line.
I need all possible recombination of card values in one line
Here is an axample.

Card1 has values: 1, 11, 111, 1111
Card2 has values: 2, 22, 222
Card3 has values: 3

At the end I need all the values above recombined in single lines of a text file like that:

3; 2; 1
3; 22; 1
3; 222; 1
3;2;11
3;22;11
3;222;11
3; 2; 111
3; 22; 111
3; 222; 111
3;2;1111
3;22;1111
3;222;1111

Now imagine, the number of Cards is different each time a program starts. The number of values of each card is different as well (among each other also).

How do I do the recombination dynamically?!
To solve this I need the code construction showed in my post 11-02-2003 10:26 PM.

Thank you again for your help!!!
 
Last edited:
Ahhhh, I get what you mean!
Nasty - the only way I can think of doing that is through recursion. Unfortunately my brain is going to sleep about now, I'll see if I get change to look at this tomorrow though.

Meanwhile here's a couple of links with regards to recursion that may give you a head start:
http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Recn/Perm/
http://personal.vsnl.com/erwin/magic.htm

and I've got to ask because I hate thinking about recursion ;) but why do you need this?
 
Hallo PlausiblyDump!
Thank you so much for your replies.
I will check the links!

Will write as soon as I get this working.

I need this for a software, that generates scenarios. Therefore the recombination algorithm. Each line represents a scenario.

Thanks again for your help!
 
One more question. Is there a method in VB, which would import VB. Code from external source like a text file?!

This would ease the whole programming tremendously!

Can I contact Robby somehow?! Or I can only hope, he will have a look at this part of the forum. He ment, you can do such permutations in a logic way...
 
Still cannot find the solution. Can anyone tell me, is there absolutely no possibility to import VB Code from a text file through an "include" method?

If there is such one you can build much better code to solve my problem.

Thank you for your comments!
 
Back
Top