How does this statement works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How does this statement works?

I don't understand the syntax of this code: private: System::Void entering_a_number(System::Object^ sender, System::EventArgs^ e) { Button^ number_you_clicked = safe_cast<Button^>(sender); // this statement is so confusing to me, I read the document on MS site but still can not understand. display->Text = ( display->Text == "0" ) ? number_you_clicked->Text : display->Text + number_you_clicked->Text; } If someone can explain this in simple english. Thank in advance.

4th May 2021, 1:25 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
1 Answer
+ 2
Precaution: I have no experience in Visual C++, so I will try to explain in general. `entering_a_number` is a void function (returns nothing), it's an event handler that uses two arguments (`System::Object` <sender> and `System::EventArgs` <e>). <sender> is an object that represents the UI control that triggers the event, <e> carries additional information regarding the event (usually be different for each event type). Button^ number_you_clicked = safe_cast<Button^>(sender) Means instantiate an object (a `Button` named <number_you_clicked>) that is generated by casting <sender> which is a general `System::Object` into a `Button` object (specific type). The lines underneath shows a conditional execution flow through the use of ternary operator. display->Text = (display->Text == "0") ? number_you_clicked->Text : display->Text + number_you_clicked->Text; If <display> text was "0", it will be updated to <number_you_clicked> text. Otherwise <number_you_clicked> text is appended to current text. Hth, cmiiw
4th May 2021, 2:13 PM
Ipang