Static Variable and Data Grid

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
ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Static Variable and Data Grid

Post by ARAS » Sun Dec 08, 2013 7:30 pm

Hi,

Does anyone know how to create a static variable?

I want to set a variable to a specific number for the first run but also increment the value on every run.

let's say. I have a variable called tCount, and it is 0 at the beginning. At the end of my code, I write "put tCount + 1 into tCount". Thus, it will be 1. However, it shouldn't be zero again when I run the code again. It should be 2 at the second run.

I hope I am clear,

Regards,
ARAS
Last edited by ARAS on Sun Dec 08, 2013 9:23 pm, edited 1 time in total.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Static Variable

Post by Klaus » Sun Dec 08, 2013 7:37 pm

Hi Aras,

make it a global variable!

Important: Remember to declare it in every SCRIPT you use it!

Example In a script, so it will be available to all its handlers and functions:

Code: Select all

global my_static_var

on preopenstack
   put 0 into my_static_var
  ### more stuff...
end preopenstack

on openstack
  dowhatever...
end openstack

on opencard
  add 1 to my_static_var
end opencard

command myhandler
  add 25 to my_static_var
end myhandler

etc...
...
In a single handler script like "mouseup"

Code: Select all

on mouseup
  global my_static_var
  answer my_static_var
end mouseup
You get the picture :D


Best

Klaus

ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Re: Static Variable

Post by ARAS » Sun Dec 08, 2013 8:39 pm

Thanks Klaus,

It works but not like I thought.

I have a datagrid in my project. I want to enter datas into the datagrid. Whenever I try to enter a new entry. It replaces with the old one. I want the second entry or the former entry to go to the next row.

This is my code.

Code: Select all

on mouseUp
   global tCount
   --answer tCount
   put field "fName" into theDataA[ tCount ][ "Name" ]
   put field "fSurname" into theDataA[ tCount ][ "Surname" ]
   if the hilite of btn "rMale" then
      put "Male" into theDataA[ tCount ][ "Sex" ]
      --put the label of button "rMale" into theDataA[ 1 ][ "Sex" ]
   else if the hilite of btn "rFemale" then
      put "Female" into theDataA[ tCount ][ "Sex" ]
   else
      --answer "You have to choose the sex of the person you are registering!" with "Okay"
   end if
   
   put the label of button "cDay" into sD
   put the label of button "cMonth" into sM
   put the label of button "cYear" into sY
   put sD & space & sM & space & sY into theDataA[ tCount ][ "Birth Date" ]
   
   set the dgData of group "dgDisplay" to theDataA
   answer "Data entry has been succesfuly done." with "Okay"
   
   put tCount + 1 into tCount
   
   
   //CLEAR SECTION
    put empty into field "fName"
   put empty into field "fSurname"
   --set the hilite of btn "rMale" to false
   --set the hilite of btn "rFemale" to false
   set the hilitedbuttonname of group "gSex" to false
   set the label of btn "cDay" to line 1 of the text of btn "cDay"
   set the label of btn "cMonth" to line 1 of the text of btn "cMonth"
   set the label of btn "cYear" to line 1 of the text of btn "cYear"
end mouseUp

Thanks,
ARAS

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Static Variable and Data Grid

Post by Klaus » Sun Dec 08, 2013 9:57 pm

Hi Aras,

well, this line:
...
set the dgData of group "dgDisplay" to theDataA
...
will always REPLACE the complete content of the datagrid!
You want to append your data to the existing data, right?

In that case you need to do this, this is how datagrids work,
and that eliminates the need of your global variable tCount completely 8)

Code: Select all

---
## NO index in the array that you want to append!
## The Datagrid will take care of this itself!
put field "fName" into theDataA["Name" ]
put field "fSurname" into theDataA["Surname" ]
if the hilite of btn "rMale" then
      put "Male" into theDataA["Sex" ]
else if the hilite of btn "rFemale" then
      put "Female" into theDataA[ "Sex" ]
   else
    --answer "You have to choose the sex of the person you are registering!" with "Okay"
end if  
put the label of button "cDay" into sD
put the label of button "cMonth" into sM
put the label of button "cYear" into sY
put sD & space & sM & space & sY into theDataA[ "Birth Date" ]

## Let the datagrid do the rest:
dispatch "addData" to group "dgDisplay" with theDataA
...
Best

Klaus

ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Re: Static Variable and Data Grid

Post by ARAS » Sun Dec 08, 2013 10:52 pm

Thanks Klaus,

You are the hero of the day! :)

Could you explain what exactly dispatch does?

Best wishes,
ARAS

ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Re: Static Variable and Data Grid

Post by ARAS » Sun Dec 08, 2013 10:58 pm

Oh I've just realized that when I delete all the data in a row and enter a new entry. I continues from the following row not from the empty row. Do you have any solution in mind?

ARAS

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Static Variable and Data Grid

Post by Klaus » Sun Dec 08, 2013 11:10 pm

Hi Aras,
ARAS wrote:You are the hero of the day! :)
I know, I know... :D
ARAS wrote:Could you explain what exactly dispatch does?
I think i cannot explain this better than the dictionary 8)
ARAS wrote:Oh I've just realized that when I delete all the data in a row and enter a new entry. I continues from the following row not from the empty row. Do you have any solution in mind?
Ah, sorry, I did not know that you want to replace the LINE tCount in your datagrid!
Then use your GLOBAL tCount, still no INDEX in the array to add and replace "dispatch..." with this line:
...
set the dgDataOfLine[tCount] of group "dgDisplay" to theDataA
...

I hope this is what you are after :)


Best

Klaus

ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Re: Static Variable and Data Grid

Post by ARAS » Mon Dec 09, 2013 7:20 pm

Hi Klaus,

If I use global variable method, it will increment the value in every run.

Let's say. We have 15 rows which have data in it, and I delete all the data in row 9. Thus I will have 14 in total. However, my global variable will be 16(instead of 15), so it will leave a blank row. Row number 15 will be empty.

I wish I knew a code that finds the empty rows and put the data there.

Anyway, the code you have given me before without a global variable works fine for me. I was just trying to learn about data grid.

Thanks again,
ARAS

Post Reply