where to place array initialization?

G

Geoff Cox

Hello,

I am trying to get a grip on where to place the initialization of two
arrays in the code below which was created using Visual C++ 2005
Express Beta 2...

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

I had assumed that I would add them after the other private lines
below? Any suggestions for a beginner's Internet tutorial or book on
using C++ Express language?

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
//
}

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:
/// <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->trackBar1 = (gcnew
System::Windows::Forms::TrackBar());
this->pictureBox1 = (gcnew
System::Windows::Forms::pictureBox());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->label2 = (gcnew
System::Windows::Forms::Label());
this->button1 = (gcnew
System::Windows::Forms::Button());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// trackBar1
//
this->trackBar1->Location =
System::Drawing::point(90, 168);
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size =
System::Drawing::Size(104, 45);
this->trackBar1->TabIndex = 0;
//
// pictureBox1
//
this->pictureBox1->Image =
(cli::safe_cast<System::Drawing::Image^
(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location =
System::Drawing::point(37, 13);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size =
System::Drawing::Size(212, 134);
this->pictureBox1->TabIndex = 1;
this->pictureBox1->TabStop = false;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location =
System::Drawing::point(27, 182);
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(230, 182);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(31,
13);
this->label2->TabIndex = 3;
this->label2->Text = L"label2";
//
// button1
//
this->button1->Location =
System::Drawing::point(100, 219);
this->button1->Name = L"button1";
this->button1->Size =
System::Drawing::Size(75, 23);
this->button1->TabIndex = 4;
this->button1->Text = L"NEXT";
//
// 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->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->trackBar1);
this->Name = L"Form1";
this->Text = L"Form1";

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

}
#pragma endregion
};
}
 
N

Nishant Sivakumar

It's a matter of style I guess.
One option - You could put all your static array members together.

Also note that you don't need to specify "private" for each declaration. One
would do.

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

Geoff Cox

It's a matter of style I guess.

Nish,

Not if you are as much of a C++ beginner as I am! I have found that if
I put them as below the build works. I cannot workout how to
declare/initialize an int called qnumber so that I can have

LHSquestions[qnumber]

How do I do this? Where would I put something like

private: int qnumber = 0;

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

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"};



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->trackBar1 = (gcnew
System::Windows::Forms::TrackBar());
this->pictureBox1 = (gcnew
System::Windows::Forms::pictureBox());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->label2 = (gcnew
System::Windows::Forms::Label());
this->button1 = (gcnew
System::Windows::Forms::Button());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// trackBar1
//
this->trackBar1->Location =
System::Drawing::point(90, 168);
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size =
System::Drawing::Size(104, 45);
this->trackBar1->TabIndex = 0;
this->trackBar1->Scroll += gcnew
System::EventHandler(this, &Form1::trackBar1_Scroll);
//
// pictureBox1
//
this->pictureBox1->Image =
(cli::safe_cast<System::Drawing::Image^
(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location =
System::Drawing::point(37, 13);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size =
System::Drawing::Size(212, 134);
this->pictureBox1->TabIndex = 1;
this->pictureBox1->TabStop = false;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location =
System::Drawing::point(27, 182);
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(230, 182);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(31,
13);
this->label2->TabIndex = 3;
this->label2->Text = L"label2";
//
// button1
//
this->button1->Location =
System::Drawing::point(100, 219);
this->button1->Name = L"button1";
this->button1->Size =
System::Drawing::Size(75, 23);
this->button1->TabIndex = 4;
this->button1->Text = L"NEXT";
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->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->trackBar1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this,
&Form1::Form1_Load);

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

}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
++qnumber;
this->label1->Text = LHSquestions[qnumber];
this->label2->Text = RHSquestions[qnumber];
}
private: System::Void trackBar1_Scroll(System::Object^ sender,
System::EventArgs^ e) {
}

private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e) {
}
};
}
 
N

Nishant Sivakumar

Not if you are as much of a C++ beginner as I am! I have found that if
I put them as below the build works. I cannot workout how to
declare/initialize an int called qnumber so that I can have

LHSquestions[qnumber]

