Page 1 of 1

for in

PostPosted: Sun May 28, 2017 2:47 am
by Lena
Hi.
Please tell me how it will be in the ะก++ language?
Code: Select all
procedure TFormMain.FormActivate(Sender: TObject);
var
  CurNotification : TPushServiceNotification;
begin
  if Length(fPushService.StartupNotifications) > 0 then
    for CurNotification in fPushService.StartupNotifications do     
       if Assigned(CurNotification) then
         Memo.Lines.Text := CurNotification.Json.ToJSON;
end;

Re: for in

PostPosted: Thu Jun 01, 2017 4:59 pm
by rlebeau
StartupNotifications is a dynamic array. Delphi's 'for..in' syntax has built-in support for iterating arrays:

Iteration Over Containers Using For Statements

In C++, you can loop through the array normally using indexes:

Code: Select all
void __fastcall TFormMain::FormActivate(TObject *Sender)
{
    DynamicArray<TPushServiceNotification*> StartupNotifications = fPushService->StartupNotifications;
    for (int i = StartupNotifications.Low; i <= StartupNotifications.High; ++i)
    {
        TPushServiceNotification *CurNotification = StartupNotifications[i];
        if (CurNotification)
            Memo->Lines->Text = CurNotification->Json->ToJSON();
    }
}


Or, if you are using a CLang-based compiler, you might try using a C++11 for-range loop:

C++11 Features Supported by RAD Studio Clang-enhanced C++ Compilers | Range-based for

Code: Select all
void __fastcall TFormMain::FormActivate(TObject *Sender)
{
    for (auto CurNotification : fPushService->StartupNotifications)
    {
        if (CurNotification)
            Memo->Lines->Text = CurNotification->Json->ToJSON();
    }
}

Re: for in

PostPosted: Fri Jun 02, 2017 8:18 am
by Lena
Tnak You very much!