Gavrilo Adamovic
Gavrilo Adamovic

Reputation: 2795

Accessing document files from dialog class in mfc, sdi

I am new to mfc, so I don't know if I will explain my problem correctly but I'll try.

So I built a puzzle game in mfc, and I want to implement high score system. When the game is over, the dialog pops up, where you put your name, and name is written in the external txt file. So, I have Dialog class, where I implement stuff about putting in your name, and sending it to a txt file, but the problem is that I can't access the info about the score, which is stored in the ProjectDoc class, so I can't link the name of the player and the score.

So the question is how to access files from ProjectDoc class from dialog class.

Upvotes: 0

Views: 298

Answers (2)

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

The solution offered by IInspectable and thomiel works great if you have one (or a few) parameter.

Another extreme would be to pass a pointer to the Document and let the Dialog pull whatever it needs out of it, but that would violate "need to know" policy.

I would suggest to define an interface (abstract class) IHighScoreProvider with required accessors, for example:

class IHighScoreProvider
{
public:
    virtual int GetGameScore() = 0;
    virtual std::string GetPlayerName() = 0;
};

Then derive your Document from it and implement those methods. And pass that interface pointer to your dialog.

Upvotes: 2

thomiel
thomiel

Reputation: 2937

Submit the score as parameter in the constructor of you dialog class:

CHighscoreDlg::CHighscoreDlg(int score)
{
    m_score = score;   // store in private class member variable
}

...
...
void CPuzzleView::EndGame() 
{
    CHighscoreDlg hs(GetDocument()->m_gamescore);
    hs.DoModal();
}

Upvotes: 1

Related Questions