Моя переменная не меняется (пространство имен / класс) - PullRequest
0 голосов
/ 31 октября 2019

Я пытаюсь передать переменную из текстового ввода в Windows Form App, используя «пространство имен» и «класс», но переменная не получает модификации. Он остается по умолчанию (transp.transported = 0 в файле .cpp). Я попробовал все, но ничего не работает. Если это невозможно исправить, могу ли я взять значение из textBox1 непосредственно в файле .cpp? Спасибо!

.cpp

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include "Form1.h"

using namespace System;
using namespace System::Windows::Forms;
using namespace std;

transp transporting;

string Globals::transporter0;
string Globals::transporter1;

[STAThread]
void main()
{
    string tester123 = to_string(transporting.transported);
    Globals::transporter0 = tester123;
    Globals::transporter1 = "cybersecurity";
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    PasswordEncoder::Form1 form;
    Application::Run(%form);
}

.h file

#pragma once


#include <atlstr.h>
#include <stdio.h>
#using <mscorlib.dll>
#include <stdio.h>
#include <string.h>
#include <msclr/marshal_cppstd.h>

namespace Globals {
    extern std::string transporter0;
    extern std::string transporter1;
    extern std::string testTransporter;
    typedef struct {
        std::string service;
        std::string username;
        std::string password;
    }account;
    extern account accounts[50];
}

class transp {
public: int transported;
};

namespace PasswordEncoder {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
        }

    protected:
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^ label1;
    private: System::Windows::Forms::TextBox^ textBox1;
    private: System::Windows::Forms::TextBox^ textBox2;


    private:


    private: System::Windows::Forms::Button^ button1;
    private: System::Windows::Forms::Label^ label2;
    private: System::Windows::Forms::Label^ label3;
    private: System::Windows::Forms::PictureBox^ pictureBox1;

    protected:

    protected:

    private:
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
            // ... componens declaration
        }
#pragma endregion
String(Globals::testTransporter.c_str());
    public: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
        transp transporter123;
        transporter123.transported = 5;
        msclr::interop::marshal_context context;
        std::string checker1 = context.marshal_as<std::string>(textBox1->Text);
        std::string checker2 = context.marshal_as<std::string>(textBox2->Text);
        int checker3 = Globals::transporter0.compare(checker1);
        int checker4 = Globals::transporter1.compare(checker2);
        if ((checker3 == 0) && (checker4 == 0)) {
            MessageBox::Show("Logged");
        }
        else {
            MessageBox::Show("Wrong!");
        }
    }
};
}
...