How do I do this? Where would I put something like

private: int qnumber = 0;

You cannot initialize a non-static member. Either make it static (if it
suites your purpose) or use an initialization list.
E.g. :-

ref class Test
{
private:
static int count = 10;
int x;
public:
Test():x(10)
{
}
};

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com


It's a matter of style I guess.

Nish,

Not if you are as much of a C++ beginner as I am! I have found that if
I put them as below the build works. I cannot workout how to
declare/initialize an int called qnumber so that I can have

LHSquestions[qnumber]

How do I do this? Where would I put something like

private: int qnumber = 0;

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

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"};



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->trackBar1 = (gcnew
System::Windows::Forms::TrackBar());
this->pictureBox1 = (gcnew
System::Windows::Forms::pictureBox());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->label2 = (gcnew
System::Windows::Forms::Label());
this->button1 = (gcnew
System::Windows::Forms::Button());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->trackBar1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// trackBar1
//
this->trackBar1->Location =
System::Drawing::point(90, 168);
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size =
System::Drawing::Size(104, 45);
this->trackBar1->TabIndex = 0;
this->trackBar1->Scroll += gcnew
System::EventHandler(this, &Form1::trackBar1_Scroll);
//
// pictureBox1
//
this->pictureBox1->Image =
(cli::safe_cast<System::Drawing::Image^
(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location =
System::Drawing::point(37, 13);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size =
System::Drawing::Size(212, 134);
this->pictureBox1->TabIndex = 1;
this->pictureBox1->TabStop = false;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location =
System::Drawing::point(27, 182);
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(230, 182);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(31,
13);
this->label2->TabIndex = 3;
this->label2->Text = L"label2";
//
// button1
//
this->button1->Location =
System::Drawing::point(100, 219);
this->button1->Name = L"button1";
this->button1->Size =
System::Drawing::Size(75, 23);
this->button1->TabIndex = 4;
this->button1->Text = L"NEXT";
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->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->trackBar1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this,
&Form1::Form1_Load);

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

}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
++qnumber;
this->label1->Text = LHSquestions[qnumber];
this->label2->Text = RHSquestions[qnumber];
}
private: System::Void trackBar1_Scroll(System::Object^ sender,
System::EventArgs^ e) {
}

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






One option - You could put all your static array members together.

Also note that you don't need to specify "private" for each declaration.
One
would do.

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

Geoff Cox

You cannot initialize a non-static member. Either make it static (if it
suites your purpose) or use an initialization list.

Nish,

Sorry - do not follow. I wish to use the qnumber as the index for the
array so I assume it cannot be static? Some how I need to declare and
initialize the int qnumber - but how?

int qnumber = 0;
LHSquestions[qnumber]

Cheers

Geoff
 
T

Tamas Demjen

Geoff said:
Sorry - do not follow. I wish to use the qnumber as the index for the
array so I assume it cannot be static?

Only you know that, it's an important design decision. A static member
variable is shared between all instances of the class. If you declare
your variable static, it means every single object will share the same
variable. A have no idea what qnumber is, and whether it should be
shared or not. Technically a static member is not owned by the object
but the class (all objects ever created within the same .exe or .dll).
Some how I need to declare and initialize the int qnumber - but how?

As Nishant has pointed it out before:

ref class Test
{
private:
int qnumber;
public:
Test();
};

Test::Test
: qnumber(0)
{
//[...]
LHSquestions[qnumber]
//[...]
}

Tom
 
G

Geoff Cox

Only you know that, it's an important design decision. A static member
variable is shared between all instances of the class. If you declare
your variable static, it means every single object will share the same
variable.

Tamas,

Thanks for above - I had misunderstood static ... incidentally I
thought I had found 2 books on Visual C++ 2005 Express but they are
not due out until October/November - can you suggest any good sites
for Visual C++ 2005 Express language for beginners? So far I have
found C# quite a bit easier to understand but I understand that the
combination of Visual C++ 2005 Express and the Windows SDK allows the
creation of Win32 apps .. so that would be interesting. Back to the
error messages ..

I seem to have the correct code for qnumber, the index for the results
array but am getting a couple of errors which you might underatnd!

