Avatar of QC20N
QC20N
Flag for Denmark asked on

The proper way

I have this code that I hope some can help me to write proper way. The code does what it should do, but I'm not sure if this is the proper way. Hopefully some of it could be rewrite to a function.

Thank you
procedure TForm1.ADInfoUpdate;
var User : IADsuser; LineList : TStringList; sADPath, sdesc : string; Query : TADOQuery; VarArray, Value : Variant; i, HighBound : integer; Updated: boolean;
begin
  Updated := false;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add('SELECT REASON, ADPATH FROM USERINACTIVE WHERE ADUPDATE = :ADUpdate');
  ADOQuery1.Parameters.ParamByName('ADUpdate').Value := False;
  ADOQuery1.Open;
  try
    while not ADOQuery1.Eof do
    begin
      try
        if SUCCEEDED(ADsGetObject(ADOQuery1.FieldByName('ADPATH').AsString, IADsUser, User)) then
        begin
          LineList := TStringList.Create;
          LineList.Delimiter := ',';
          LineList.CaseSensitive := True;
          try
            sdesc := User.Description;
            try
              LineList.DelimitedText := Uppercase(StringReplace(sDesc,' ','_',[rfReplaceAll]));
              for I := 0 to LineList.Count - 1 do
              begin
                if pos('BOSS',LineList[i]) <> 0 then
                begin
                  LineList[i] := StringReplace('BOSS: ' + ADOQuery1.FieldByName('REASON').AsString,' ','_',[rfReplaceAll]);
                  Updated := true;
                end;
              end;
              for I := 0 to LineList.Count - 1 do
              begin
                if pos('_',LineList[i]) = 0 then
                  LineList[i] := QuotedStr(LineList[i])
                else
                  LineList[i] := StringReplace(LineList[i],'_',' ',[rfReplaceAll]);
              end;
              User.Description := Linelist.DelimitedText;
              User.SetInfo;
            finally
              User := nil;
              LineList.Free;
            end;
          except
            user.Description := '"BOSS: ' + ADOQuery1.FieldByName('REASON').AsString+'"';
            User.SetInfo;
          end;
        end;
      except
        on EOleException do
        begin
          // ShowMessage('Test');
        end;
      end;
      ADOQuery1.Next;
    end;
  finally
    ADOQuery1.Close;
  end;
end;

Open in new window

Delphi

Avatar of undefined
Last Comment
Ephraim Wangoya

8/22/2022 - Mon
systan

The code just viewed fine,
except if you want to generalized the procedure or function
procedure TForm1.ADInfoUpdate(var User : IADsuser; sADPath, sdesc : string );
var LineList : TStringList;  Query : TADOQuery; VarArray, Value : Variant; i, HighBound : integer; Updated: boolean;
...
...
ASKER CERTIFIED SOLUTION
Ephraim Wangoya

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
QC20N

ASKER
@ewangoya:

I see your point. Nice.

I know I sad my code runs as it should be, but when I run my code agan I see a small problem.

My startpoint in the description is:
"VACATION","BOSS: VACATION","TEST"

but the endresult is:
'VACATION',"BOSS: VACATION LEAVE",'TEST'

I can't figure out why every word isen't enclosed in quotation marks.

I hope this isen't to much off topic
Ephraim Wangoya


Double quotes is only used when you have spaces in your string
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy