-2

I wanna know how to write the following codes in C++ Builder by using With Do Begin statement similar to Delphi.

I tried with ComboBox->Text .... do ... try and it's not working. I tried with just do ComboBox->Text .... try, also not working.

if (ComboBox->Text.operator==(String("C++ Builder XE7")))
  {
  try
    {

     // do something

   if ((Form1->Memo1->Lines->Text).Pos("<") !=0)
      {

      // do something 

      }
    }
 catch(Exception &ex)
  {
   ShowMessage(ex.ToString());
  }


if (ComboBox->Text.operator==(String("C++ Builder XE8")))
  {
  try
    {

     // do something

   if ((Form1->Memo1->Lines->Text).Pos("<") !=0)
      {

      // do something 

      }
    }
 catch(Exception &ex)
  {
   ShowMessage(ex.ToString());
  }
July Elizabeth
  • 105
  • 1
  • 11
  • 1
    C++ doesn't have `with` statement. If you seek for _exception safety_ in C++, you should look at [RAII](https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) – myaut Jul 20 '15 at 19:22
  • 1
    @myaut: Delphi's `with` statement has nothing to do with exception handling. Maybe you are thinking of .NET's `using` statement? – Remy Lebeau Jul 20 '15 at 19:26
  • Is this C++? What are all of those objects? Why are you calling `operator==()` rather than just `==`? There's not a lot for readers to work with, or simply understand, here. – underscore_d Jul 20 '15 at 19:30
  • @underscore_d: yes, this is C++ (Embarcadero's flavor of it, anyway). You are correct about the `operator==` misuse, though. – Remy Lebeau Jul 20 '15 at 19:32
  • @July: `with ComboBox.Text .... do ... try` does not make sense even in Delphi, as you cannot use a `String` as an argument for `with`, you will get a compiler error: `[DCC Error] E2018 Record, object or class type required.` – Remy Lebeau Jul 20 '15 at 19:35
  • You should've come to Lounge in the chat room section if you wanted to ask, "What is similar to `with` in C++?" – CinchBlue Jul 20 '15 at 19:44
  • Thank you guys. The following Remy's answer resolved my question. – July Elizabeth Jul 20 '15 at 20:49

1 Answers1

1

There is no equivalent to Delphi's with statement in C++. The best you can do in C++ is use pointers/references instead, eg:

TComboBox *cb = ComboBox;
TStrings *lines = Form1->Memo1->Lines;

if (cb->Text == "C++ Builder XE7")
{
    try
    {
        // do something

        if (lines->Text.Pos("<") != 0)
        {
            // do something 
        }
    }
    catch(const Exception &ex)
    {
        ShowMessage(const_cast<Exception&>(ex).ToString());
    }
}

if (cb->Text == "C++ Builder XE8")
{
    try
    {
        // do something

        if (lines->Text.Pos("<") != 0)
        {
            // do something 
        }
    }
    catch(const Exception &ex)
    {
        ShowMessage(const_cast<Exception&>(ex).ToString());
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • `@Remy:` So i'm getting some error message while using `catch(const Exception &ex)`. These errors are `E2522 Non-const function _fastcall Exception::ToString() called for const object` and `E2285 Could not find a match for 'operator UnicodeString::=(TByteDynArray)'`. So by using `the catch(Exception &ex)` doesn't cause any error in my C++ Builder XE8. Thank you very much, Remy. – July Elizabeth Jul 20 '15 at 21:06
  • 1
    For performance and safety, you should always catch exception objects by const reference. However, if you need to call a `const` method on them, like `ToString()`, you can use `const_cast` for that. I have updated my answer to show that. – Remy Lebeau Jul 20 '15 at 21:18
  • It's fully working now :-). Thank you very much, Remy. :-) – July Elizabeth Jul 20 '15 at 21:23
  • @RemyLebeau have you got any evidence to back up the claim about catching exceptions by const reference? Exceptions are modifiable lvalues; in standard C++ [there is nothing wrong](http://stackoverflow.com/a/18402039/1505939) with catching by non-const reference. (Not sure if Embarcadero does something non-conforming) – M.M Jul 20 '15 at 21:32
  • IMO it would be better style to simply catch by non-const; or to do the conversion in a different way that does not involve calling a non-const member function on the exception object. – M.M Jul 20 '15 at 21:51
  • @MattMcNabb: your own link has several answers that state catching by const reference allows the compiler to optimize the catch since it knows the exception object will not be modified. Embarcadero is no different in that regard. – Remy Lebeau Jul 20 '15 at 22:02
  • @RemyLebeau I don't think those statements about optimization are correct. Everyone seems to agree that the object may still be modified (via const_cast) even if it is caught by const reference. – M.M Jul 22 '15 at 03:38