Page Index Toggle Pages: 1 [2]  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) writing different datatypes in array (Read 2085 times)
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: writing different datatypes in array
Reply #15 - Apr 12th, 2006 at 7:37pm
Print Post Print Post  
Quote:
Some questions after successful programming:

Why is it not possible to handle Arrays defined in global code in a subroutine like this:

Code
Select All
var tempArray as Array[10] of string

subroutine test
tempArray[1] = "xy"
end subroutine

test()

  



i managed this with the following code

Code
Select All
[b]stat[/b] tempArray as Array[10] of string

subroutine test
tempArray[1] = "xy"
end subroutine

test()

  




Both of these example above work just fine (though both need to have parenthesis in the subroutine declaration), if they are called as you have presented them - from global code. But if you call the first one from anywhere other than global code, the "tempArray" variable falls out of scope, and without the "stat" declaration SBasic has no way of knowing that it should retain the value of the variable, so that it can be accessed when the subroutine is called from a different event.

  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: writing different datatypes in array
Reply #16 - Apr 12th, 2006 at 7:46pm
Print Post Print Post  
Quote:
next question:

why is it not possible to use generic arrays on stat arrays like this:

Code
Select All
stat tempArray as Array[10] of string

subroutine test (var arrayIn as array)

SetStringElement("xy",arrayIn,1)
end subroutine

test(tempArray)

 




The one above worked perfectly for me. Both when I called "test" from global code and when I called it from another event. I assume that you have this subroutine and the static array declared in global code. What problem are you seeing?
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: writing different datatypes in array
Reply #17 - Apr 12th, 2006 at 7:58pm
Print Post Print Post  
Quote:
and an other question:

i just tried to use the split command in a while loop and this does not work

Code
Select All
i = 10
j = 1

While j <= i
{
	   vLine = ArrayTemp[j]

	   while k < 19
	   {
	LSArray[k,j] = Split(vLine,"$")
	k = k + 1

	   }
j = j + 1
}
 




I just tried:
Code
Select All
var ss as string

ss = "one$two$three$four$five"

while(@Len(ss) > 0)
{
  writeln(Split(ss, "$"))
}
 



and all seemed well. Are you taking into account that Split will "deplete" the original input string?

What problem are you seeing?
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
wase
Junior Member
**
Offline


Always looking for a good
code

Posts: 57
Location: Austria
Joined: Jun 22nd, 2004
Re: writing different datatypes in array
Reply #18 - Apr 13th, 2006 at 3:27pm
Print Post Print Post  
OK i forget to put in the parenthesis when writing it into the forum. In original programming i used them.

But: why is a variable declared as var in global code access able from anywhere (also subroutines) but a array declared as var in global code not access able in subroutines called from anywhere?


Maybe i had a bad day when programming the other code (generic array and while loop with split).

  
Back to top
ICQ ICQ  
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: writing different datatypes in array
Reply #19 - Apr 13th, 2006 at 3:59pm
Print Post Print Post  
Quote:
OK i forget to put in the parenthesis when writing it into the forum. In original programming i used them.

But: why is a variable declared as var in global code access able from anywhere (also subroutines) but a array declared as var in global code not access able in subroutines called from anywhere?


Both a regular variable and an array are "accessible" from other code. But if not declared with "stat", SBasic will not attempt to retain either their values of the memory holding their values. When you then try to access that memory, you are accessing memory that Sesame no longer "owns". That can, and in the case of an array - probably will, result in a segmentation fault (the operating system error that occurs when a program tries to access memory that some other process is using).

In brief - don't do it. The only reason to declare a variable as var and outside of any function or subroutine, yet inside of global code, is so that more than one function or subroutine in global code, and called from global code, can access that variable. If a variable, or array, is to be accessed from outside global code, or from a function or subroutine called from outside global code - it must be declared as static.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
wase
Junior Member
**
Offline


Always looking for a good
code

Posts: 57
Location: Austria
Joined: Jun 22nd, 2004
Re: writing different datatypes in array
Reply #20 - Apr 13th, 2006 at 4:26pm
Print Post Print Post  
that is very useful information!!

i think i had much luck by programming because i declare nearly all variables i use in different sections in global code as var and not as static and it works !!

i think i have to change some declarations to static

thanks for this info

I think there is a little work in Programming Guide
It is a bit confused described (text below)
there is a section in the guide where static variables are described but in my mind there is no section where the information you wrote is described.

Global Code
Sesame’s Program Editor contains a Global Code area. Programming placed in this
area runs once — when the form loads and before the first record is displayed.
Variables and arrays (see the section on Variables and Arrays) declared here are
available to any other event on the form and retain their values until you close the
form. The contents of such variables or arrays do not change as you
add/browse/update records, unless your programming changes them.
The Global Code area of the Program Editor can be used for things like these:
  
Back to top
ICQ ICQ  
IP Logged
 
Page Index Toggle Pages: 1 [2] 
Send Topic Send Topic Print Print