#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    86
    Rep Power
    13

    Proc fails when moved to another unit...


    Hello, I wonder if you can help?

    I have a procedure I'm trying to implement in another unit, basically to tidy up my code (I'm new to Delphi)

    The part I'm having problems with is the variable type "Tedit" particularly, it works as is till I move it.

    There is no *.dfm attached to the user unit if this makes any difference.

    It's not allowing me to assign "eName,eEmail,Eweddingdate : Tedit" of course from then on it all goes downhill...



    Code:
    unit users;
    
    
    interface
    uses dialogs,SQLite3,SQLiteTable3,SysUtils,  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,Unit1;
    procedure checkforuser();
    implementation
        procedure checkforuser(ename,eWeddingDate,eEmail : Tedit);
        var
        SQL , slDBpath,usr_name,usr_weddingdate,usr_email : string;
        sldb: TSQLiteDatabase;
        sltb: TSQLIteTable;
        begin
        slDBPath := 'data.db';
        sldb := TSQLiteDatabase.Create(slDBPath);
    
        SQL := 'select * from users';
        sltb := sldb.GetTable(SQL);
      while not sltb.EOF do
      begin
          eName.caption := sltb.FieldAsString(sltb.fieldIndex['usr_name']) ;
          eWeddingDate.caption := sltb.FieldAsString(sltb.fieldIndex['usr_weddingdate']) ;
          eEmail.caption := sltb.FieldAsString(sltb.fieldIndex['usr_email']) ;
      sltb.Next;
        end;
        sldb.Free;
        sltb.Free;
        end;
    end.
    Thanking you in advance :-)

    Greg.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    First, when things do not work, you will generally get better and quicker answers if you post the actual error messages you receive.

    The short answer is that you have not added the unit that contains TEdit to your uses clause. I believe, from memory, it is stdctrls.

    More importantly (IMO), as you are new to Delphi, let me say that it would be a much better design to pass the captions in question into the procedure as simple var string parameters and then assign the new values back to the various TEdit captions on returning from the procedure.

    This give you freedom to do what you like with the form (e.g. replace a TEdit with a TPanel - just an example) without the need to touch your procedure in any way.

    HTH - Clive
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    86
    Rep Power
    13
    Hi Clivew, thank you for your help, I appreciate it and will in future post the error messages ( I can't believe I did not this time... )

    2nd: I prefer your logic regarding using string parameters, but I am confused as to how to get them "out" of the procedure... would you simply make them public?

    This I can do but I'm trying to graduate from "hacker" to real coding so any good coding advise is most appreciated.

    Thanks!

    Greg.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    Code:
    procedure checkforuser(var ename,eWeddingDate,eEmail : String);
    implementation
        procedure checkforuser(var ename,eWeddingDate,eEmail : String);
    By declaring the parameters as var you can change them within the procedure and the new values will be retained when the procedure exits.

    Also, I did not notice this before, but the declaration of the procedure in the interface section must match that in the implementation (see above).

    Caution
    You may not be able to pass TEdit.Text into the procedure as a var parameter ( i have not tested), you may need to declare an intermediate string variable.
    Code:
    var
      tmpName: String;
    begin
        tmpName := nameEdit.Text;
        checkforuser(tmpName,.....
        nameEdit.Text := tmpName;
    Clive
    Last edited by clivew; June 26th, 2012 at 01:23 PM. Reason: Added Caution section
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    86
    Rep Power
    13

    What an elegant solution thank you!


    Hi Clive, what an elegant solution!

    I did have to use the tmp_var, it would have been nice to pass the edit field straight in.

    Thank you so much!

    G
    Last edited by dlumley; June 26th, 2012 at 02:41 PM. Reason: To add more comment...

IMN logo majestic logo threadwatch logo seochat tools logo