Search code examples
visual-c++c++-cliwindows-forms-designer

C2227 and C3867 errors in visual c++ Windows Form


I am attempting to read a file in my application and am having a few errors pop up that I don't fully understand.

C2227: left of '->Equals' must point to class/struct/union/generic type C3867: 'System::String:ToString': non-standard syntax; use '&' to create a pointer to member

Here is the code:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    OpenFileDialog^ open = gcnew OpenFileDialog();

    open->InitialDirectory = "c:\\";
    open->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    open->FilterIndex = 2;
    open->RestoreDirectory = true;

    if (open->ShowDialog->Equals(System::Windows::Forms::DialogResult::OK) )
    {
        textBox1->Text = open->FileName;
    }
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    ifstream inFile;

    string time;
    string name;
    Shack shackPref;
    pair<int, int> av;
    string filename = textBox1->Text->ToString;

    inFile.open(filename);

    if (inFile.is_open()) {
        cout << "File has been opened" << endl;
    }
    else {
        cout << "NO FILE OPENED" << endl;
    }

    while (!inFile.eof()) {

    }

Can someone explain why open->ShowDialog does not fall under class/struct/union/generic type? How can I rearrange this to get it to be the correct input? Do I need to create a struct that holds that data and if so will '->Equals' still be able to compare it to the DialogResult?

Same question goes for trying to convert the filename to an std:string from a String^. When I try to treat Text as a member of textBox1 it says that it doesn't have the member. Why doesn't '->ToString' recognize 'Text' as a member?

Thank you in advance for your help. I am still fairly new to c++ so this has been a real brain-cruncher for me.


Solution

  • You're trying to use ShowDialog like a property or member, but it is a method so it needs to be called with parenthesis.

    open->ShowDialog()
    

    The same goes for ToString. It needs to be called as it's a function.

    textBox1->Text.ToString();
    

    But besides, calling ToString() on a String makes no sense. You should be able to use the answers to this question to convert the .net String to a std::string.

    C++ .NET convert System::String to std::string