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

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

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

Post by lemodizon » Wed Jun 22, 2022 2:37 pm

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

Attachments
error code.PNG
Last edited by lemodizon on Wed Jun 22, 2022 3:41 pm, edited 1 time in total.
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

andresdt
Posts: 146
Joined: Fri Aug 16, 2019 7:51 pm

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

Post by andresdt » 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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » 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

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

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

Post by lemodizon » Wed Jun 22, 2022 3:25 pm

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.
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

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

Post by lemodizon » Wed Jun 22, 2022 3:30 pm

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
Attachments
err.PNG
err.PNG (10.34 KiB) Viewed 3523 times
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

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

Post by lemodizon » Wed Jun 22, 2022 3:40 pm

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.
Attachments
error code.PNG
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Wed Jun 22, 2022 4:06 pm

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

stam
Posts: 2635
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

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

Post by stam » 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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu Jun 23, 2022 6:25 pm

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

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

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

Post by lemodizon » Sat Jun 25, 2022 1:37 am

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.
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”