Hey,
I have the following codes:

// In Form1.h
protected: 
 ItemGroup^ History;
 ItemGroup^ Favourites;
// In Form1(void)
History = (gcnew ItemGroup());
Favourites = (gcnew ItemGroup());
// ItemGroup
public:
ItemGroup(void);
int Length() { return _length; }
Item^ Items(int _index) { return _items[_index]; };
void Add(String^ _text)
{
	for (int i = _length; i < 0; i--)
	{
		_items[i] = (gcnew Item());
		_items[i] = _items[i - 1];
	}
	if (_length < _maxlength)
		_length++;
	_items[0] = (gcnew Item());
	_items[0]->Text(_text);
}
void Delete(int index)
{
	for (int i = index; i < _length; i++)
		_items[i] = _items[i + 1];
	_length--;
}
private:
static const int _maxlength = 8;
static int _length = 0;
static array<Item^>^ _items = gcnew array<Item^>(_maxlength);
};
// Item
public:
	Item(void);
	String ^ Text() { return _text; }
	void Text(String ^ value) { _text = value; }
private:
	String ^ _text;

Both History and Favourites are treated as the same object. They both have exactly the same items, length and everything. I do one thing to one, the other one updates too. What could be causing this?
Thanks and sorry for the masses of code (I tried to trim it).

Recommended Answers

All 9 Replies

Are these ItemGroup^ supposed to be ItemGroup* ? If so, then I bet you are storing and using a pointer somewhere where you think you are storing and using the actual value.

Dave

Are these ItemGroup^ supposed to be ItemGroup* ? If so, then I bet you are storing and using a pointer somewhere where you think you are storing and using the actual value.

Hey,
I he just switched from C#. I'm not really sure what all these * and ^ symbols mean. If I do not include them, it will not compile. Everything I used has been pasted here.
Any suggestions?
Cheers

Lines 10 and 11 in ItemGroup make no sense. Line 10 sets _items to a value, and line 11 immediately overwrites that value with a different value.

Once you figure out what the code is supposed to be doing, I suspect that the solution to the problem will be straightforward.

Lines 10 and 11 in ItemGroup make no sense. Line 10 sets _items to a value, and line 11 immediately overwrites that value with a different value.

Once you figure out what the code is supposed to be doing, I suspect that the solution to the problem will be straightforward.

But even with that, it should not make two items be the same, no?

Why are you declaring your array as static? There's only going to be one copy over all the objects of your class.

Why are you declaring your array as static? There's only going to be one copy over all the objects of your class.

Otherwise it will not compile- I have a value set.

I'm confused what you mean by "a value set." I would think you would want a new instance of the array for each object. Was it set up in C# like that?

I'm confused what you mean by "a value set." I would think you would want a new instance of the array for each object. Was it set up in C# like that?

The variable has been assigned a value. If I do not declare static, I cannot assign a value on creation.

Declare the variables in your class without giving them a value. Define them in the constructor for the class. Then they won't have to be static.

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.