Search code examples
classfunctionvisual-c++clrmanaged

How do I make a class module with one or more functions in Visual C++ 2008 Managed


I am an absolute novice in Visual C++ and hence I have to ask you, how would I create a managed class module (new class) with one or more functions inside of my managed C++ project (Visual Studio 2008)? How would I call the method of the class for example if a button was pressed. I was unable to understand the very complicated tutorials on it and most of the tutorials referring to unmanaged code or older versions of visual studio. My own attempt totally failed and produced only errors, since I found no right options on how I would add a new class file to my Visual C++ managed project. If I choose a new "CLR Component Class" I get a warning message telling me about components filling the right pane of my screen. If I choose a new "CLR Windows Form Class" it happens just nothing, no file with the extension ".class" would be added or I do not know the file which was newly added. I just need a very basic class file with one public function in it which I would be able to call from any location of my project.

I have a very big main() cpp-file already (main.cpp) with lots of functions in it. There are about thousand functions or more, so it becomes difficult to search or scroll. Now I would like to put some of these 1000 functions in a second cpp-file within my current project (in Visual Basic 6 it was simply called a 'new module' in CSharp it is called a 'new class file'). The problem is, that I can't call this functions in Visual C++, once I have moved them out of my main.cpp to module1.cpp for example.

That's what I don't want:

  • a DLL
  • a second project
  • adding a reference to something (in project/references)
  • double declarations

I know there is a way to make just a simple class and then create a new object of this class to use its methods. That's what I want. The examples in Google on this did not work for me, because they were for earlier versions of visual studio and not compatible with my version. From this examples I know what I want, but I haven't got the knowledge to implement it in Visual Studio 2008.


Solution

  • I'm going to try again to post step-by-step instructions on how to add a class file to a VS2008 WinForm project. I again did not realize until completing the list that numbered items don't always work cleanly here. I was able to fix all but one of the numbers, so this should be good to go - David W

    1. These steps assume a simple starting project in VS2008, consisting of a WinForms app with a default form and a single button control, as illustrated here: Starting point of demo project.
    2. To add a new class to this project, right-click on "DemoWinFormApp," select "Add," then select "Class..." as shown below: Add a class
    3. Give the class a name, DemoNewClass, in the "Name" field of the "Add Class" dialog, and click the "Add" button: Add new class name
    4. Visual Studio will add two files to your project: DemoNewClass.h, the header file for definitions, and DemoNewClass.cpp for the actual C++ implementation of the functions defined in the header. Once added, the Visual Studio editor makes the header file active, but displays the following: Editor message after header file added Click the "click here to switch to code view" link to open the source editor.
    5. The editor now displays the source of the DemoNewClass.h header file. This file includes an automatic declaration of the namespace from the main application, DemoWinFormApp, and also contains the two default constructors in the "public:" region, which we will not touch. We will add a new public static method to DemoNewClass immediately after the second constructor definition.

    6. We will add a static method - one that does not require instantiation of the host class - that puts asterisks around a String. The method will be called "DecorateString"

    7. Immediately following the second constructor, add the declaration as illustrated here and highlighted in blue: New function declaration

    8. With the declaration in place, we must now add the implementation. In Solution Explorer, double-click on "DemoNewClass.cpp" to edit the file: Editing implementation file

    9. The editor window opens, and the file has only two lines - two include directives that instruct the compiler to bring in the referenced files as part of the current source. The declaration provided in DemoNewClass.h is provided by default.

    10. Add the implementation of the DecorateString method to the .cpp file as shown here: enter image description here This completes the definition of the new class, the DecorateString declaration, and the provision of the implementation for the DecorateString method. All that remains now is to allow the WinForms app to reference the class and method from the main form.

    11. From the Solution Explorer, double-click the "Form1.h" file in the "Header Files" list of the DemoWinFormApp project, which brings up Form1 in the editor. (The button was already added, and the steps to add the button are left to the reader): enter image description here

    12. To demonstrate the new class method, we'll have the button click display a string modified by our DecorateString method via the MessageBox class. First, we must make the form aware of our new class by supplying an include directive to the new class' header at the top of the Form1.h header file. Right-click on the form in the designer, and click "View Code.." to bring up the source editor enter image description here

    13. To make the class and its methods available to the form, we must supply an include directive for the class' header, DemoNewClass.h, near the top of the Form1.cpp implementation source file: Include directive

    14. From Solution Explorer, double-click on "Form1.h" to open the Form Designer with Form1. Alternatively, if the file is still open in the editor, you could open the Designer by clicking the editor tab labled "Form1.h [Design]"

    15. We will call our new class method from the event handler tied to the "button1" button on the form. Visual Studio provides a default handler by double-clicking "button1" in the designer: button click handler

    16. Type the code to invoke the MessageBox::Show method, passing as a parameter a string modified by the DemoNewClass::DecorateString method, as follows - noting how Intellisense is aware of our new class along the way: enter image description here
    17. And here's the completed event handler/class method call: complete method call
    18. From the Visual Studio "Build" menu, select "Build Solution" (or press F6). This will compile the solution and reveal any syntax errors that may have been detected. The sample, as provided, compiles and builds cleanly.

    Build solution...

    enter image description here 20. With a successful build, all that remains is to test the application. Press F5 to run the application and display the test form:

    enter image description here

    1. Click "button1" to run the handler, and see the decorated string:

    enter image description here

    The passed parameter, "foo", was successfully passed to the new class' DecorateString method, and returned to the MessageBox method for display. The new class method declaration and form reference are now complete.

    Good luck.