Jump to content

Declare The Array Inside A Function


Recommended Posts

All,

Assuming I want to declare the array inside a function, and poll the array outside of it, how to do?

MyFunc()
msgbox (0,"",$x[0][0])

Func MyFunc ()
    $size=5
    Dim $x[$size][$size]
    $x[0][0]="abc"
EndFunc

This code is not working

Link to comment
Share on other sites

try declaring it global and rediming it

i.e.

Global $x[1][1]
MyFunc()
msgbox (0,"",$x[0][0])

Func MyFunc ()
    $size=5
    ReDim $x[$size][$size]
    $x[0][0]="abc"
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

  • Moderators

All,

Assuming I want to declare the array inside a function, and poll the array outside of it, how to do?

MyFunc()
msgbox (0,"",$x[0][0])

Func MyFunc ()
    $size=5
    Dim $x[$size][$size]
    $x[0][0]="abc"
EndFunc

This code is not working

Do you mean something like this:
MsgBox(0, '', MyFunc())

Func MyFunc ()
    $size=5
    Dim $x[$size][$size]
    $x[1][0] = "abc"
    Return  $x[1][0]
EndFunc
??

Edit:

Gary that's what I thought to begin with, but it looked like he was only trying to get the value... I am sure you are right.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks, guys,

Since my array's size is dynamic, I'll declare it outside of a function and use the ReDim...

Just though that declaring it inside a function will declare the array in a global scope...

Actually you can, I just like having my globals at the top

MyFunc()
msgbox (0,"",$x[0][0])

Func MyFunc ()
    $size=5
    Global $x[$size][$size]
    $x[0][0]="abc"
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

This would be a good time for a snip from the helpfile:

The difference between Dim, Local and Global is the scope in which they are created:

Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!)

Global = Forces creation of the variable in the Global scope

Local = Forces creation of the variable in the Local/Function scope

When using variables, the local scope is checked first and then the global scope second.

So dim-ing an array in the function (when it hasn't been used) is the same as making it local, which is why you can't use it outside that function.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...