Passing managed arrays

J

jlea

Does anyone know how to pass a managed array to a method and have that
method actually create the array if necessary, ie., if the passed array is
null? For example:

void AddAttribute(XmlAttribute* attribList[], XmlAttribute* anAttrib)
{
if (attribList == 0L) {
attribList = new XmlAttribute * [1];
attribList[0] = anAttrib;
} else {
if (!AttributeExists(attribList, anAttrib)) {
int size = attribList->get_Length();
for (int i=1; i<=size; i++) {
if (attribList[i-1] == 0L) {
attribList[i-1] = anAttrib;
goto Done;
}
}

XmlAttribute* temp[];
temp = new XmlAttribute * [size+1];
attribList->CopyTo(temp, 0);
temp[size] = anAttrib;
attribList = temp;
} else {
throw new ApplicationException("Attribute already exsits");
}
}
Done: ;
}

In the old days of native C++, I would have passed the array as:
XmlAttribute*& attribList so that the function can change the pointer
itself. Thanks. Jon.
 
V

Vadim Melnik

Hi,
Does anyone know how to pass a managed array to a method and have that
method actually create the array if necessary, ie., if the passed array is
null? For example:
void AddAttribute(XmlAttribute* attribList[], XmlAttribute* anAttrib)
{


Does the following help?

void AddAttribute(XmlAttribute* (*attribList)[], XmlAttribute* anAttrib)

...
Regards,
Vadim.
 
J

jlea

It doesn't like when I do:

attribList = new XmlAttribute * [1];

Jon.


Vadim Melnik said:
Hi,
Does anyone know how to pass a managed array to a method and have that
method actually create the array if necessary, ie., if the passed array is
null? For example:
void AddAttribute(XmlAttribute* attribList[], XmlAttribute* anAttrib)
{


Does the following help?

void AddAttribute(XmlAttribute* (*attribList)[], XmlAttribute* anAttrib)

..
Regards,
Vadim.
 
V

Vadim Melnik

Hi,
It doesn't like when I do:

attribList = new XmlAttribute * [1];

The following should work:

void AddAttribute2(XmlAttribute* (*attribList)[], XmlAttribute* anAttrib)
{
*attribList = new XmlAttribute *[2];
....
}

...
Regards,
Vadim.
 
V

Vadim Melnik

Jon,

Sure it won't compile, attribList is pointer to array of XmlAttribute*, so
you need dereference it before:

(*attribList)[0] = anAttrib;


jlea said:
Your modified line compiled by the next line didn't:

attribList[0] = anAttrib;

it says

DAL_XML.cpp(377) : error C2440: '=' : cannot convert from
'System::Xml::XmlAttribute __gc *' to 'System::Xml::XmlAttribute __gc *
__gc[]'

Jon.


Vadim Melnik said:
Hi,
It doesn't like when I do:

attribList = new XmlAttribute * [1];

The following should work:

void AddAttribute2(XmlAttribute* (*attribList)[], XmlAttribute* anAttrib)
{
*attribList = new XmlAttribute *[2];
...
}

..
Regards,
Vadim.
 
J

jlea

I got the function to compile - thanks, but now I'm having problems with
passing it the actual array. I'm using the following code:

public System.Xml.XmlAttribute[] AnyAttr;
//the data member is in a C# class

XmlAttribute* attribList[] = pSSConnItemType->AnyAttr; //get the
array from the C# object
AddAttribute(attribList, anAttrib);
//doesn't compile


error C2664: 'void
LeapSoft::DAL_Imp::DAL_XML::AddAttribute(System::Xml::XmlAttribute __gc
*(__gc *) __gc[],System::Xml::XmlAttribute __gc *)' : cannot convert
parameter 1 from 'System::Xml::XmlAttribute __gc * __gc[]' to
'System::Xml::XmlAttribute __gc *(__gc *) __gc[]'
Can only convert a __gc array to or from Object * or Array *

Jon.

Vadim Melnik said:
Jon,

Sure it won't compile, attribList is pointer to array of XmlAttribute*, so
you need dereference it before:

(*attribList)[0] = anAttrib;


jlea said:
Your modified line compiled by the next line didn't:

attribList[0] = anAttrib;

it says

DAL_XML.cpp(377) : error C2440: '=' : cannot convert from
'System::Xml::XmlAttribute __gc *' to 'System::Xml::XmlAttribute __gc *
__gc[]'

Jon.


Vadim Melnik said:
Hi,

It doesn't like when I do:

attribList = new XmlAttribute * [1];


The following should work:

void AddAttribute2(XmlAttribute* (*attribList)[], XmlAttribute* anAttrib)
{
*attribList = new XmlAttribute *[2];
...
}

..
Regards,
Vadim.
 
V

Vadim Melnik

Hi Jon,

Add & before attribList:

AddAttribute(&attribList, anAttrib);

P.S.: Probably it does make sense obtain good book about C++ language...


jlea said:
I got the function to compile - thanks, but now I'm having problems with
passing it the actual array. I'm using the following code:

public System.Xml.XmlAttribute[] AnyAttr;
//the data member is in a C# class

XmlAttribute* attribList[] = pSSConnItemType->AnyAttr; //get the
array from the C# object
AddAttribute(attribList, anAttrib);
//doesn't compile


error C2664: 'void
LeapSoft::DAL_Imp::DAL_XML::AddAttribute(System::Xml::XmlAttribute __gc
*(__gc *) __gc[],System::Xml::XmlAttribute __gc *)' : cannot convert
parameter 1 from 'System::Xml::XmlAttribute __gc * __gc[]' to
'System::Xml::XmlAttribute __gc *(__gc *) __gc[]'
Can only convert a __gc array to or from Object * or Array *

Jon.

Vadim Melnik said:
Jon,

Sure it won't compile, attribList is pointer to array of XmlAttribute*, so
you need dereference it before:

(*attribList)[0] = anAttrib;


jlea said:
Your modified line compiled by the next line didn't:

attribList[0] = anAttrib;

it says

DAL_XML.cpp(377) : error C2440: '=' : cannot convert from
'System::Xml::XmlAttribute __gc *' to 'System::Xml::XmlAttribute __gc *
__gc[]'

Jon.


Hi,

It doesn't like when I do:

attribList = new XmlAttribute * [1];


The following should work:

void AddAttribute2(XmlAttribute* (*attribList)[], XmlAttribute* anAttrib)
{
*attribList = new XmlAttribute *[2];
...
}

..
Regards,
Vadim.
 

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