# Thread: Can someone explaine this logic to me??

1. ## Can someone explaine this logic to me??

Sample 1...
</font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">void main ()
{
INTEGER i;

// Run the test 5 times...
for (i = 0; i &lt; 5; ++i)
{
TestArray ();
}
}

testcase TestArray () appstate none
{
INTEGER i;
STRING sValue = &quot;MyValue&quot;;
INTEGER iIndex = 0;
ARRAY [] of STRING asValues;

// Add 5 elements to the array...
for (i = 0; i &lt; 5; ++i)
{
// Bump the index...
iIndex = ArraySize (asValues) + 1;

// Resize the array...
ArrayResize (asValues, iIndex);

// Stuff the line into the array...
asValues[iIndex] = sValue;
}

// Print the array size message...
Print (&quot;The array now has &quot; + [STRING]ArraySize (asValues) + &quot; elements in it.&quot;
}</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">Sample 2...
</font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">void main ()
{
INTEGER i;

// Run the test 5 times...
for (i = 0; i &lt; 5; ++i)
{
TestArray ();
}
}

testcase TestArray () appstate none
{
INTEGER i;
STRING sValue = &quot;MyValue&quot;;
INTEGER iIndex = 0;
ARRAY [] of STRING asValues = {};

// Add 5 elements to the array...
for (i = 0; i &lt; 5; ++i)
{
// Bump the index...
iIndex = ArraySize (asValues) + 1;

// Resize the array...
ArrayResize (asValues, iIndex);

// Stuff the line into the array...
asValues[iIndex] = sValue;
}

// Print the array size message...
Print (&quot;The array now has &quot; + [STRING]ArraySize (asValues) + &quot; elements in it.&quot;
}</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">in the first sample we declare our array like this.

ARRAY [] of STRING asValues;

And our print outs are 5, 10, 15, 20, 25,

and in sample 2 we declare it like this..

ARRAY [] of STRING asValues = {};

and we get a print out of 5, 5, 5, 5, 5

Isn't the declaration of the first sample private to the testcase, and we re-enter the testcase the variable should be empty each time?

2. ## Re: Can someone explaine this logic to me??

I don't have any explanation, but a question instead. Why not use LIST OF STRING so that you don't have to do all that ArraySize & ArrayResize stuff? It seems like you are building your own LIST OF STRING on the fly.

3. ## Re: Can someone explaine this logic to me??

I could use LIST OF STRING, but using ARRAY[] of STRING will allow me to build a robust list sorting class. Either way the logic does make any sense in my example.

4. ## Re: Can someone explaine this logic to me??

I can't say that I've ever used ListSort() so I don't know how robust it (is/is not)...

I've noticed the opposite of what you describe with LIST OF STRING. If you don't have the '= {}' in your declaration then ListAppend() won't work. Funny, huh?

I suppose that you dumped out something like:

iIndexTest = ArraySize (asValues)
Print (iIndexTest)

to see what it returns in both cases?

5. ## Re: Can someone explaine this logic to me??

You can easily build a robust list sorting class using a LIST OF STRING, you don't need to use arrays.

6. ## Re: Can someone explaine this logic to me??

the question wasn't how i am building the class, the question was why are the dirrences in the two samples?

7. ## Re: Can someone explaine this logic to me??

And yes I do agree that LIST of STRING can build a robust list sorting class, I just found this logic to be a little disturbing.

8. ## Re: Can someone explaine this logic to me??

If you look in the help document there is a section called "Uninitialized variables" and it notes that "SilkTest does not initialize variables for you". So this combined with "appstate none" (if this even affect this), I'm not surprised that you are seeing the results you are.

9. ## Re: Can someone explaine this logic to me??

What do you see if you put a copy of

Print ("The array now has " + [STRING]ArraySize (asValues) + " elements in it.");}

immediately after each of the "ARRAY [] of STRING asValues ..." lines?

John

10. ## Re: Can someone explaine this logic to me??

First of all! The logic has got nothing to do with the scope of the function. Second thing and obviously, the most important thing is that, Silk behaves differently with Arrays. Try the following statement…

STRING sValue = "MyValue{ArraySize (asValues)}"

The statement should be ahead of the Array declaration. In normal circumstances, with any other compiler, it throws an error saying that the variable asValues is not declared. But, silk does not in case of Arrays. If you try with the following statement..

STRING sValue = "MyValue{ArraySize (iIndex)}"

Silk definitely throws an error saying that, “iIndex is not set”.

From the above two examples, it is clear that the Arrays scope is always global to the file. So, the behavior shown in the two function declarations is not at all a surprise. So, in the example 2, the behavior is very well expected. In this, you have initialized the array like asValues={}. This declaration makes a lot of difference. Whenever a call is made to the test case, first of it initializes the array with empty contents and with 0 elements.

Page 1 of 2 12 Last

#### Posting Permissions

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

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.