Objective Scope



Objective Scope

Assigning values to object variables

You can assign one object pointer variable to another object pointer variable if the variables are of the same type or assignment compatible, just as you can assign variables of any type other than objects to variables of the same type or assignment-compatible types. For example, if the variables Form1 and Form2 have been declared as pointers to TForm, you could assign Form1 to Form2:

Form2 = Form1;

You can also assign an object pointer variable to another object pointer variable if the type of the variable you are assigning a value to is an ancestor of the type of the variable being assigned. For example, here is a TDataForm class declaration:

class TTDataForm : public TForm

{

__published:     // IDE-managed Components

TButton *Button1;

TEdit *Edit1;

TDBGrid *DBGrid1;

TDatabase *Database1;

private: // User declarations

public:              // User declarations

virtual __fastcall TTDataForm(TComponent* Owner);

};

Here are the declarations of two objects; one of type TForm, and one of type TDataForm:

TDataForm *DataForm;

TForm *AForm;

AForm is of type pointer to TForm, and DataForm is of type pointer to TDataForm. Because TDataForm is a descendant of TForm, this assignment statement is legal:

AForm = DataForm;

You might wonder why this is important. Taking a look at what happens behind the scenes when your application calls an event handler shows you why.

Suppose you write an event handler for the OnClick event of a button. When the button is clicked, the event handler for the OnClick event is called.

Each event handler has a Sender parameter of type pointer to TObject. For example, note the Sender parameter in this empty Button1Click handler:

void __fastcall TForm1::Button1Click(TObject *Sender)

{

}

TObject is at the top of the C++Builder Visual Component Library hierarchy. That means that all C++Builder objects are descendants of TObject. Because Sender is of type TObject, any object can be assigned to Sender. Although you don’t see the code that makes the assignment, the component or control to which the event happened is assigned to Sender. This means the value of Sender is always the control or component that responds to the event that occurred.

How is this information useful? You can test Sender to find the type of component or control that called the event handler. For example:

if (typeid(*Sender) == typeid(TEdit))

DoSomething;

Else  DoSomethingElse;

About the Author

MoreHere

Objective-C Tutorial – Lesson 11: Part 1: Scope of Variables


Leatherwood CMR 1-4x24 Riflescope, Red Circle Dot Reticle, Matte CMR14X24-R


Leatherwood CMR 1-4×24 Riflescope, Red Circle Dot Reticle, Matte CMR14X24-R


$359.00


Leatherwood CMR 1-4×24 Riflescope, Red Circle Dot Reticle, Matte CMR14X24-R…

Carl Zeiss Optical Inc Conquest Riflescope with Reticle 20 Hunting Turret (3.5-10x44 MC)


Carl Zeiss Optical Inc Conquest Riflescope with Reticle 20 Hunting Turret (3.5-10×44 MC)


$636.39


The shortest full-size model in the Conquest line, this lightweight yet powerful all-around scope is suited for use at dawn and early dusk, making it an excellent choice for hunting everything from varmints to whitetail. The scope is outfitted with a robust one-piece tube body that’s designed to stand up to abusive recoil and demanding terrain, so you don’t need to baby it when the conditions get …

The Mi5 Polarizing Binoc scope, Infinity Plan objectives, Halogen illumination


The Mi5 Polarizing Binoc scope, Infinity Plan objectives, Halogen illumination


$3,299.00


The Mi5 Polarizing Binoc scope, Infinity Plan objectives, Halogen illuminationMi5LW Scientific is proud to introduce the new Mi5 microscope, our most advancedmodular microscope to date. The Mi5 was developed for use in a broad range ofapplications from routine research and lab inspections, to more specific healthcareapplications such as pathology, cytology, oncology, hematology, and gynecology.Bri…

The Mi5 Polarizing Trinoc Scope, Infinity Plan objectives, halogen illumination


The Mi5 Polarizing Trinoc Scope, Infinity Plan objectives, halogen illumination


$3,549.00


The Mi5 Polarizing Trinoc Scope, Infinity Plan objectives, halogen illuminationMi5LW Scientific is proud to introduce the new Mi5 microscope, our most advancedmodular microscope to date. The Mi5 was developed for use in a broad range ofapplications from routine research and lab inspections, to more specific healthcareapplications such as pathology, cytology, oncology, hematology, and gynecology.Br…

Tasco Essentials 8x21 Binocular (Black)


Tasco Essentials 8×21 Binocular (Black)


$9.99


These ultra-light binoculars offer outstanding compact performance with rugged rubber armor for hiking backpacking or bicycle touring….

Aim Sports Red Dot Sight with 4 Different Reticles


Aim Sports Red Dot Sight with 4 Different Reticles


$29.95


Tactical Red Dot Open Sight 4 Reticle Patterns To Choose From Weaver Rail Base 7 Breightness Settings Windage And Elevation Adjustments Waterproof And Shockproof Anodized Aluminum Construction Magnification : 1X Objecetive Aperture: 33mm…

Bushnell Medalist Laser Rangefinder


Bushnell Medalist Laser Rangefinder


$449.95


Find the flag and take your aim with accuracy when you use the Bushnell(r) Medalist rangefinder. The 4x-magnification Pinseeker feature lets you zero in on the flag from 300 yds. It also features a rainproof construction for durability in wet conditions….

Burris SUNSHADE FULFIELD II 50MM MAT 3 Reduce / Eliminate Glare On The Bright...


Burris SUNSHADE FULFIELD II 50MM MAT 3 Reduce / Eliminate Glare On The Bright…


$43.60


Burris 3″ Fulfield 50mm Sunshade….

Burris Sunshade-SMag 4.5-14,Cmp 4-12 (2.25 626034


Burris Sunshade-SMag 4.5-14,Cmp 4-12 (2.25 626034


$19.09


Burris Sunshade-SMag 4.5-14,Cmp 4-12 (2.25 inch)…

BUSHNELL 730132P TROPHY 1 X 32 RED DOT RIFLESCOPE


BUSHNELL 730132P TROPHY 1 X 32 RED DOT RIFLESCOPE



Trophy Scope.Engineered with Amber-Bright optics that quickly help you distinguish between a brown tree and a brown critter, the Trophy riflescopes have been heralded by hunters as the ideal scope for the autumn deer woods. With multi-coated optics for increased brightness in low-light conditions, the avid inner hunter in you will be most satisfied. No question about it, they’re designed to excel …


You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.