Page 1 of 1

My code is not working anymore. but before it is working. (Solved Thanks)

Posted: Wed Jun 22, 2022 2:37 pm
by lemodizon
Hi Everyone,

below is my code for my edit button. first run (project file) it will work but now once i open the edit script it has a error. before it's working in the community version and standalone application. I tried to run in starter livecode and community version same result has error but before it is working.

hope you can help me. thanks in advance

Code: Select all

on mouseUp tbutton
   global tLineToEdit
   
   if tbutton = 1 then
      
      put the dgHilitedLine of group "MyUserList" into tLineToEdit
      
      if tLineToEdit is empty then
         beep
         Answer info "Please select user name to EDIT INFORMATION" titled "DSWD"
      else
         if tLineToEdit is a number and tLineToEdit >0 then
            set the defaultstack to "EditUserStack"
            set the itemdel to tab
            put line tLineToEdit of the dgtext of grp "MyUserList" of cd "UserCard" of stack "UserStack" into tData
            
            put item 1 of tData into fld "userIDFld"
            put item 2 of tData into fld "FNameFld"
            put item 3 of tData into fld "MNameFld"
            put item 4 of tData into fld "LNameFld"
            put item 5 of tData into fld "UserNameFld"
            put item 6 of tData into fld "PasswordFld"
            set the label of btn "GenderList" to item 7 of tData
            
            put item 8 of tData into fld "CityFld"
            put item 9 of tData into fld "ProvinceFld"
            set the label of btn "RoleList" to item 10 of tData
            
            
            set the defaultstack to "EditUserStack"
            modal stack "EditUserStack"
            
         end if
      end if
      
   else
      exit mouseUp
   end if
   
end mouseUp


Re: My code is not working anymore. but before it is working.

Posted: Wed Jun 22, 2022 2:54 pm
by andresdt
change your second line

Code: Select all

global tLineToEdit
by

Code: Select all

local tLineToEdit, tData
or in the script editor settings, disable the "Veriable checking" option.

Re: My code is not working anymore. but before it is working.

Posted: Wed Jun 22, 2022 3:19 pm
by dunbarx
@Andre. Not sure why you changed the global "tLineToEdit' to local, since I assume it is required elsewhere in the stack, and do not see the need to declare "tData" as local.

@lemodizon, I tried your handler as it stands and found no issues. Anyway, what does the error say?

Craig

Re: My code is not working anymore. but before it is working.

Posted: Wed Jun 22, 2022 3:25 pm
by lemodizon
andresdt wrote:
Wed Jun 22, 2022 2:54 pm
change your second line

Code: Select all

global tLineToEdit
by

Code: Select all

local tLineToEdit, tData
or in the script editor settings, disable the "Veriable checking" option.
Hi andresdt,

Thanks. I will try it.

Re: My code is not working anymore. but before it is working.

Posted: Wed Jun 22, 2022 3:30 pm
by lemodizon
dunbarx wrote:
Wed Jun 22, 2022 3:19 pm
@Andre. Not sure why you changed the global "tLineToEdit' to local, since I assume it is required elsewhere in the stack, and do not see the need to declare "tData" as local.

@lemodizon, I tried your handler as it stands and found no issues. Anyway, what does the error say?

Craig
Hi Craig,

here's the error below

Image

Re: My code is not working anymore. but before it is working. Solved Thanks

Posted: Wed Jun 22, 2022 3:40 pm
by lemodizon
Hi Craig and andresdt,

I unchecked this box called "Strict Compilation Mode" and the error is gone and it works again just like before.

Does anyone use this Strict Compilation Mode?

Thanks for the support everyone.

Re: My code is not working anymore. but before it is working. (Solved Thanks)

Posted: Wed Jun 22, 2022 4:06 pm
by dunbarx
I never use "Strict Compilation Mode". Just too lazy, and I am old-school, or at least just old.

But know if you do, you can no longer get away with creating variables on the fly. They must be declared explicitly in every case.

Craig

Re: My code is not working anymore. but before it is working. (Solved Thanks)

Posted: Thu Jun 23, 2022 2:14 pm
by stam
I always use strict compilation mode because it catches simple typo errors effectively.

