Search code examples
winformsvariablestextboxc++-cli

A non-static member reference must be relative to a specific object


I'm trying to get the text in my textbox tb_key to write to my std::string Key Variable by doing this:

std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;

I get an error saying :

A non-static member reference must be relative to a specific object

I tried to search but I couldn't find anything really that fixed it for me.

Minimal Reproducible Example:

LoginForm.h

namespace TRIPRECOILWARE {

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



private: System::Void tb_key_TextChanged(System::Object^ sender, System::EventArgs^ e) 
{
}

}

LoginForm.cpp

std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;

I'm trying to use this in LoginForm.h

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    
    if (Authenticate(StringToChar(Key), (StringToChar(hwid)))) // Authenticates key & hwid
    {
        this->Hide();
        Software^ soft = gcnew Software();
        soft->Show();
    }

Basically, I want to get Key from Textbox called tb_key and write it to my Key variable defined above. Then use that key to authenticate and perform code


Solution

  • Your real problem is a duplicate of How to turn System::String^ into std::string?

    Corrected code:

    #include <msclr\marshal.h>
    #include <msclr\marshal_cppstd.h>
    
    using namespace System;
    using namespace msclr::interop;
    
    void button1_Click(System::Object^ sender, System::EventArgs^ e)
    {
        std::string Key = marshal_as<std::string>(tb_key->Text);
        if (Authenticate(Key.c_str(), hwid.c_str())) // Authenticates key & hwid
        {
            Hide();
            Software^ soft = gcnew Software();
            soft->Show();
        }
    }