Hey,
I have an item class:

#pragma once
using namespace System;
using namespace System::Drawing;

ref class Item
{
public:
	Item(void);
	int Type() { return _type; }
	void Type(int value) { _type = value; }
	String ^ Text() { return _text; }
	void Text(String ^ value) { _text = value; }
	static const int Types_Text = 0;
	static const int Types_Image = 1;
private:
	int _type;
	String ^ _text;
};

I want to add a class to group the items, but am not sure how I would do it. So far, I just added to the form:

array<Item^>^ items = gcnew array<Item^>(itemCount);
for (int i = 0; i < itemCount; i++)
{
    items[i] = (gcnew(Item));
}

I want to add this to a class, so that I can add sorting etc. and I will be having multiple item arrays.
Cheers

Recommended Answers

All 14 Replies

class MyItemGroup
{
  public:
    Item myItem;
};

Something like this?

class MyItemGroup
{
  public:
    Item myItem;
};

Something like this?

Yes, but I need an array of items.
Cheers

class MyItemGroup
{
  public:
    Item myItem[10];  //Array
};

Something like this?

class MyItemGroup
{
  public:
    Item myItem[10];  //Array
};

Something like this?

Yes, but won't I have to do a (gcnew(Item)) for each item, and I would also like a constant that picks the length of the array, which would be defined outside of the class.

which would be defined outside of the class.

As far a possible you should avoid globals.

Yes, but won't I have to do a (gcnew(Item)) for each item

They are not pointers.
IMO you are switching from C#, which fails(read: wrong) in labeling pointers as *unsafe* (which they are) & discourages programmers not to use them.

They are not pointers.
IMO you are switching from C#, which fails(read: wrong) in labeling pointers as *unsafe* (which they are) & discourages programmers not to use them.

I'm not following you, but I have switched from C#. Are you saying just declaring the array will be sufficient?
Cheers

int array[5];        //static allocation
/* Consider this */
int *array;
array = new int[5];      //dynamic allocation

Are you saying just declaring the array will be sufficient?

Yep.

Okay, thanks a lot. I'll mark as solved, but I can just create a setting for the array count, correct?

Yes, you can.

Is your Item class also ref? If yes, try removing it.

Is your Item class also ref? If yes, try removing it.

It just says I cannot use a managed type when I do that.

You are here working with generic classes rather than managed. Remove ref/val keywords from any classes & use new instead of gcnew.

Unmanaged arrays can't hold managed objects (and vice versa) due to garbage collection issues. If you are going to exchange data between the two it has to be marshaled.

Your best bet is to have a class member that's a cli::array and initialize it in the constructor of your ref class. You can pass the number of objects in the array,obtained from wherever, into the constructor.

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.