Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed Post any question regarding Basic4ppc.


Passing arrays to Subs.


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-15-2008, 03:04 PM
Junior Member
 
Join Date: Jul 2007
Posts: 32
Default Passing arrays to Subs.

Hi everyone,

I've been checking how to pass arrays as parameters to Subs. As I got some errors, try to use the sample in help:
Sub Globals
Dim arr1(10), arr2(10)
Dim tempArray (0)
End Sub


Sub App_Start
SomeSub (arr1())
msgbox(arr1(5)) 'will show 5
End Sub


Sub SomeSub (tempArray())
tempArray(5) = 5
End Sub


But got the same error. (ErrorArrays.jpg). If I comment the Dim TempArray(0) in Globals, got another error (ErrorArrays2.jpg)

So, my question is how do I do to pass arrays to Subs?

Thanks in advance,

Jes.
Attached Images
File Type: jpg ErrorArrays.JPG (11.0 KB, 7 views)
File Type: jpg ErrorArrays2.JPG (8.6 KB, 4 views)
Reply With Quote
  #2 (permalink)  
Old 04-15-2008, 03:33 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,199
Default

You can't pass the array as a parameter.
However you can assign one array to another and get the same result:
Code:
Sub Globals
 Dim arr1(10), arr2(10)
 Dim tempArray (0)
End Sub


Sub App_Start
tempArray() = arr1()
 SomeSub
 msgbox(arr1(5)) 'will show 5
tempArray() = arr2()
SomeSub

End Sub


Sub SomeSub 
  tempArray(5) = 5
End Sub
Reply With Quote
  #3 (permalink)  
Old 04-15-2008, 04:14 PM
Junior Member
 
Join Date: Jul 2007
Posts: 32
Thumbs up OK.

Ok. But as I took the sample from Help, maybe the help file should be updated, just for not other users to go wrong.

Thank you, Erel.
Reply With Quote
  #4 (permalink)  
Old 04-15-2008, 05:00 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,199
Default

Which topic are you referring to?
Reply With Quote
  #5 (permalink)  
Old 04-15-2008, 05:36 PM
Junior Member
 
Join Date: Jul 2007
Posts: 32
Default

It's in Help --> Main Help-->Find-->Variables.
I copy&paste it for you to see.

All simple variables are variant variables.
Which means that any variable can store any type of number or a string.
Variables can be global or local.
Local variables value can be used only while their parent sub is executing.
Global variables’ values can be used everywhere.
Except for array and structure variables, there is no need to declare a local variable before using it.
Since version 5.0 an optional (and recommended) check is done to make sure that all local variables are used and that no variable is used before it is assigned any value.
Global variables are variables that were declared (or used) in Sub Globals; all other variables are local.


Example:
(An error message will show if using the check unassigned variables.)
Sub Globals
a=20
End Sub


Sub App_Start
b=10
CalcVars
End Sub


Sub CalcVars
Msgbox ("a = " & a)
Msgbox ("b = " & b)
End Sub


Result: First msgbox will show a = 20
Second msgbox will show b =
b is empty because it's local and wasn't assigned any value yet in this sub.


To pass data between subs you could use global variables, or better, you can pass the data as parameters.


Example:
Sub Globals
a=20
End Sub


Sub App_Start
c=10
CalcVars (c)
End Sub


Sub CalcVars (b)
Msgbox ("a = " & a)
Msgbox ("b = " & b)
End Sub
Result: The second msgbox will show b = 10


Note: When using parameters the variable is not passed, but its value is copied.
In the above example, I used a variable named c instead of b.
Remember that if you change a local variable’s value in one place (even if it had received its data from a parameter), then the change will not affect any other local or global variable outside the current sub.




Array Variables


Basic4ppc supports arrays of up to three dimensions.
Array variables are always global and must be declared before used.
Arrays can store items of a certain data type.
This is useful for working with external libraries which sometimes expect an array of a certain type.
Declare global variables with the Dim keyword.
The same array can be declared again with a different size any number of times during the program.
However, it must be first declared in Sub Globals (even with 0 items).
An array index starts from 0 and its last index is array dimensions - 1.
Example:
Sub Globals
Dim Books (20)
Dim Buffer (100) As Byte
End Sub


Result: Declares an array of 20 items starting from Books(0) and up to Books(19) and an array of bytes starting from Buffer(0) and up to Buffer(99)
When you need to reference an entire array (not one item in the array) write the array's name followed by ().
Example:
i = ArrayLen (buffer() )
BinaryFile.WriteBytes (buffer())


Arrays can be declared with 0 items like:
Dim Buffer(0) As Byte
Buffer is an empty array that can be used later with an external library which returns an array.
Example:
Buffer() = Serial.InputArray


Arrays can be passed as parameters to other subs.
Unlike other variables, arrays are passed by reference.
Changing the value of the array in the target sub will also change the array in the caller sub.
Example:
Sub Globals
Dim arr1(10), arr2(10)
Dim tempArray (0)
End Sub


Sub App_Start
SomeSub (arr1())
msgbox(arr1(5)) 'will show 5
End Sub


Sub SomeSub (tempArray())
tempArray(5) = 5
End Sub


Structure Variables


Structures are variables with customized fields.
Using structures the code can be clearer and better organized.
Structures must be declared in Sub Globals.
See the Dim keyword for information on declaring structures.
Using regular structures is similar to using a control's properties.
Write the name of the variable followed by a period '.' and a list of the available fields will pop.
Example:
Sub Globals
Dim Type(Name, ID, Age) person
End Sub


Sub App_Start
person.Name = "John"
person.ID = 1234567
person.Age = 30
End Sub


You could also use arrays of structures (up to two dimensions):
Sub Globals
Dim Type(Name, ID, Age) persons (100)
End Sub


Sub App_Start
persons(0).Name = "John"
persons(0).ID = 1234567
persons(0).Age = 30
End Sub


Notes:
- On the device only, you need to press on Subs - REFRESH in order that any declaration change will appear in the pop up list.
- You can reference the whole structure using the structure name followed by ().
Example: person() = SomeSub
- Structures are converted to arrays during the compilation.
In the first example, person will be an array of one dimension, person.Name = "John" will be converted to person (0) = "John" and so on.
You can also use the array syntax to work with structures.
Reply With Quote
  #6 (permalink)  
Old 04-15-2008, 05:56 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,199
Default

This topic was updated in V6.0.
For some reason you still see the old topic.
It is also available here: http://www.basic4ppc.com/help/variables.html
Reply With Quote
  #7 (permalink)  
Old 04-15-2008, 06:03 PM
Junior Member
 
Join Date: Jul 2007
Posts: 32
Default

That's true. I got help v5.80.

But if it was possible to pass arrays like arguments in prior versions, I think it would be quite useful to do it in new versions also.

Thank you again for your support.

Jes.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
IDE subs list width klaus Beta Versions 1 09-28-2008 04:56 AM
Redundant SUBs badkarma Basic4ppc Wishlist 0 04-10-2008 08:50 PM
How to use timer to clock speed of subs? Stellaferox Questions & Help Needed 4 02-28-2008 08:07 AM
Passing arrays in B4PPC6? Frank Questions & Help Needed 1 01-14-2008 01:15 PM
Passing references to tables to subs LineCutter Questions & Help Needed 5 05-31-2007 09:27 PM


All times are GMT. The time now is 02:42 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0