Click to See Complete Forum and Search --> : calling method


leguignol
December 24th, 2005, 05:28 AM
Hello everybody!

I have a question on VC++.NET.
I have a project with a form with controls etc. This form lays in Form1.h. I have also an own class named Music which is in the file Music.h. Both are allocated in the same namespace. In Form1.h I included "#include Music.h" and in Music.h I included "#include Form1.h". In Form1.h I have a public method Play. When instancing an object of type Music I want to call the Play-method in Form1.h right out of constructor of Music-class. But that doesn't work. I tried to fix that with different notation like Form1::Play but nothing did work. What is the solution? Even if I declare an control on Form1 as public I cannot access it from Music.h.
I want to do this project in an very OOP way, so I came to think about events, but I don't have clue how to implement them, how to fire them and how to receive them in Form1.h.
Maybe you haven an idea how I could access Play-method. I'm very thankful for an advice.

David

NoHero
December 24th, 2005, 05:30 AM
You could post your code - but please with code tags - which would be very helpful when finding your bug/problem...

leguignol
December 24th, 2005, 05:56 AM
This is the content of Form1.h:
#pragma once
#include "Music.h"

namespace eMusicSheet
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::IO;

public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void) {
InitializeComponent();
Music* Test = new Music(S"Kristall.pdf"); }

void Play(String* s) {
MessageBox::Show(S"Started playing..."); }

protected:
void Dispose(Boolean disposing) /*...*/
private:
void InitializeComponent(void) /*...*/
};
}

This is the content of Music.h:
#pragma once
#include "Form1.h"

namespace eMusicSheet
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Data;

public __gc class Music
{
public:
Music() {
Form1->Play(); } //this doesn't work!!
};
}

Ok, that should be all. It came just right now to me: Maybe to put Music into another namespace name "nsMusic" and include in Form1.h "using namespace nsMusic;" ?!
Thanks for looking at it! David

NoHero
December 24th, 2005, 08:26 AM
Music* Test = new Music(S"Kristall.pdf");

The class music does not have a constructor defined which takes a String* as parameter. Create one or cut the String* parameter out. This shouldn't work either, or you haven't posted the full code ;)

Music()
{
Form1->Play();
} //this doesn't work!!

You need an object to access non-static methods of a class. So either pass an object to your music class, or make any method you need static.

Music ( Form1 *form )
{
form->Play();
}

and may be called by:

#pragma once
#include "Music.h"

namespace eMusicSheet
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::IO;

public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void) {
InitializeComponent();
Music* Test = new Music(this); }

void Play(String* s) {
MessageBox::Show(S"Started playing..."); }

protected:
void Dispose(Boolean disposing) /*...*/
private:
void InitializeComponent(void) /*...*/
};
}

The code above gives the current object to the constructor of the Music class, which itself calls the play method of this class.