Hi friends,
I dunno how to declare global array.
Its funny because, it works if declared locally.
my code looks like that

private:
		/// <summary>
		/// Required designer variable.
		array<String^>^ salka = gcnew array<String^>(20); // GIVES ME ERROR
		System::String^ date;
		System::String^ linia;
		System::Int32^ token;
		System::Int32^ i;
		System::Int32^ j;
		System::Int32^ k;
		System::Boolean helpdesk;

btw...can you suggest me a book (for cli/c++ beginners?).
Because switching from pure c++ to cli/c++ is pain in the ass.
Is it dead language btw? nobody programs in it? - its very hard to find sth on the internet.

Recommended Answers

All 14 Replies

If you're going to instantiate your array outside of one of the methods (Load, Click, etc.) it must be static. The constructor of the form is a good place to instantiate it, but leave the declaration of it right where it is.

I've heard (though I've yet to get my hands on a copy) that Ivor Horton's book has a good treatment on C++/CLI. Yes, it is a pain in the ass. It was originally meant as a connector between C++ and .NET, but I think it evolved into something that some people use for its own sake. FunctionX has one of the better tutorials for it, but it's getting outdated.

My impression (but don't quote me on it, I'm not an expert, I just find the C++/CLI dialect interesting) is that one of M$'s evil plans is to get people to use C# for their UI (and other coding) with native C++ DLLs where necessary and C++/CLI filling in any middle ground.

Thx mate for quick answer.
I've changed the code to:

public:
		/// <summary>
		/// Required designer variable.
		System::String^ date;
		System::String^ linia;
		System::Int32^ token;
		System::Int32^ i;
		System::Int32^ j;
		System::Int32^ k;
		System::Boolean helpdesk;
	    static array<String^>^ salka = gcnew array<String^>(20);
		/// </summary>

Unfortunatelly,when I am providing data to the array, I recive an error:
Code:

salka[i]=linia;

Error:

error C3262: invalid array indexing: 0 dimension(s) specified for 1-dimensional 'cli::array<Type> ^'

When I finish that proggy, I start to learn c#...ehm...............

I tried the following program:
testme.h

#pragma once

ref class testme
{
    public:
        testme(void);
        static cli::array<System::String ^>^ strar = gcnew cli::array<System::String^>(20);
};

Arraytest2.cpp

// ArrayTest2.cpp : main project file.

#include "stdafx.h"
#include "testme.h"
using namespace System;

int main(array<System::String ^> ^args)
{
    testme tm;
    tm.strar[0] = "Test";
    tm.strar[1] = "Me";
    
    
    return 0;
}

(I didn't make a Winforms app, but it's essentially what you are asking it to do)
So, I'm not exactly sure what that error message is directed at. Do you try to give it an i > 19?

I still say the best thing to do is put

cli::array<String^>^ salka;  where you had it, and add

salka = cli::array<String^>(20); to your form's constructor

Ok I will make several tests...

New errors :/

public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			salka = cli::array<String^>(20);
			//
		}
public:
		/// <summary>
		/// Required designer variable.
		System::String^ date;
		System::String^ linia;
		System::Int32^ token;
		System::Int32^ i;
		System::Int32^ j;
		System::Int32^ k;
		System::Boolean helpdesk;
	    static cli::array<String^>^ salka;
		/// </summary>

error:

Error	1	error C2440: '<function-style-cast>' : cannot convert from 'int' to 'cli::array<Type>'

Damn, WHy these stupid arrays cannot work like in pure c++ :/

You don't need the array to be static if you do it this way. What line is that Error 1 pointing to?

It points to:

salka[i]=linia;
i++;

I want to load data from text file to my array..
If I remove static, I get even more errors

When I leave static - it seams to work...
UInfortunatelly there is a problem with

System::Int32^ i;

which was declared with all others global variables.
It doesnt work globally - I have to redeclare it locally:

System::Int32^ i=0;

Maybe I will explain what my program should do:.
Its a conference room manager...
It reads conference room names from text file.
tex contains sth like that:

Marlboro,1,2,3
Moon,1,4,7

Digits are used to unlock texboxes to fill...
so if Marlboro is pointed in the combobox, it unlocks texbox 1,2 and 3
Digits should be replaced from the names in combobox...( for a clean view)
so Druring loading items to combobox they are all removed...
and also loaded 2nd time to array.
this way I have nice Conference Rooms names in combobox (without digits) and names with digits in array.
then...thanks to the condition like:

System::Int32 j=0;
				 j=CRBox1->SelectedIndex; //numer porzadkowy opcji w combobox
				 for(int k = 0; i; k++)
				 {
				 if (j=k && salka[k]->Contains(",1")) {textBox1->Enabled=true;comboBox1->Enabled=true;}
                                 etc...				 
}

..I am able to unlock proper texboxes/Comboboxes (SelectedIndex + corresponding item (name with digits) in array)
unfortnatelly, as you can see, I have problem with globals.
Global array doesnt work...
Global int32 i - doesnt work too..(truly dont know why :/)
So I cannot pass the data and finish my program.

May I make a suggestion? Seeing as how this is getting to be painful with the arrays, try using a List<String^> instead. It's a lot more intuitive. It's under the System::Collections::Generics namespace. You can index in, but you can also just use the add method to tack the next string onto the end.

Also, rather than going through a lot of if statements like you have in your last code snippet, make a list (or array lol) of textBoxes and a List of comboBoxes, that way you can use a loop over everything.

Probably I will have to follow your idea.
Cli/C++ starts to piss me off...its really idiotic :/
declaration for global variable like i=1; works
i++; doesnt work....error:

Error	2	error C2440: 'initializing' : cannot convert from 'System::Int32 ^' to 'int'

Oh, I didn't even notice that before (sorry), you don't need to make those handles to int32s (System::Int32^), you can just make them ints.

Did you experiment with the lists at all.

System::Int^ i;

Error	7	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

:(

Error	2	error C2143: syntax error : missing ';' before '^'

:((
I can delcare Int16 or Int32....but I cannot use i++ then - WHY?!
I haven't tried to follow your ideas, will try them soon... :)...

Ok I replaced System::Int^ i; with int=i; lol...now it works...
I have to do several test to check if my ideas work :)

OMG It works!!!!!!!!!!!!!!

/// Required designer variable.
		System::String^ date;
		System::String^ linia;
		int token;
		int i;
		int j;
		int k;
		System::Boolean helpdesk;
	    static array<String^>^ salka = gcnew array<String^>(20);

this code solved the problem

THX mate - I have to take you for a beer if you visit Cracow :)

Now , I have to solve last problem...
How to send email with variables I've gathered in textboxes :/
I hope it wont be pain in the ass....

I have to take you for a beer if you visit Cracow

Sounds good to me!!

For a head start on the mailing stuff check out
See http://www.daniweb.com/forums/thread259176.html (most of that stuff is in the System::Net namespace.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.