Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pass an array from x++ to a method in a .NET DLL

1,384 views
Skip to first unread message

Philip

unread,
Dec 23, 2009, 12:07:01 AM12/23/09
to
Here is a question which mentioned two years ago, and now not any solution
for this yet. Can anyone help me on this? Thanks.

Is it possible to pass an array from x++ to a method in a .NET DLL?

I can get an array as a return from my DLL into x++, but I'm struggling with
passing the array back to the DLL. I get a compile error stating the class
does not contain the function (but it does). I'm guessing it is a problem
with the parameter type.

For example, this works fine:
================
System.Array myArray;
MyDllClass myObject = new MyDllClass();
AnotherDllClass anotherObject;

myArray = myo.getArray();
anotherObject = myArray.GetValue(0); // gets the first object
================


But I can't figure out how to pass an array back like this:
================
MyDllClass myClass = new MyDllClass();
AnotherDllClass anotherObject;
System.Array myArray = System.Array::CreateInstance(anotherObject.GetType(),
2);

// setup the array
anotherObject = new AnotherDllClass();
myArray.SetValue(anotherObject,0);
myArray.SetValue(anotherObject,1);

// pass the array
myClass.TakeArray(myArray); // errors out with message: "The class
does not contain this function"
==========================


Any help would be greatly appreciated!

Luegisdorf

unread,
Dec 23, 2009, 6:58:01 AM12/23/09
to
Hi Philip

I guess, you want to pass the mentioned object array for parameters when
creating a new .NET-Object.

In Ax 4.0 this is unfortunately not possible.

We had the same problem and found only one solution (well, except to avoid
.NET :):

Extend the .NET class with parmMethods and provide a new constructor which
doesn't requires parameters.

That way you can first create the .NET-object (without forced to pass an
object array - the parameter object array is optional) and set the parmaters
with the parmMethods afterwards.

Hope you understand what I mean, if no, please post back.

Regards
Patrick

Philip

unread,
Dec 28, 2009, 12:35:01 AM12/28/09
to
Hi Patrick,

Thanks for your reply first. Actually, I wanna pass the mentioned object
array for parameters of a method when using the .Net-Object in X++, AX 4.0.
For example,
In .Net, there are
Class: MyNetClass2;
Class: MyNetClass1, method: MyNetMethod(MyNetClass2[] _myNetClass1)

In X++, I want to use the above method in this way:
MyNetClass1 myClass = new MyNetClass1();
MyNetClass2 anotherObject;
System.Array myArray = System.Array::CreateInstance(MyNetClass2.GetType(),

2);
// setup the array

anotherObject = new MyNetClass2();


myArray.SetValue(anotherObject,0);
myArray.SetValue(anotherObject,1);
// pass the array

myClass.MyNetMethod(myArray); // errors out with message: "The class


does not contain this function"

Do you have any idea about this? Thanks very much.

Philip

Luegisdorf

unread,
Dec 28, 2009, 8:01:01 AM12/28/09
to
Hi Philip

I think there are 2 ways to make it run:

Idea A)

In C#

create a new method on MyNetClass1 which looks like this:

public void addMyNetClass2(MyNetClass2 _arg)()
{
// add _arg to your list, or array, or like that
}

may be a clear funtion would also be nice:
public void clearMyNetClasses2(()
{
// clear your list, array or so on
}


After, modify your X++ code:

MyNetClass1 myClass = new MyNetClass1();
MyNetClass2 anotherObject;

;
anotherObject = new MyNetClass2();
myClass.addNetClass2(anotherObject);
anotherObject = new MyNetClass2();
myClass.addNetClass2(anotherObject);
// you could also use a for/next, while/next or do/while if you have to
add dynamic count of MyNEtClass2 instances


Idea B)

In c#. Just change the parameter profile of method MyNetMethod

Instead MyNetMethod(MyNetClass2[] _objects) you turn into MyNetMethod(Array
_objects)

Hope that works.
Best regards
Patrick

Adam

unread,
Jan 4, 2010, 3:40:01 PM1/4/10
to
I was having this same problem a while back. I never found a good solution
in AX so what I did was create a wrapper DLL in .NET to access the array
manually similar to what Patrick has mentioned. I added a List<T> and
methods for AddItem and FinishAddition to the class. To fill up the array I
called the AddItem() method for each item. The AddItem method just added the
object that I passed to the list. Then when I was done adding items, I
called the FinishAddition method which converted the List to an Array and
assigned the array property.

I also added methods, GetArrayCount and GetItem that I could call to get the
size of resulting arrays and that would return one item at a time from the
array given an index into the array. This way I was always dealing with the
array through the DLL.

There might be a better way of doing it through AX but I never found it.
Good luck

jorgeaviles

unread,
Jan 21, 2010, 4:02:09 PM1/21/10
to
EXAMPLE:

[Serializable()]
public class MyUser
{
private List<Item> items;

public MyUser()
{
items = new List<Item>();
}

public void AddItem(Item item)
{
items.Add(item);
}


public string ToXML()
{
string result = string.Empty;
this.items.ForEach(delegate(Item item)
{
result += item.GroupID + ",";
});
return "";
}
}

[Serializable()]
public class Item
{
private string groupID;

public string GroupID
{
get { return groupID; }
set { groupID = value; }
}
}


