Escolar Documentos
Profissional Documentos
Cultura Documentos
http://msdn.microsoft.com/en-us/library/fc77hxyh
Note
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.
Note
Always specify the name of a new project when you create it. Doing so sets the root namespace, assembly name, and project name, and also ensures that the default component will be in the correct namespace. 2. In Solution Explorer, right-click CDemoLib and select Properties from the shortcut menu. Notice that the Default namespace box contains CDemoLib. The root namespace is used to qualify the names of components in the assembly. For example, if two assemblies provide components named CDemo, you can specify your CDemo component using CDemoLib.CDemo. Close the dialog box. 3. From the Project menu, choose Add Component. 4. In the Add New Item dialog box, select Component Class and type CDemo.cs in the Name box. Click Add to create the component. A component named CDemo is added to your class library. 5. In Solution Explorer, right-click CDemo.cs and choose View Code from the shortcut menu. The code editor opens. Notice the : Component immediately after public partial class CDemo. This section designates the class from which your class inherits. By default, a component inherits from the Component class provided by the system. The Component class provides many features for your component, including the ability to use designers. 6. In Solution Explorer, right-click Class1.cs and choose Delete. This deletes the default class that is provided with the class library, as it will not be used in this walkthrough. 7. From the File menu, choose Save All to save the project.
To add code for the constructor and destructor of the CDemo class
1. In the Code Editor, add member variables to keep a running total of instances of the CDemo class, and an ID number for each instance.
public readonly int InstanceID; private static int NextInstanceID = 0; private static long ClassInstanceCount = 0; Because the InstanceCount and NextInstanceID member variables are declared static, they exist only at the class level. All instances of CDemo that access these members will use the same memory locations. Static members will be initialized the first time the CDemo class is referred to in code. This could be the first time a CDemo object is created, or the first time one of the static members is accessed. 2. Locate public CDemo() and public CDemo(IContainer container), the default constructors for the CDemo class. In Visual C#, all constructors have the same name as the class. Your component can have several constructors, with different parameters, but they must all have the same name as your component.
Note
The access level of the constructors determines which clients will be able to create instances of the class. 3. Add the following code to public CDemo(), to increment the instance count when a new CDemo is created, and to set the instance ID number.
Note
Always add your code after the call to InitializeComponent. At that point, any constituent components have been initialized.
InstanceID = NextInstanceID ++; ClassInstanceCount ++; As a readonly member, InstanceID can be set only in the constructor.
Note
1 of 4
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/fc77hxyh
Users familiar with multithreading will point out quite rightly that assigning InstanceID and incrementing NextInstanceID should be an atomic operation. This and other issues related to threading are illustrated in Walkthrough: Authoring a Simple Multithreaded Component with Visual C#. 4. Add the following method after the end of the constructor:
~CDemo() { ClassInstanceCount --; } This method is called a destructor, and is signified by the tilde character (~) in front of the class name. The memory manager calls the destructor just before it finally reclaims memory occupied by the CDemo object. By implementing a destructor you can perform cleanup just before your component is removed from memory. However, as you'll see later in this walkthrough, there are good reasons to release resources earlier.
To add the CDemoTest client project as the startup project for the solution
1. From the File menu, point to Add and choose New Project to open the Add New Project dialog box. 2. Select the Windows Application project template, and type CDemoTest in the Name box and then click OK. 3. In Solution Explorer, right-click CDemoTest and click Set as Startup Project from the shortcut menu. In order to use the CDemo component, the client test project must have a reference to the class library project. After adding the reference, it is a good idea to add a using statement to the test application to simplify use of the component.
using CDemoLib; Adding the using statement allows you to omit the library name, and refer to the component type as CDemo. You will now create and use a test program to test your component.
this.Text = "CDemo instances: " + CDemo.InstanceCount; On every tick of the timer, the form's caption will display the current instance count for the CDemo class. The class name is used as a qualifier for the static InstanceCount property there is no need to create an instance of CDemo to access a static member. 4. Locate the constructor for Form1 (public Form1()), and add the following code after the call to InitializeComponent().
2 of 4
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/fc77hxyh
timer1.Enabled = true; This will start the timer as soon as the form is created. 5. Click the Form1.cs [Design] tab to return to the designer. 6. Double-click the Button on Form1, to create an event-handling method for the button's Click event. Place the following code in the event-handling method.
CDemo cd; int ct; for (ct = 0; ct < 1000; ct++) cd = new CDemo(); This code may look strange to you. As each instance of CDemo is created, the previous instance is released. When the for loop is done, there will be only one instance of CDemo left. When the event-handling method exits, even that instance will be released, because the variable cd will go out of scope. As you may have guessed already, things won't happen quite that way.
Note
If you've clicked more than 10 times, and the number of CDemo instances has not decreased, you may need to adjust the code so that it uses more memory. Close the form to return to the development environment, and increase the number of iterations in the for loop to 10000. Then run the project again. 4. Repeat Step 3. You will get farther this time before the memory manager finalizes more objects. In fact, each time you repeat step 3, you will probably be able to allocate more CDemo objects before the memory manager steps in. This is because more and more of Visual Studio is swapped out, leaving more space for instances of CDemo. 5. Close the form to return to the development environment.
See Also
Other Resources Programming with Components Component Authoring Walkthroughs
Yes
No
Community Content
Not for Beginners
This was perplexing to me too. I guess the difficulty is in not having a grasp of what a class is. So this tutorial is not basic enough. It is above the beginner. I figured out where to put the constructor through trial and error. This allowed me to better understand a class. The other insertions were then much more understandable. 11/1/2011 Stephen R. Wilson
Code?
Useless to me without the full code showing what goes where. I like to dive in the deep end. Here I just sink. Frustrating. 10/17/2011 William R. Boyd
3 of 4
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/fc77hxyh
[tfl - 09 08 10] Visual Studio : SQL Server : .NET Framework : PowerShell : All Public :
Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communit http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C& http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C& http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1 http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
7/28/2010 lowzhien
4 of 4
5/25/2012 11:39 AM