Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2007

Tip: Looking for answers? Try searching our database.

Microsoft make brain bleed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jeff.sharkfinn@gmail.com - 14 Aug 2007 11:45 GMT
I'm trying to create a pointer to a class in form1.h button1 event
handler. It should be simple... but no. This example is as simple as I
can make:

Form1.h (produced with the designer containing a single button)
***********************************************************************************
#pragma once

namespace MyProject {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    ///
    /// WARNING: If you change the name of this class, you will need to
change the
    ///          'Resource File Name' property for the managed resource
compiler tool
    ///          associated with all .resx files this class depends on.
Otherwise,
    ///          the designers will not be able to interact properly with
localized
    ///          resources associated with this form.
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
//    private:  MyClass^ MyPtr;
    protected:

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            //
            // button1
            //
            this->button1->Location = System::Drawing::Point(91, 65);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(104, 32);
            this->button1->TabIndex = 0;
            this->button1->Text = L"button1";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
            //
            // Form1
            //
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(292, 256);
            this->Controls->Add(this->button1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);

        }
#pragma endregion
#include "MyClass.h"
#pragma once
    private: System::Void button1_Click(System::Object^  sender,
System::EventArgs^  e) {
//            MyClass^ MyPtr = gcnew MyClass();
//                MyPtr->MyMethod();
                MyClass instance;
                instance.MyMethod();
            }
    };
}

************************
At the end here, I want to have the event handler for button 1 execute
MyMethod();

MyClass.h Produced by adding a C++ class
*****************************
#pragma once

ref class MyClass
{
public:
    MyClass(void);
    static void MyMethod(void);
};
***********************************************
MyClass.cpp

#include "StdAfx.h"
#include "MyClass.h"
#include "Form1.h"

using namespace MyProject;

MyClass::MyClass(void)
{
}
void MyClass::MyMethod()
{
}
***************************************************
MyProject.cpp

// MyProject.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace MyProject;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);

    // Create the main window and run it
    Application::Run(gcnew Form1());
    return 0;
}
************************************************************

I don't want to have to put all my executable code in Form1.h
I want to have separate files for unrelated classes and call them from
various event handlers.
The way the files are as shown I get LNK2020 unresolved token.
If I pull the #include "MyClass.h" from Form1.h I get error C2065
undeclared identifier.

I'm fit to be tied. I've spent hours searching for an answer and it
seems to be a common problem world-wide with no resolution. I've tried
all the old fashioned ways too. MyClass* pMyClass = new MyClass();
etc.

Anybody able to help?
TIA
Carl Daniel [VC++ MVP] - 14 Aug 2007 15:16 GMT
> I don't want to have to put all my executable code in Form1.h
> I want to have separate files for unrelated classes and call them from
> various event handlers.
> The way the files are as shown I get LNK2020 unresolved token.

Which symbol is undefined?  Presumably you have both .cpp files in your
project.

> If I pull the #include "MyClass.h" from Form1.h I get error C2065
> undeclared identifier.

Yes, you definitely need the declaration of MyClass to be visible in order
to instantiate it and call a member function on it.

> I'm fit to be tied. I've spent hours searching for an answer and it
> seems to be a common problem world-wide with no resolution. I've tried
> all the old fashioned ways too. MyClass* pMyClass = new MyClass();
> etc.

If you make MyClass a non-CLR class (i.e. class MyClass instead of public
ref class MyClass), then "the old fashioned way" will work just fine.

> Anybody able to help?
> TIA

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.