2

I have created empty C++/CLI form in Microsoft Visual Studio.

What I want to do is to keep car brands in one array and car models in other array. Then I will draw one brand, to put it on label, one car model that is from that brand and 3 other car models from other brands.

In my form i want to create 2 arrays:

String brands[7]={"Mercedes","Opel","Toyota","Fiat","Audi","Renault","Volkswagen"};
String models[7][5]={{"Benz","Vito","AMG","Klasa A","Klasa E"},
                     {"Astra","Corsa","Insignia","Zafira","Mokka"},
                     {"Avensis","Corolla","Yaris","Auris","RAV4"},
                     {"126p","Panda","Punto","500","Tipo"},
                     {"A4","A6","Q7","R8","A7"},
                     {"Megane","Captur","Scenic","Kadjar","Espace"},
                     {"Golf","Passat","Tiguan","Beetle","Touran"}};

I can't put it anywhere in my MyForm.h file, I am getting E2022 error. What simple solution would you suggest me?

#pragma once

namespace Project1 {

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

    /// <summary>
    /// Podsumowanie informacji o MyForm
    /// </summary>
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            //
            //TODO: W tym miejscu dodaj kod konstruktora
            //
        }


    protected:
        /// <summary>
        /// Wyczyść wszystkie używane zasoby.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected:
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::Button^  button3;
    private: System::Windows::Forms::Button^  button4;
    private: System::Windows::Forms::Button^  button5;
    private: System::Windows::Forms::Label^  label1;
    String marki[7]={"Mercedes","Opel","Toyota","Fiat","Audi","Renault","Volkswagen"};
    String modele[7][5]={{"Benz","Vito","AMG","Klasa A","Klasa E"},
                     {"Astra","Corsa","Insignia","Zafira","Mokka"},
                     {"Avensis","Corolla","Yaris","Auris","RAV4"},
                     {"126p","Panda","Punto","500","Tipo"},
                     {"A4","A6","Q7","R8","A7"},
                     {"Megane","Captur","Scenic","Kadjar","Espace"},
                     {"Golf","Passat","Tiguan","Beetle","Touran"}};

    private:
        /// <summary>
        /// Wymagana zmienna projektanta.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Wymagana metoda obsługi projektanta — nie należy modyfikować 
        /// zawartość tej metody z edytorem kodu.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->button3 = (gcnew System::Windows::Forms::Button());
            this->button4 = (gcnew System::Windows::Forms::Button());
            this->button5 = (gcnew System::Windows::Forms::Button());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->SuspendLayout();
            // 
            // button1
            // 

            this->button1->Location = System::Drawing::Point(12, 108);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(147, 23);
            this->button1->TabIndex = 1;
            this->button1->UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this->button2->Location = System::Drawing::Point(178, 108);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(146, 23);
            this->button2->TabIndex = 1;
            this->button2->UseVisualStyleBackColor = true;
            // 
            // button3
            // 
            this->button3->Location = System::Drawing::Point(12, 183);
            this->button3->Name = L"button3";
            this->button3->Size = System::Drawing::Size(147, 23);
            this->button3->TabIndex = 1;
            this->button3->UseVisualStyleBackColor = true;
            // 
            // button4
            // 
            this->button4->Location = System::Drawing::Point(178, 183);
            this->button4->Name = L"button4";
            this->button4->Size = System::Drawing::Size(146, 23);
            this->button4->TabIndex = 1;
            this->button4->UseVisualStyleBackColor = true;
            // 
            // button5
            // 
            this->button5->Location = System::Drawing::Point(178, 250);
            this->button5->Name = L"button5";
            this->button5->Size = System::Drawing::Size(146, 23);
            this->button5->TabIndex = 1;
            this->button5->Text = L"Nastepne";
            this->button5->UseVisualStyleBackColor = true;
            this->button5->Click += gcnew System::EventHandler(this, &MyForm::button5_Click);
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(140, 28);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(0, 13);
            this->label1->TabIndex = 2;
            // 
            // MyForm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(361, 306);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->button5);
            this->Controls->Add(this->button4);
            this->Controls->Add(this->button3);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Name = L"MyForm";
            this->Text = L"QUIZ";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion

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

}
};
}
5
  • 2
    What message of E2022 is? I do not want to google it. Commented Jun 15, 2018 at 13:41
  • Need more context, please create an minimal reproducible example. Commented Jun 15, 2018 at 13:56
  • Is the error during compilation or run-time? Commented Jun 15, 2018 at 13:56
  • How to: Use Arrays in C++/CLI. Commented Jun 15, 2018 at 14:06
  • Added full header code Commented Jun 15, 2018 at 14:07

1 Answer 1

1

Here is the way to do:

First declare your arrays under protected section (as you did there).

array<String^>^ marki;
array<String^,2>^ modele; //Note the declration 2 next to String^. It is rank of the array. 2 for two dimensional.

And in your constructor (MyForm() ) intialize your arrays as shown below.

//SINGLE DIMENSION ARRAY
marki = gcnew array<String^> {"Mercedes","Opel","Toyota","Fiat","Audi","Renault","Volkswagen"};

//MULTI DIMENSIONAL ARRAY
modele = gcnew array<String^,2> {{"Benz","Vito","AMG","Klasa A","Klasa E"},
                     {"Astra","Corsa","Insignia","Zafira","Mokka"},
                     {"Avensis","Corolla","Yaris","Auris","RAV4"},
                     {"126p","Panda","Punto","500","Tipo"},
                     {"A4","A6","Q7","R8","A7"},
                     {"Megane","Captur","Scenic","Kadjar","Espace"},
                     {"Golf","Passat","Tiguan","Beetle","Touran"}};
        }

Testing:

String^ test = modele[2,4]; //RAV4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.