line 155 error C2227 left of '->ToString' must point to
class/struct/union/generic type
line 181 error C2228 left of '.Close' must have class/struct/union

I have following for initialization which I think is OK.


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

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

private: static int qnumber = 0;

private: static array<String^>^ results = gcnew array<String^>
[LHSquestions->Length];



the first error message comes for next line

this->results[qnumber] = trackBar1->Value->ToString(); // line 155



and the second error message for line 188 below

TextWriter^ outfile = gcnew StreamWriter("h:\\a-temp1\\data.txt");

for (int i=0; i < LHSquestions->Length; i++) {


outfile->WriteLine(results);

}

outfile.Close(); // line 188



Cheers

Geoff













A have no idea what qnumber is, and whether it should be
shared or not. Technically a static member is not owned by the object
but the class (all objects ever created within the same .exe or .dll).
Some how I need to declare and initialize the int qnumber - but how?

As Nishant has pointed it out before:

ref class Test
{
private:
int qnumber;
public:
Test();
};

Test::Test
: qnumber(0)
{
//[...]
LHSquestions[qnumber]
//[...]
}

Tom
 
G

Geoff Cox

Tam

re my message with the 2 errors - have found the cause of the 2nd one
- I had outfile.Close() instead of outfile->Close()

also found error in the other code but still get 2 similar error
messages

results[qnumber] = trackBar1_Scroll->Value->ToString();

(I had trackBar1 not trackBar1_Scroll)

error messages re above

error C2227: left of '->Value' must point to
class/struct/union/generic type
error C2227: left of '->ToString' must point to
class/struct/union/generic type

thoughts?

Cheers

Geoff
 
T

Tamas Demjen

Geoff said:
Thanks for above - I had misunderstood static ... incidentally I
thought I had found 2 books on Visual C++ 2005 Express but they are
not due out until October/November - can you suggest any good sites
for Visual C++ 2005 Express language for beginners?

I'm waiting for this book to be published (I don't know when it will be
available):
http://www.amazon.com/exec/obidos/t...103-2357637-8533432?v=glance&s=books&n=507846
(or)
http://tinyurl.com/b4c3c

The problem is that your questions are not strictly C++/CLI related.
You're trying to learn C++/CLI without a strong C++ background. I don't
know C++/CLI as extensively as C++ -- few people know it inside out,
other than Microsoft engineers. But knowing C++ well certainly helps a
lot. I'm not saying you'll absolutely have to learn C++ before you can
learn C++/CLI, but given the fact that there are no C++/CLI books
available yet, your best bet at this moment is to rely on existing
resources and the draft standard.
So far I have
found C# quite a bit easier to understand

Difficult problems sometimes can not be addressed efficinetly in a
flexible ways with simple tools. But with .NET you have the advantage of
mixing languages. For example, you may prefer designing the GUI in C#
while writing lower level libraries in C++.
line 155 error C2227 left of '->ToString' must point to
class/struct/union/generic type
line 181 error C2228 left of '.Close' must have class/struct/union

try
..ToString()
->Close()

You use -> for pointers and handles. You use . otherwise (value types,
references). I know it may seem confusing first for people coming from
VC, C#, Delphi and similar languages where . applies to handles as well
as value types. In C++ there is a big difference. In fact, with smart
pointers .get() and ->get() have entirely different meaning, you can't
mix them.

Tom
 
G

Geoff Cox

Tamas

Thanks for your rhoughts. I have made a couple of steps forward in
that I can now create an array with the same number of elements as
there are in another array!

private: static array<String^>^ results = gcnew
array<String^>(otherArray->Length);

although I do not understand the use of the ^ yet!

Also, have been able to access the Scroll value of the trackbar

trackBar1->Value

Another odd one (to me!) in that the entry to the right of the Scroll
Event in Visual C++ 2005 Express Beta 2 is trackBar1_Scroll so I
thought the value would be

trackBar1_Scroll->Value

One I have not solved yet is making a button invisible - I thought it
would be

button1->Visible(false);

but this does not work! Any ideas?

Cheers

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