for in

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

for in

Postby Lena » Sun May 28, 2017 2:47 am

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;
Lena
BCBJ Master
BCBJ Master
 
Posts: 524
Joined: Sun Feb 06, 2011 1:28 pm

Re: for in

Postby rlebeau » Thu Jun 01, 2017 4:59 pm

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();
    }
}
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: for in

Postby Lena » Fri Jun 02, 2017 8:18 am

Tnak You very much!
Lena
BCBJ Master
BCBJ Master
 
Posts: 524
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 11 guests

cron