Sign in to follow this  
Followers 0
<SparTacuS>

Problem with Array

5 posts in this topic

Hello Everyone,

My first post/first script and I'm afraid it's a question already (pro'lly a no-brainer for the experienced)

My script is intended to keep Media Player obviously visable if running - but this part keeps throwing a "Subscript used with nonArray variable" error.

;Dim $size[2];
;
$size = WinGetClientSize("Windows Media Player")
;
;
If $size[0]<300 OR $size[1]<300 Then;
         WinMove("Windows Media Player", "", $xpos, $ypos, 300, 300);
EndIf;

As you can see i've tried both Dim-ing it and letting WinGetClientSize do it for me. Neither makes any difference.

Where am I going wrong?

Share this post


Link to post
Share on other sites



You need to test for @error = 1 because when WinGetClientSize fails it returns a 1 in stead of an Array:

Failure: Returns numeric 1 and sets @error to 1 if windows is not found.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

So

Dim $size[2];

$size = WinGetClientSize("Windows Media Player")

If Not @error AND ($size[0]<300 OR $size[1]<300) Then
        WinMove("Windows Media Player", "", $xpos, $ypos, 300, 300)
EndIf

Blank lines are allowed and trailing ; are not needed.

Edit: small note about my If line, there is no need for a separed If for @error and size[0] since being a AND if the @error becomes false AutoIt wont even consider the last part of the test because whatever is second part result this won't alter the final result!

So if there is an error size is no checked and so it gives no problem about reading the non existent array.

Edited by ezzetabi

Share this post


Link to post
Share on other sites

Thank you v.much ezzetabi - that works perfectly.

And thanks both for your swift replies.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You shouldn't assume everything will work as expected.

You have to add error checking.

Check the help file for more info.

Failure: Returns numeric 1 and sets @error to 1 if windows is not found.

While I fixed your code a little, here's what you do:

$size = WinGetClientSize("Windows Media Player")
If NOT @error Then
 ;If function failed then @error equals 1 so NOT @error means turn 1 into 0 and that means statement is false.
 ;If successful then @error equals 0 so NOT @error means turn 0 into 1 and that means statement is true.
   If $size[0] < 300 OR $size[1] < 300 Then
      WinMove("Windows Media Player", "", $xpos, $ypos, 300, 300)
   EndIf
EndIf

Damn! You're too quick for me :)

Edited by SlimShady

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0