declare & initialize array?

G

Geoff Cox

Hello,

Can I separately declare and initialize a string array? How and where
would I do it in the code below? It was created using Visual C++ 2005
Express Beta 2 ...

In C# I would have

private string[] myArray;

and later

myArray = new string[] {"element 1","element 2"};

Now, I know C++ is more difficult to learn than C# but .......

Thanks

Geoff



#pragma once

namespace slider1
{
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();
//
//TODO: Add the constructor code here
//

}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">"description of the
parameter"</param>
virtual void Dispose(Boolean disposing) override
{
if (disposing && components)
{
delete components;
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::pictureBox^ pictureBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::TrackBar^ trackBar1;


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)
{

System::ComponentModel::ComponentResourceManager^ resources = (gcnew
System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->pictureBox1 = (gcnew
System::Windows::Forms::pictureBox());
this->button1 = (gcnew
System::Windows::Forms::Button());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->label2 = (gcnew
System::Windows::Forms::Label());
this->trackBar1 = (gcnew
System::Windows::Forms::TrackBar());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Image =
(cli::safe_cast<System::Drawing::Image^
(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location =
System::Drawing::point(36, 26);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size =
System::Drawing::Size(218, 129);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// button1
//
this->button1->Location =
System::Drawing::point(105, 231);
this->button1->Name = L"button1";
this->button1->Size =
System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location =
System::Drawing::point(23, 190);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(31,
13);
this->label1->TabIndex = 2;
this->label1->Text = L"label1";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location =
System::Drawing::point(222, 190);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(31,
13);
this->label2->TabIndex = 3;
this->label2->Text = L"label2";
//
// trackBar1
//
this->trackBar1->Location =
System::Drawing::point(87, 180);
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size =
System::Drawing::Size(104, 45);
this->trackBar1->TabIndex = 4;
this->trackBar1->Scroll += gcnew
System::EventHandler(this, &Form1::Form1_Load);
//
// 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->trackBar1);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this,
&Form1::Form1_Load);

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e) {
}
};
}
 
M

Markus Minichmayr

Hi Geoff!

Declare the array like

private: System::String __gc* myArray __gc[];

and initialize it anywhere you want like

this->myArray = new System::String* __gc[2];

this->myArray[0] = "element 1";

this->myArray[1] = "element 2";


Hope that helps!

- Markus


Hello,

Can I separately declare and initialize a string array? How and where
would I do it in the code below? It was created using Visual C++ 2005
Express Beta 2 ...

In C# I would have

private string[] myArray;

and later

myArray = new string[] {"element 1","element 2"};

Now, I know C++ is more difficult to learn than C# but .......

Thanks

Geoff



#pragma once

namespace slider1
{
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();
//
//TODO: Add the constructor code here
//

}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">"description of the
parameter"</param>
virtual void Dispose(Boolean disposing) override
{
if (disposing && components)
{
delete components;
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::pictureBox^ pictureBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::TrackBar^ trackBar1;


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)
{

System::ComponentModel::ComponentResourceManager^ resources = (gcnew
System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->pictureBox1 = (gcnew
System::Windows::Forms::pictureBox());
this->button1 = (gcnew
System::Windows::Forms::Button());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->label2 = (gcnew
System::Windows::Forms::Label());
this->trackBar1 = (gcnew
System::Windows::Forms::TrackBar());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Image =
(cli::safe_cast<System::Drawing::Image^
(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location =
System::Drawing::point(36, 26);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size =
System::Drawing::Size(218, 129);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// button1
//
this->button1->Location =
System::Drawing::point(105, 231);
this->button1->Name = L"button1";
this->button1->Size =
System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location =
System::Drawing::point(23, 190);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(31,
13);
this->label1->TabIndex = 2;
this->label1->Text = L"label1";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location =
System::Drawing::point(222, 190);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(31,
13);
this->label2->TabIndex = 3;
this->label2->Text = L"label2";
//
// trackBar1
//
this->trackBar1->Location =
System::Drawing::point(87, 180);
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size =
System::Drawing::Size(104, 45);
this->trackBar1->TabIndex = 4;
this->trackBar1->Scroll += gcnew
System::EventHandler(this, &Form1::Form1_Load);
//
// 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->trackBar1);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this,
&Form1::Form1_Load);

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e) {
}
};
}
 
W

William DePalo [MVP VC++]

Can I separately declare and initialize a string array? How and where
would I do it in the code below? It was created using Visual C++ 2005
Express Beta 2 ...

In C# I would have

private string[] myArray;

and later

myArray = new string[] {"element 1","element 2"};

Now, I know C++ is more difficult to learn than C# but .......

This is the VC.Net 2003 equivalent:

String *myArray[];

myArray = new String*[2];
myArray[0] = new String("element 1");
myArray[1] = new String("element 2");

I should tell you that I don't have 2005 installed on this box (where I have
a two C++ compiler limit <g>) but I think that to convert from MC++ to
C++/CLI all you have to do is to replace the *'s with ^'s and new's with
gcnew's

String ^myArray[];

myArray = new String^ [2];
myArray[0] = gcnew String("element 1");
myArray[1] = gcnew String("element 2");

Regards,
Will
 
T

Tamas Demjen

Almost like in C#:

array<String^>^ myArray = gcnew array<String^> {"element 1",
"element 2"};

You can only do this in Visual C++ 2005, and only in .NET. It's a
completely new feature.

See: http://www.codeproject.com/managedcpp/cppcliarrays.asp

In standard C++ and old MC++ you can't initialize arrays. C++ doesn't
have the concept of array initialization.

Tom
 
W

Willy Denoyette [MVP]

Use the initializer syntax in C++CLI...

array<String^>^ myArray;
....
myArray= gcnew array<String^> {"first", "second"};

Willy.

Hello,

Can I separately declare and initialize a string array? How and where
would I do it in the code below? It was created using Visual C++ 2005
Express Beta 2 ...

In C# I would have

private string[] myArray;

and later

myArray = new string[] {"element 1","element 2"};

Now, I know C++ is more difficult to learn than C# but .......

Thanks

Geoff



#pragma once

namespace slider1
{
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();
//
//TODO: Add the constructor code here
//

}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">"description of the
parameter"</param>
virtual void Dispose(Boolean disposing) override
{
if (disposing && components)
{
delete components;
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::pictureBox^ pictureBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::TrackBar^ trackBar1;


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)
{

System::ComponentModel::ComponentResourceManager^ resources = (gcnew
System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->pictureBox1 = (gcnew
System::Windows::Forms::pictureBox());
this->button1 = (gcnew
System::Windows::Forms::Button());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->label2 = (gcnew
System::Windows::Forms::Label());
this->trackBar1 = (gcnew
System::Windows::Forms::TrackBar());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Image =
(cli::safe_cast<System::Drawing::Image^
(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location =
System::Drawing::point(36, 26);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size =
System::Drawing::Size(218, 129);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// button1
//
this->button1->Location =
System::Drawing::point(105, 231);
this->button1->Name = L"button1";
this->button1->Size =
System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location =
System::Drawing::point(23, 190);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(31,
13);
this->label1->TabIndex = 2;
this->label1->Text = L"label1";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location =
System::Drawing::point(222, 190);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(31,
13);
this->label2->TabIndex = 3;
this->label2->Text = L"label2";
//
// trackBar1
//
this->trackBar1->Location =
System::Drawing::point(87, 180);
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size =
System::Drawing::Size(104, 45);
this->trackBar1->TabIndex = 4;
this->trackBar1->Scroll += gcnew
System::EventHandler(this, &Form1::Form1_Load);
//
// 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->trackBar1);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this,
&Form1::Form1_Load);

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e) {
}
};
}
 
G

Geoff Cox

Hi Geoff!

Declare the array like

private: System::String __gc* myArray __gc[];

and initialize it anywhere you want like

this->myArray = new System::String* __gc[2];

this->myArray[0] = "element 1";

this->myArray[1] = "element 2";

Markus,

Thanks for the help. I have used as above in code below but am getting
a lot of errors.

Errors first and code after that. Any thoughts?! Have I declared the
arrays in the wrong place? Rememeber I am using Visual C++ 2005
Express Beta 2 - this relevant?

Cheers

Geoff

-------------errors------------------

Compiling...
stdafx.cpp
Compiling...
slider1.cpp
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(60) : error C4980: '__gc' : use
of this keyword requires /clr:blush:ldSyntax command line option
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(60) : error C3699:
'interior_ptr' : cannot use this indirection on type 'System::String'
compiler replacing 'interior_ptr' with '^' to continue parsing
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(60) : error C2728:
'System::String ^' : a native array cannot contain this managed type
Did you mean 'array'?
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(61) : error C3699:
'interior_ptr' : cannot use this indirection on type 'System::String'
compiler replacing 'interior_ptr' with '^' to continue parsing
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(61) : error C2728:
'System::String ^' : a native array cannot contain this managed type
Did you mean 'array'?
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(62) : error C3699:
'interior_ptr' : cannot use this indirection on type 'System::String'
compiler replacing 'interior_ptr' with '^' to continue parsing
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(62) : error C2728:
'System::String ^' : a native array cannot contain this managed type
Did you mean 'array'?
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C3150: '[' : '__gc'
can only be applied to a class, struct, interface, array or pointer
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C2143: syntax error
: missing ')' before '}'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C2143: syntax error
: missing ';' before '}'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C2238: unexpected
token(s) preceding ';'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(60) : error C4368: cannot define
'LHSquestions' as a member of managed 'slider1::Form1': mixed types
are not supported
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(61) : error C4368: cannot define
'RHSquestions' as a member of managed 'slider1::Form1': mixed types
are not supported
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(62) : error C4368: cannot define
'results' as a member of managed 'slider1::Form1': mixed types are not
supported
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C2143: syntax error
: missing ';' before 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(27) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(27) : error C2143: syntax error
: missing ';' before '{'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(27) : error C2447: '{' : missing
function header (old-style formal list?)
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(47) : error C2065: 'components'
: undeclared identifier
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(49) : error C2541: 'delete' :
cannot delete objects that are not pointers
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C2059: syntax error
: '->'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C3150: '[' : '__gc'
can only be applied to a class, struct, interface, array or pointer
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C2143: syntax error
: missing ')' before '}'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C2143: syntax error
: missing ';' before '}'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(66) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(67) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(68) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(69) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(76) : error C3145: 'components'
: global or static variable may not have managed type
'System::ComponentModel::Container ^'
may not declare a global or static variable, or a member of a
native type that refers to objects in the gc heap
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(76) : error C2373: 'components'
: redefinition; different type modifiers
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(85) : error C2653: 'Form1' : is
not a class or namespace name
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(85) : fatal error C1903: unable
to recover from previous error(s); stopping compilation
AssemblyInfo.cpp
Generating Code...




---------code-------------------

#pragma once


namespace slider1
{
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
//


// this.label1.Text = LHSquestions[qnumber];
// this.label2.Text = RHSquestions[qnumber];

}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">"description of the
parameter"</param>
virtual void Dispose(Boolean disposing) override
{
if (disposing && components)
{
delete components;
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::pictureBox^ pictureBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::TrackBar^ trackBar1;

private: System::String __gc* LHSquestions __gc[];
private: System::String __gc* RHSquestions __gc[];
private: System::String __gc* results __gc[];

this->LHSquestions = new System::String* __gc[2}
this->RHSquestions = new System::String* __gc[2}
this->LHSquestions[0] = "question 1";
this->LHSquestions[1] = "question 2";
this->RHSquestions[0] = "question 1";
this->RHSquestions[1] = "question 2";



/// <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)
{

System::ComponentModel::ComponentResourceManager^ resources = (gcnew
System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->pictureBox1 = (gcnew
System::Windows::Forms::pictureBox());
this->button1 = (gcnew
System::Windows::Forms::Button());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->label2 = (gcnew
System::Windows::Forms::Label());
this->trackBar1 = (gcnew
System::Windows::Forms::TrackBar());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Image =
(cli::safe_cast<System::Drawing::Image^
(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location =
System::Drawing::point(36, 26);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size =
System::Drawing::Size(218, 129);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// button1
//
this->button1->Location =
System::Drawing::point(105, 231);
this->button1->Name = L"button1";
this->button1->Size =
System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"NEXT";
this->button1->Click += gcnew
System::EventHandler(this, &Form1::Form1_Load);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location =
System::Drawing::point(23, 190);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(31,
13);
this->label1->TabIndex = 2;
this->label1->Text = L"label1";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location =
System::Drawing::point(222, 190);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(31,
13);
this->label2->TabIndex = 3;
this->label2->Text = L"label2";
//
// trackBar1
//
this->trackBar1->Location =
System::Drawing::point(87, 180);
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size =
System::Drawing::Size(104, 45);
this->trackBar1->TabIndex = 4;
this->trackBar1->Scroll += gcnew
System::EventHandler(this, &Form1::Form1_Load);
//
// 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->trackBar1);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this,
&Form1::Form1_Load);

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e) {
}
};
}
 
G

Geoff Cox

Use the initializer syntax in C++CLI...

array<String^>^ myArray;
...
myArray= gcnew array<String^> {"first", "second"};

Thanks Willy - I'm moving on a little! I have used

private: array<String^>^ LHSquestions;
private: array<String^>^ RHSquestions;

line 60 LHSquestions = gcnew array<String^> {"question 1", "question
2"};
line 61 RHSquestions = gcnew array<String^> {"question 1", "question
2"};

but get following errors

Form1.h(60) : error C4430: missing type specifier - int assumed. Note:
C++ does not support default-int
Form1.h(60) : error C3845: 'slider1::Form1::LHSquestions': only static
data members can be initialized inside a ref class or value type

Form1.h(61) : error C4430: missing type specifier - int assumed. Note:
C++ does not support default-int
Form1.h(61) : error C3845: 'slider1::Form1::RHSquestions': only static
data members can be initialized inside a ref class or value type

any thoughts?

Cheers

Geoff
 
G

Geoff Cox

Almost like in C#:

array<String^>^ myArray = gcnew array<String^> {"element 1",
"element 2"};

Thanks - could build OK with above but get error message with

array<String^>^ myArray;
....
myArray= gcnew array<String^> {"first", "second"};


error message

++ does not support default-int
Form1.h(60) : error C3845: 'slider1::Form1::LHSquestions': only static
data members can be initialized inside a ref class or value type

Form1.h(61) : error C4430: missing type specifier - int assumed. Note:
C++ does not support default-int
Form1.h(61) : error C3845: 'slider1::Form1::RHSquestions': only static
data members can be initialized inside a ref class or value type

any thoughts?

Cheers

Geoff
 
M

Markus Minichmayr

Sorry, I missed the point that you are using VC++ 2005. :-/

Hi Geoff!

Declare the array like

private: System::String __gc* myArray __gc[];

and initialize it anywhere you want like

this->myArray = new System::String* __gc[2];

this->myArray[0] = "element 1";

this->myArray[1] = "element 2";

Markus,

Thanks for the help. I have used as above in code below but am getting
a lot of errors.

Errors first and code after that. Any thoughts?! Have I declared the
arrays in the wrong place? Rememeber I am using Visual C++ 2005
Express Beta 2 - this relevant?

Cheers

Geoff

-------------errors------------------

Compiling...
stdafx.cpp
Compiling...
slider1.cpp
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(60) : error C4980: '__gc' : use
of this keyword requires /clr:blush:ldSyntax command line option
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(60) : error C3699:
'interior_ptr' : cannot use this indirection on type 'System::String'
compiler replacing 'interior_ptr' with '^' to continue parsing
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(60) : error C2728:
'System::String ^' : a native array cannot contain this managed type
Did you mean 'array'?
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(61) : error C3699:
'interior_ptr' : cannot use this indirection on type 'System::String'
compiler replacing 'interior_ptr' with '^' to continue parsing
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(61) : error C2728:
'System::String ^' : a native array cannot contain this managed type
Did you mean 'array'?
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(62) : error C3699:
'interior_ptr' : cannot use this indirection on type 'System::String'
compiler replacing 'interior_ptr' with '^' to continue parsing
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(62) : error C2728:
'System::String ^' : a native array cannot contain this managed type
Did you mean 'array'?
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C3150: '[' : '__gc'
can only be applied to a class, struct, interface, array or pointer
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C2143: syntax error
: missing ')' before '}'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C2143: syntax error
: missing ';' before '}'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(64) : error C2238: unexpected
token(s) preceding ';'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(60) : error C4368: cannot define
'LHSquestions' as a member of managed 'slider1::Form1': mixed types
are not supported
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(61) : error C4368: cannot define
'RHSquestions' as a member of managed 'slider1::Form1': mixed types
are not supported
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(62) : error C4368: cannot define
'results' as a member of managed 'slider1::Form1': mixed types are not
supported
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C2143: syntax error
: missing ';' before 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(27) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(27) : error C2143: syntax error
: missing ';' before '{'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(27) : error C2447: '{' : missing
function header (old-style formal list?)
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(47) : error C2065: 'components'
: undeclared identifier
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(49) : error C2541: 'delete' :
cannot delete objects that are not pointers
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C2059: syntax error
: '->'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C3150: '[' : '__gc'
can only be applied to a class, struct, interface, array or pointer
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C2143: syntax error
: missing ')' before '}'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(65) : error C2143: syntax error
: missing ';' before '}'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(66) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(67) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(68) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(69) : error C2059: syntax error
: 'this'
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(76) : error C3145: 'components'
: global or static variable may not have managed type
'System::ComponentModel::Container ^'
may not declare a global or static variable, or a member of a
native type that refers to objects in the gc heap
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(76) : error C2373: 'components'
: redefinition; different type modifiers
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(85) : error C2653: 'Form1' : is
not a class or namespace name
d:\documents and settings\administrator\my documents\visual studio
2005\projects\slider1\slider1\Form1.h(85) : fatal error C1903: unable
to recover from previous error(s); stopping compilation
AssemblyInfo.cpp
Generating Code...




---------code-------------------

#pragma once


namespace slider1
{
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
//


// this.label1.Text = LHSquestions[qnumber];
// this.label2.Text = RHSquestions[qnumber];

}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">"description of the
parameter"</param>
virtual void Dispose(Boolean disposing) override
{
if (disposing && components)
{
delete components;
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::pictureBox^ pictureBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::TrackBar^ trackBar1;

private: System::String __gc* LHSquestions __gc[];
private: System::String __gc* RHSquestions __gc[];
private: System::String __gc* results __gc[];

this->LHSquestions = new System::String* __gc[2}
this->RHSquestions = new System::String* __gc[2}
this->LHSquestions[0] = "question 1";
this->LHSquestions[1] = "question 2";
this->RHSquestions[0] = "question 1";
this->RHSquestions[1] = "question 2";



/// <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)
{

System::ComponentModel::ComponentResourceManager^ resources = (gcnew
System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->pictureBox1 = (gcnew
System::Windows::Forms::pictureBox());
this->button1 = (gcnew
System::Windows::Forms::Button());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->label2 = (gcnew
System::Windows::Forms::Label());
this->trackBar1 = (gcnew
System::Windows::Forms::TrackBar());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Image =
(cli::safe_cast<System::Drawing::Image^
(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location =
System::Drawing::point(36, 26);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size =
System::Drawing::Size(218, 129);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// button1
//
this->button1->Location =
System::Drawing::point(105, 231);
this->button1->Name = L"button1";
this->button1->Size =
System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"NEXT";
this->button1->Click += gcnew
System::EventHandler(this, &Form1::Form1_Load);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location =
System::Drawing::point(23, 190);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(31,
13);
this->label1->TabIndex = 2;
this->label1->Text = L"label1";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location =
System::Drawing::point(222, 190);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(31,
13);
this->label2->TabIndex = 3;
this->label2->Text = L"label2";
//
// trackBar1
//
this->trackBar1->Location =
System::Drawing::point(87, 180);
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size =
System::Drawing::Size(104, 45);
this->trackBar1->TabIndex = 4;
this->trackBar1->Scroll += gcnew
System::EventHandler(this, &Form1::Form1_Load);
//
// 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->trackBar1);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this,
&Form1::Form1_Load);

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e) {
}
};
}
 
G

Geoff Cox

Sorry, I missed the point that you are using VC++ 2005. :-/

No problem! Wonder if you can comment on my postings to Tamas and
Willy? I am not clear where I should place the initialization of the
arrays...

Geoff
 
G

Geoff Cox

Sorry, I missed the point that you are using VC++ 2005. :-/

No problem! Wonder if you can comment on my postings to Tamas and
Willy? I am not clear where I should place the initialization of the
arrays...

Geoff
 
W

Willy Denoyette [MVP]

Weird, It looks like your LHSquestions and RHSquestions declarations are not
visible by the initializers ( = gcnew array<String^> {"question 1).....
Could you post all of the code (just strip the non relevant parts).

Willy.
 
G

Geoff Cox

Weird, It looks like your LHSquestions and RHSquestions declarations are not
visible by the initializers ( = gcnew array<String^> {"question 1).....
Could you post all of the code (just strip the non relevant parts).

Willy

I have got it building OK with the code below, ie putting the
declarations/initializations with the others, but, I cannot see
how/where to declare/initialize an int qnumber which would be used as
the array index ...

eg

int qnumber = 0;

to be used in

LHSquestions[qnumber]

How would I do this?

Cheers

Geoff


#pragma once


namespace trackbar1
{
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
//
private: int qnumber = 0;
this->label1->Text = LHSquestions[qnumber];
this->label2->Text = RHSquestions[qnumber];


}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">"description of the
parameter"</param>
virtual void Dispose(Boolean disposing) override
{
if (disposing && components)
{
delete components;
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::TrackBar^ trackBar1;
protected:
private: System::Windows::Forms::pictureBox^ pictureBox1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Button^ button1;

private: static array<String^>^ LHSquestions = gcnew
array<String^> {"question 1", "question 2"};
private: static array<String^>^ RHSquestions = gcnew
array<String^> {"question 1", "question 2"};
 
T

Tamas Demjen

Geoff said:
Form1.h(60) : error C3845: 'slider1::Form1::LHSquestions': only static
data members can be initialized inside a ref class or value type

Is it possible that you're initializing variables in the class
declaration? Just like in C++, C++/CLI can't do that either. You must
initialize member variables in your c'tor:

ref class C
{
C();
int i;
array<String^>^ myArray;
};

C::C()
: i(0), myArray(gcnew array<String^> {"first", "second"})
{
}

or

C::C()
{
i = 0;
myArray = gcnew array<String^> {"first", "second"};
}

Tom
 
G

Geoff Cox

Is it possible that you're initializing variables in the class
declaration? Just like in C++, C++/CLI can't do that either. You must
initialize member variables in your c'tor:

could well be the case but what is c'tor?!

Geoff
 
G

Geoff Cox

Is it possible that you're initializing variables in the class
declaration? Just like in C++, C++/CLI can't do that either. You must
initialize member variables in your c'tor:

got it ! constructor?

Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top