globales array definieren
-
Hallo,
in einer WinForms Application benötige ich ein Array. Auf die Elemente dieses Arrays soll von verschiedenen Ereignisbehandlungsroutinen aus zugegriffen werden, daher muss das Array global verfügbar sein.
Ich habe jetzt in form1.h zwei verschiedene Möglichkeiten zur Definition eines Arrays ausprobiert:
...
#pragma endregionarray<int> ^mein_array1 = gcnew array<int>(64); /* Variante 1 /
int mein_array2[64]; / Variante 2 */private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
}
};
}Beide Varianten liefern Fehlermeldungen:
error C3845: 'test_23_5_2010::Form1::mein_array1': only static data members can be initialized inside a ref class or value type
error C4368: cannot define 'mein_array2' as a member of managed 'test_23_5_2010::Form1': mixed types are not supportedWas mache ich falsch, wie macht man es richtig? Und insbesondere: Ist das die richtige Stelle, um globale Variable zu definieren?
Gruss
Michael
-
Nimm das erste und initialisiere es im Konstruktor des Forms.
-
Hallo,
witte schrieb:
Nimm das erste und initialisiere es im Konstruktor des Forms.
Gut, das funktioniert soweit. Aber mir ist noch nicht klar wie ich jetzt innerhalb der Ereignisbehandlungsroutinen auf die Elemente des Arrays zugreifen kann.
Gruss
Michael
-
Hallo,
ich habe mal mein Beispielprogramm einkopiert. Das Problem ist, dass das Array innerhalb der Ereignisbehandlungsroutine (ganz unten) nicht bekannt ist:
error C2065: 'mein_array1' : undeclared identifierGruss
Michael#pragma once namespace test_23_5_2010 { 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> /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // array<int> ^mein_array1 = gcnew array<int>(64); } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(64, 80); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(80, 40); this->button1->TabIndex = 0; this->button1->Text = L"button1"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(292, 266); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Form1"; this->ResumeLayout(false); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { mein_array1[4] = 5; } }; }
-
public ref class Form1 : public System::Windows::Forms::Form { public: array<int> ^mein_array1;//<--- Hier Form1(void) { InitializeComponent(); // //TODO: Konstruktorcode hier hinzufügen. // mein_array1 = gcnew array<int>(64);//und da.... }
-
Danke für die Hilfe, jetzt funktioniert es!
Gruss
Michael