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 so follow these steps:

1.
Click the Form1.cs Design tab to view the Form Designer.
2.
Add a button to the form by double-clicking the Button item in the toolbox. Set the button's properties as follows:
PropertyValue
NamebtnCreateObject
Location104,120
Size88,23
TextCreate Object
3.
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 ...

Get Sams Teach Yourself Microsoft® Visual C#™ .NET 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.