Normal Topic Declaring Vars in Global Code subroutines (Read 1086 times)
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Declaring Vars in Global Code subroutines
Apr 4th, 2007 at 4:26pm
Print Post Print Post  
I'm having some difficulty with some subroutines I use to check if the text entered into an element is longer than the space available on the form where it will be printed.  I think it relates to where the type of variable is declared, and where the value is declared.  My understanding is a bit muddled.

I know the var type needs to be declared in the part of global code that defines the subroutine, just after the "Subroutine" statement, but do I need to declare a Stat as well at the beginning of Global code?  And do I need to declare the Var type in the element's programming, before the subroutine is called?  And just where is the best place to declare its starting value?

Thanks in advance.
  

**
Captain Infinity
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: Declaring Vars in Global Code subroutines
Reply #1 - Apr 4th, 2007 at 5:22pm
Print Post Print Post  
Hello Scott,

Without seeing all of your code and knowing what problems you are having, I can't really tell you if you need another variable or not and where it should be declared.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Declaring Vars in Global Code subroutines
Reply #2 - Apr 4th, 2007 at 8:07pm
Print Post Print Post  
I'll try to explain my current confusion.  When creating a subroutine in global code, we have to use Statics.  But if the figures or strings or whatever need to change when using it in an element, we need to use Variables.

So in Global code I declare my Stats, then write my subroutine to use the Stats.  But now to call it from an element I'm not sure what to do.

I don't have this problem with any of my subroutines that just use my element names directly, but as soon as I need to adjust something, say the size of the available space, I'm lost.  I'm sure I'm just misunderstanding something but I don't know what it is.
  

**
Captain Infinity
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Declaring Vars in Global Code subroutines
Reply #3 - Apr 4th, 2007 at 8:27pm
Print Post Print Post  
Scott,

Stats and Vars are both variables.

You only need to use Stats in GLOBAL CODE outside of a UDF.

Code
Select All
GLOBAL CODE

stat sID as Int

Subroutine TestIt()
var vLoop as Int

// Do stuff
End Subroutine

 



  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1350
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Declaring Vars in Global Code subroutines
Reply #4 - Apr 4th, 2007 at 9:08pm
Print Post Print Post  
Scott,

If you don't need to retain the variable's value for use in another event or for use in another record (while navigating through the result set or adding new records), then you only need to declare the variable that the subroutine will be needing within that subroutine. You do not need to duplicate that declaration as a "Stat" in global code.

If you do need to retain the variable's value, then only declare it as a "Stat" in global code. You don't want to re-declare it again in the subroutine. If you declare a static variable named "A", and a regular variable within a subroutine also named "A", then the subroutine will use the one declared within it, not the static one declared in global code.

If a subroutine refers to variable "A", it will first try to use one that was declared within the subroutine. If there was no variable "A" declared inside the subroutine, it will then try to a static variable "A" declared in global code (assuming the subroutine definition is located in global code; otherwise, it will first try to use variable "A" delclared in the same event, then it will try to use one declared in global code).

I'm not sure if I made this clear, or if I muddied the waters more for you. Maybe a little demo will help. Try inserting this code into a test application.

Global Code:
Stat A as String = "Stat A (as declared in Global Code)"
Stat B as String = "Stat B (as declared in Global Code)"


SUBROUTINE MySubroutine()
Var A as String = "Var A (as declared in MySubroutine)"

WriteLn("Subroutine:")
WriteLn(A)
WriteLn(B)
WriteLn("")

End SUBROUTINE


MyCommandButton :: On Element Entry:
MySubroutine()

WriteLn("Event:")
WriteLn(A)
WriteLn(B)
WriteLn("")



When you click on the command button you should get this in the slate window:
Subroutine:
Var A (as declared in MySubroutine)
Stat B (as declared in Global Code)

Event:
Stat A (as declared in Global Code)
Stat B (as declared in Global Code)


Notice that the "A" variable from inside the subroutine was used by MySubroutine, but because "B" was not declared inside the subroutine, the static variable "B" from global code was used. Also, the static "A" was not changed by declaring variable "A" inside the subroutine. The WriteLn call in the event shows that the static "A" is still the same after the subroutine was run.

Hope this helps.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Declaring Vars in Global Code subroutines
Reply #5 - Apr 4th, 2007 at 9:11pm
Print Post Print Post  
Carl: That's a very nice writeup. Thank you!

Just a note: NEVER DO THIS WITH Stats!

Global Code:
Stat A as String = "Stat A (as declared in Global Code)"
Stat B as String = "Stat B (as declared in Global Code)"

Always initialize them in a separate line or you can get some very odd behavior.

Global Code:
Stat A as String
Stat B as String

    A = "Stat A (as declared in Global Code)"
    B = "Stat B (as declared in Global Code)"
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1350
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Declaring Vars in Global Code subroutines
Reply #6 - Apr 4th, 2007 at 9:36pm
Print Post Print Post  
Thanks for the tip Erika. I wasn't aware of that (or I don't remember it, anyway).

Can we expect the Version 2 program editor to check for that?
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Declaring Vars in Global Code subroutines
Reply #7 - Apr 4th, 2007 at 9:43pm
Print Post Print Post  
Quote:
Thanks for the tip Erika. I wasn't aware of that (or I don't remember it, anyway).

Can we expect the Version 2 program editor to check for that?


It's not something the program editor can check for as it is not actually illegal syntax.  It will do exactly what you are telling it to do, which may cause unexpected behavior due to the optimization which allows a form to compile its code only once per mode per session.

By initializing the variable in the declaration, you cause it to be initialized at compile time. Therefore, the second time you open the form, it is not reinitialized as the form does not recompile. By initializing it in an executable line of code you ensure that it will be reinitialized every time the form is opened.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1350
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Declaring Vars in Global Code subroutines
Reply #8 - Apr 5th, 2007 at 12:32am
Print Post Print Post  
Hmmm... I just tested it, and it seems to be re-initializing each time the form is opened. But I highly respect your opinion, and am sure you have good reason to say that; so I will avoid coding that way in the future.

Thanks again. Smiley
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Declaring Vars in Global Code subroutines
Reply #9 - Apr 5th, 2007 at 12:58pm
Print Post Print Post  
Quote:
Hmmm... I just tested it, and it seems to be re-initializing each time the form is opened. But I highly respect your opinion, and am sure you have good reason to say that; so I will avoid coding that way in the future.

Thanks again. Smiley


The results you see will vary depending on particular circumstances. It may appear to reinitialize (it doesn't). I may appear to hold the last value you set it to before closing the form (it doesn't). It may appear to have a "random" value. Because it isn't actually being reinitialized, it's value becomes unpredictable and coincidental.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged