Topic: Reseting variables

Hello Dmitry and all MVD fans,


Second question of the day : I handle a lot of data from web pages. After cleaning the page source, I get a lot of data that I check against my database before saving, updating or discarding it.


In that process, after cleaning the webpage source code, I get a long StringList which is then split into an array and then, looping through the array, I send request to the database to check my data.

For each loop, the variables I use are the same (I mean same name), I would like to known what would be the correct way to reinitialize those variables at the begining of each loop.

For example :

sku_array := SplitString(cat_string,',');
     for i := 0 to Length(sku_array) - 1 do
          begin
               known_sku := SQLExecute('SELECT COUNT(id) FROM asset WHERE asset_sku = "'+sku_array[i]+'"');
                    if known_sku = 0 then .....
                                    begin
                                          ..... //some code
                                    end
                    if known_sku <> 0 then
                                    begin
                                          ..... //more awesome code
                                    end;

                    known_sku := NIL; //-----> reset the variable before new loop

          end;

Is it the correct way to reset a variable to make sure it is empty, or is there another / better way ?


Thanks in advance


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Reseting variables

hi Mathias

if you are talking about resetting i, you dont need to if you are going to use it in another 'for' loop. using it in the loop automatically resets it.

lee

Re: Reseting variables

Hello,


No need to reset local variables.

Dmitry.

Re: Reseting variables

Thank you both for your answers.


if I understand well, each loop resets it's own variables (in this case known) so I don't have to reset them manually. That's a relief.

I suppose this is the same on FormClose, the local variables are destroyed ?


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Reseting variables

mathmathou wrote:

Thank you both for your answers.


if I understand well, each loop resets it's own variables (in this case known) so I don't have to reset them manually. That's a relief.

I suppose this is the same on FormClose, the local variables are destroyed ?


Cheers


Mathias

Your variables rewrites each loop.
Also the local variables will be destroyed, when your function is finished. Don't worry about that.


But when you create objects (like TStringList or OpenDialog) they must be free before your function is finished.

Dmitry.

Re: Reseting variables

you probably want to reset a variable if you repurpose it.  for instance if you let t be the total amount of red and green apples in a basket and then further on let t be the total amount of purple and green grapes in a basket. if you dont set t to 0 before the second basket you get a weird tasting smoothie. ;-)

lee