----------------------------
FROM DYNAMICS AX 4

static void Job3(Args _args)
{

Pounce.MyUser user = new Pounce.MyUser();
Pounce.Item item = new Pounce.Item();

List mylist;

;
item.set_GroupID("1");


user.AddItem(item);

pause;
}


SALUDOS

Luegisdorf wrote:

Hi PhilipI guess, you want to pass the mentioned object array for parameters
23-Dec-09

Hi Philip

I guess, you want to pass the mentioned object array for parameters when
creating a new .NET-Object.

In Ax 4.0 this is unfortunately not possible.

We had the same problem and found only one solution (well, except to avoid

..NET :):

Extend the .NET class with parmMethods and provide a new constructor which

does not requires parameters.

That way you can first create the .NET-object (without forced to pass an
object array - the parameter object array is optional) and set the parmaters
with the parmMethods afterwards.

Hope you understand what I mean, if no, please post back.

Regards
Patrick

"Philip" wrote:

Previous Posts In This Thread:

On Wednesday, December 23, 2009 12:07 AM
Philip wrote:

pass an array from x++ to a method in a .NET DLL
Here is a question which mentioned two years ago, and now not any solution
for this yet. Can anyone help me on this? Thanks.

Is it possible to pass an array from x++ to a method in a .NET DLL?

I can get an array as a return from my DLL into x++, but I am struggling with


passing the array back to the DLL. I get a compile error stating the class

does not contain the function (but it does). I am guessing it is a problem
with the parameter type.

For example, this works fine:
================
System.Array myArray;
MyDllClass myObject = new MyDllClass();
AnotherDllClass anotherObject;

myArray = myo.getArray();
anotherObject = myArray.GetValue(0); // gets the first object
================


But I cannot figure out how to pass an array back like this:


================
MyDllClass myClass = new MyDllClass();
AnotherDllClass anotherObject;
System.Array myArray = System.Array::CreateInstance(anotherObject.GetType(),
2);

// setup the array
anotherObject = new AnotherDllClass();
myArray.SetValue(anotherObject,0);
myArray.SetValue(anotherObject,1);

// pass the array
myClass.TakeArray(myArray); // errors out with message: "The class
does not contain this function"
==========================


Any help would be greatly appreciated!

On Wednesday, December 23, 2009 6:58 AM
Luegisdorf wrote:

Hi PhilipI guess, you want to pass the mentioned object array for parameters
Hi Philip

I guess, you want to pass the mentioned object array for parameters when
creating a new .NET-Object.

In Ax 4.0 this is unfortunately not possible.

We had the same problem and found only one solution (well, except to avoid

..NET :):

Extend the .NET class with parmMethods and provide a new constructor which

does not requires parameters.

That way you can first create the .NET-object (without forced to pass an
object array - the parameter object array is optional) and set the parmaters
with the parmMethods afterwards.

Hope you understand what I mean, if no, please post back.

Regards
Patrick

"Philip" wrote:

On Monday, December 28, 2009 12:35 AM
Philip wrote:

Hi Patrick,Thanks for your reply first.
Hi Patrick,

Thanks for your reply first. Actually, I wanna pass the mentioned object


array for parameters of a method when using the .Net-Object in X++, AX 4.0.
For example,
In .Net, there are
Class: MyNetClass2;
Class: MyNetClass1, method: MyNetMethod(MyNetClass2[] _myNetClass1)

In X++, I want to use the above method in this way:
MyNetClass1 myClass = new MyNetClass1();

MyNetClass2 anotherObject;
System.Array myArray = System.Array::CreateInstance(MyNetClass2.GetType(),


2);
// setup the array

anotherObject = new MyNetClass2();


myArray.SetValue(anotherObject,0);
myArray.SetValue(anotherObject,1);
// pass the array

myClass.MyNetMethod(myArray); // errors out with message: "The class


does not contain this function"

Do you have any idea about this? Thanks very much.

Philip

"Luegisdorf" wrote:

On Monday, December 28, 2009 8:01 AM
Luegisdorf wrote:

Hi PhilipI think there are 2 ways to make it run:Idea A)In C#create a new
Hi Philip

Idea A)

In C#


Idea B)

"Philip" wrote:

On Monday, January 04, 2010 3:40 PM
Adam wrote:

I was having this same problem a while back.
I was having this same problem a while back. I never found a good solution
in AX so what I did was create a wrapper DLL in .NET to access the array
manually similar to what Patrick has mentioned. I added a List<T> and
methods for AddItem and FinishAddition to the class. To fill up the array I
called the AddItem() method for each item. The AddItem method just added the
object that I passed to the list. Then when I was done adding items, I
called the FinishAddition method which converted the List to an Array and
assigned the array property.

I also added methods, GetArrayCount and GetItem that I could call to get the
size of resulting arrays and that would return one item at a time from the
array given an index into the array. This way I was always dealing with the
array through the DLL.

There might be a better way of doing it through AX but I never found it.
Good luck

"Luegisdorf" wrote:


Submitted via EggHeadCafe - Software Developer Portal of Choice
JavaScript - prevent multiple form submittals
http://www.eggheadcafe.com/tutorials/aspnet/2f417b1b-ac35-431a-b56c-37ea0b80d72b/javascript--prevent-mult.aspx

0 new messages