if your code wasn't working before it's because you, strictly speaking, had some form of syntax error (eg incorrect spelling of variable, no quotes where there should be some etc). Or more commonly because you're using variable not previously declared.

The line causing the error in your code that was being complained about was

Code: Select all

put line tLineToEdit of the dgtext of grp "MyUserList" of cd "UserCard" of stack "UserStack" into tData
but tData wasn't declared either globally or in the handler - hence the error.

Also, keep in mind scope - by declaring a global variable inside your handler, it is unavailable to other handlers in the same script unless they also declare this inside themselves. A better practice if you need a global variable would be to declare this outside of your handler, at the start of your script - that way it's available for all the handlers in the script - and only needs to be be declared once per script (instead of once per handler)

in other words my practice would be:

Code: Select all

global tLineToEdit -- a common convention would be to prefix with 'g' ie gLineToEdit if global variable 

on mouseUp
   local tData
   ...
   put line tLineToEdit of the dgtext of grp "MyUserList" of cd "UserCard" of stack "UserStack" into tData
   ...
end mouseUp
Of course you can ignore all of this and just switch off strict compilation mode and if there are no typos this will work, but you risk not identifying an error in spelling. By declaring the variable at the start, you 'pin' the name.


Consider the following example with an inadvertent spelling error:

Code: Select all

put 999 into tNum
add 1 to tNm -- the 'u' is missed out by accident
this will run fine without strict compilation, but you'll end up with variables tNum = 999 and tNm = 1, instead of the intended tNum = 1000
By declaring 'local tNum' at the start and with strict complication on, this will flag an error and will direct you to fix this.

So actually i use this always because i'm lazy and don't want to spend hours debugging simple typos ;)

Stam

Re: My code is not working anymore. but before it is working. (Solved Thanks)

Posted: Thu Jun 23, 2022 6:25 pm
by dunbarx
Stam makes a very important point. Declared variables, global or local, must be placed ABOVE any handlers that might need them.

There was a discussion many years ago that placing them in the middle of a script that contained multiple handlers allowed those handlers below the declaration line to "see" the variables, but not those above. The comments were whether this could be exploited in some way to enhance functionality.

I asserted that way lay madness. Anyway, I always put ALL mine on line 1.

Craig

Re: My code is not working anymore. but before it is working. (Solved Thanks)

Posted: Sat Jun 25, 2022 1:37 am
by lemodizon
stam wrote:
Thu Jun 23, 2022 2:14 pm
I always use strict compilation mode because it catches simple typo errors effectively.

if your code wasn't working before it's because you, strictly speaking, had some form of syntax error (eg incorrect spelling of variable, no quotes where there should be some etc). Or more commonly because you're using variable not previously declared.

The line causing the error in your code that was being complained about was

Code: Select all

put line tLineToEdit of the dgtext of grp "MyUserList" of cd "UserCard" of stack "UserStack" into tData
but tData wasn't declared either globally or in the handler - hence the error.

Also, keep in mind scope - by declaring a global variable inside your handler, it is unavailable to other handlers in the same script unless they also declare this inside themselves. A better practice if you need a global variable would be to declare this outside of your handler, at the start of your script - that way it's available for all the handlers in the script - and only needs to be be declared once per script (instead of once per handler)

in other words my practice would be:

Code: Select all

global tLineToEdit -- a common convention would be to prefix with 'g' ie gLineToEdit if global variable 

on mouseUp
   local tData
   ...
   put line tLineToEdit of the dgtext of grp "MyUserList" of cd "UserCard" of stack "UserStack" into tData
   ...
end mouseUp
Of course you can ignore all of this and just switch off strict compilation mode and if there are no typos this will work, but you risk not identifying an error in spelling. By declaring the variable at the start, you 'pin' the name.


Consider the following example with an inadvertent spelling error:

Code: Select all

put 999 into tNum
add 1 to tNm -- the 'u' is missed out by accident
this will run fine without strict compilation, but you'll end up with variables tNum = 999 and tNm = 1, instead of the intended tNum = 1000
By declaring 'local tNum' at the start and with strict complication on, this will flag an error and will direct you to fix this.

So actually i use this always because i'm lazy and don't want to spend hours debugging simple typos ;)

Stam

Hi Stam,

Thank you for the advice.