Declaring a single-dimension Array in a Form app.

After checking on the internet I've found out that I can't simply use static arrays like I used to in console c++ (int i[32];) but I have to use some strange commands.
Well, I've found this:
 
array<Int32>^ value1 = gcnew array<Int32>{0, 1, 2};

The thing is I basically want to get all the values from a database, so I mainly want to declare an array that can hold a number of values. The problem is that the array I need has to hold 92 variables, so I can't just use
 
array<Int32>^ iArray = gcnew array<Int32>{0, 1, 2, 4, 5, 6, 7}; // etc 


Also, I've read something about this being a dynamic array, does that mean that basically if I define it like this but then I use like:
1
2
3
4
5
array<Int32>^ iArray = gcnew array<Int32>{0, 1, 2};

for ( int i = 0, i < 92, i ++ ) {
       iArray[i] = i + 1;
      }

will create the array iArray which would display:
iArray[0] = 1
iArray[1] = 2
etc?
Last edited on
When in .Net, use collections instead of arrays. Much simpler. In your case, you can use List<int>.
After looking up the List command, I understood that I should go like this:
1
2
3
4
5
List<int> iList = new List<int>()

for ( int i = 0, i < 92, i ++ ) {
      iList.Add(i) = i + 1;
}

Like basically creating the list and using it like I would use a normal Array in console programming ?

I shall try this, thank you for your time and reply :)

Edit: Also, for example iList[0]; could be used as a normal int, right? Just making sure :D

Well it seems it won't let me use it, as this is what I've got:
1
2
3
1>c:\users\breaker\documents\visual studio 2010\projects\damagecalculator\damagecalculator\Form1.h(68): error C2143: syntax error : missing ';' before '<'
1>c:\users\breaker\documents\visual studio 2010\projects\damagecalculator\damagecalculator\Form1.h(68): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\breaker\documents\visual studio 2010\projects\damagecalculator\damagecalculator\Form1.h(68): error C2238: unexpected token(s) preceding ';'


The lines are
1
2
int iLevel;
List<int> iHealth = new List<int>();


I know these may be stupid questions, but I am fairly new to this, and I appreciate any help I can get :D

After adding
 
using namespace System::Collections::Generic;

I got rid of those, now I got:
 
1>c:\users\breaker\documents\visual studio 2010\projects\damagecalculator\damagecalculator\Form1.h(68): error C3845: 'DamageCalculator::Form1::iHealth': only static data members can be initialized inside a ref class or value type


Edit: Solved that too using
 
static int iHealth = new List<int>();
Last edited on
Now I got an error telling me
1
2
3
4
5
c:\users\breaker\documents\visual studio 2010\projects\damagecalculator\damagecalculator\Form1.h(70): error C2750: 'System::Collections::Generic::List<T>' : cannot use 'new' on the reference type; use 'gcnew' instead
1>          with
1>          [
1>              T=int
1>          ]

So I changed it from:
 
static int iHealth = new List<int>();

to
 
static int iHealth = gcnew List<int>();

1
2
3
4
5
6
7
c:\users\breaker\documents\visual studio 2010\projects\damagecalculator\damagecalculator\Form1.h(70): error C2440: 'initializing' : cannot convert from 'System::Collections::Generic::List<T> ^' to 'int'
1>          with
1>          [
1>              T=int
1>          ]
1>          No user-defined-conversion operator available, or
1>          There is no context in which this conversion is possible

Edit: Sorry, doubleposted instead of editing..
Last edited on
You seem to be confusing C# with C++/CLI. I don't know CLI, but I know C# and your two new lines are C#. I THINK C++/CLI would be:

List<int>^ iHealth = gcnew List<int>();

Review your core tutorials on C++/CLI as you don't seem to have a good grasp on the basics of the language (me niether, but I do my .Net in C# instead).
Well it is C++/CLI as far as I know (Visual studio 2010 Express Windows Form Application)

When I tried the one you gave me, you know, List<int>^..etc
 
error C3845: 'DamageCalculator::Form1::iHealth': only static data members can be initialized inside a ref class or value type

Googled it, found out it should be a static, changed it to a static, the problems kept going :D
Cannot help further. It is a C++/CLI language issue, not a .Net issue. I know .Net with C#, so keep Googling or wait to see if someone else contributes here (unlikely, so don't hold your breath).

My best recommendation would be for you to sit down a read a good tutorial on C++/CLI, the language.
Post all of your code and what you're trying to do and I can help. Do you know the size of the array you want to begin with? do you need to use a list or an array?
for example: this will get you an array of int size 92 populated

1
2
3
4
5
array<Int32>^ iHealth = gcnew array<Int32>(92);
				for(int i = 0; i<92; i++)
				{
					iHealth[i] = i+1;
				}

This was populated on the form load event.
Last edited on
Tried that, didn't work, got the static error.
As I had to change to double instead of int, this is how I fixed it:
 
static array <double>^ iHealth = gcnew array<double>(92);

Works without any problems for the moment
are you doing this in a class? can we see the code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
namespace DamageCalculator {
	using namespace std;
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Collections::Generic;
	using namespace System::Collections::ObjectModel;

	/// <summary>
	/// Summary for Form1
	/// </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>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::PictureBox^  pctChamp1;
	private: System::Windows::Forms::PictureBox^  pctChamp2;
	protected: 

	private: System::Windows::Forms::ComboBox^  champName1;
	private: System::Windows::Forms::ComboBox^  champName2;
	private: System::Windows::Forms::Panel^  panel1;
	private: System::Windows::Forms::Panel^  panel2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::TextBox^  txtLevel1;


	private: System::Windows::Forms::Label^  label3;



	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;
	private: System::Windows::Forms::TextBox^  txtLevel2;

	private: System::Windows::Forms::Label^  label5;

	private:

		int iLevel;
		static array <double>^ iHealth = gcnew array<double>(92);

etc
are those variable meant to be global? consider where you want those to be available. I'm not sure of your ultimate goal here. hope this helps.
go to design mode and double click your form. this will give you the code for the form load event. drop in your code there and designate your array as a type Int32. this will allow you to use the managed types. this will also shine some light on how to manage the variables you create.
Topic archived. No new replies allowed.