Instantiating Objects from Classes

After you obtain a reference to an object and assign it to a variable, you can manipulate the object using an object variable. You're going to do this now.

Click the Form1.cs Design tab to view the form designer and add a button to the form by double-clicking the Button item in the toolbox. Set the button's properties as follows:

Property Value
Name btnCreateObject
Location 104,120
Size 88,23
Text Create Object

Next, double-click the button to access its Click event and enter the following code:

clsMyClass objMyObject = new clsMyClass();
MessageBox.Show(objMyObject.AddTwoNumbers(1,2).ToString());

The first statement creates a variable of type clsMyClass. (Declaring variables was discussed in Hour 12, “Using ...

Get Sams Teach Yourself C#™ in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.