in a customization job, I make use of 2 different Arrays of Struct, like
Array = new Array(types::Class);
Array2 = new Array(types::Class);
Array.value(id, new Struct(types::real, "param1", types::Integer, "param2"));
Array2.value(id, new Struct(types::string, "param3", types::Integer, "param4"));
first array holds in real 23 values in the struct (23 seems to be the limit in Struct)
So thats why I created a 2nd Array, cause I have more values.
What I experience is that after making a modification and start the code again, some values of the Struct are old values.
It looks like the Structs are not fully cleaned or initialized in memory.
Is this a memory leak?
Is finalize() the way to fix this, if yes how to use finalize exactly?
What can be the problem of this behaviour?
Why are you using Struct at all, especially if you're saying that it doesn't meet your requirements?
If you have a bunch of named values, why don't you design a class for that? If you don't know them at design time, you can use a Map object to hold name-value pairs.
Well.......because I know how to handle struct.
And am not into design classes.
I get the concept of classes, but to design from zero is not my cup of tea (yet)
Could you ls give me a headstart with a class for a bunch of value pairs?
It's a critical piece of knowledge - object-oriented programming (OOP) is all about objects (= instances of classes), therefore understanding them is crucial. If you don't know the basics, you simply must go and learn them. Fortunately internet is full of great resources.
Also realize that it's nothing specific to AX and AX resources won't teach you OOP. But that's not a problem; you can learn concepts from books written for other languages (e.g. I got the basics from Thinking in Java).
When you need something specific to AX, you can consult documentation. I would also highly recommend downloading and reading Inside Dynamics AX 4.0 (it's free!). Yes, it's for a newer version, but many things are the same and you'll learn a lot from it.
Now to the actual implementation. The class will be very simple - it will have several variables in ClassDesclaration, holding the values. Such as:
But these variables can't be accessed by other classes, therefore you need methods allowing to read and modify values from outside.
For each variable, you could write a "get" method returning the current value and a "set" method changing it, but the usual approach in AX is combining it to a single "parm" method. Like this:
public str parmName(str _name = name)
name = _name;
When you have that, you can create an instance of your class and use the methods to manipulate values. Because methods has meaningful names, it's easy to understand what's going on:
MyClass o = new MyClass();
Note that the variables may hold other objects too, not just primitive data types such as int.
Therefore if you have values that logically belongs together, you can put them all together to a separate class, rather than mixing them with unrelated values. You'll later learn how important it is to split the application to logical pieces.
Thanks Martin for the headstart.
This concept is totally clear for me and implementing it right now.
Thanks for the links too, it will give me a deeper understanding of Classes, so in future I can create it myself and be sure it's done good.
I understand what you're saying.
I can create also Extended datatypes, enums and primitive datatypes etc in the Class.
But I can do this in 1 class I guess?. Those are all different types, belonging to the same process.
Or do you mean to make a separate class for the enums in the process and a separate for the extended datatypes?
What's the benefit of that?
You can use any type inside a class. A few examples:
int counter; // number
FileType fileType; // enum
UserInfo user; // table
InventMovement inventMov; // class
List list; // collection class
You should group data logically, not based on data types; a class should encapsulate data and logic of a certain logical entity. For example, SysRecurrence class has a unit (SysRecurrenceUnit enum), a start time (number), an end date (date) etc., together with related business logic (e.g. nextDateFromToday() method).
By the way, the usual OOP languages don't have the concept of tables; everything is a class there. But in AX, you must decide what we'll be modeled as a class and what as a table (possibly a temporary one).
All clear, thanks for your help!
Now that I'm busy with it.
Should I continue with Array = new Array(types::Class);
And then call Array.value(_id, o.parmname());
Or is there a way to initialize several instances of this same class, and identify those classes by an _id.
I think it should be possible, but where can I find an example of how this is done.
Can you please explain what you're trying to achieve? Ideally start with telling us a bit about the business requirement you're dealing with. Then it should be clearer what the technical solution needs to cover.
Of course you can create any number of instances of the same class. Like this:
MyClass c1 = new MyClass();
MyClass c2 = new MyClass();
You can put them into an array if you want, but I don't see any reason why you would put individual fields into an array. It looks like you stick to the wrong code that you designed before learning about classes. Again, an explanation of what you're doing would give me a better idea.