web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to set values in a list of objects

(0) ShareShare
ReportReport
Posted on by 172

How can I create a list with objects containing a set of values to feed XML.

I do have declared :        public List<line> line{ get; set; }     

I need to create multiple objects of line for every orderline. I'm sure the problem is in IEnumerable declaration yet i don't knwo how to fix it. Right now the code ends at getEnumerator() since its null.

 protected mySchema.lines createlines()
    {
        mySchema.lines lines = new mySchema.lines();

        lines.zz = '3';
        lines.xx = '100.00';
       
        System.Collections.IEnumerable list = lines.line;
        System.Collections.IEnumerator enumerator = list.GetEnumerator();
 
        while  (enumerator.MoveNext())
        {
          line myline = enumerator.current;
          myline  = this.createline();
        }
        return FaWiersze;
    }

    protected mySchema.line createline()
    {

        mySchema.line line = new mySchema.line();

        line.qq = '1';
        line.ww = 'aaa';
        line.ee = 'bbb';
        line.rr = 'ccc.';

        return line;
    }

I have the same question (0)
  • Martin Dráb Profile Picture
    237,898 Most Valuable Professional on at

    It seems that you forgot to provide some details.

    Do youu mean that you have a C# class library with mySchema.lines class and you've created line property of type List<line>? (Note that you violated C# naming conventions.) And now you're trying to use it in X++. Did I guess everything correctly?

    When you said that " code ends at getEnumerator() since its null", you likely meant list (lines.line) by it. It's indeed always empty, because you never set any value into it.

  • RadekM Profile Picture
    172 on at

    Yes, I have a C# class library and I try to set values for mySchema in X++ class using this library. I need to create a xml and recreate the same structure with different values. Thats why I declared the List<line> but I don't know how to correctly create it. How can I set it correct? It always overwrittes the provious created object or like above I failed on creating the list.line.

  • Martin Dráb Profile Picture
    237,898 Most Valuable Professional on at

    If your goal is creating an object, than trying to iterate the collection has nothing to do with your goal and the fix for this part of code is throwing it away.

    X++ has a very limited support for generics (and Microsoft seems to pretend that there is none, likely because they did it just partially and then stopped the investment), therefore the best thing you can do is designing your class in a way that can be easily consumed from X++. For example, initialize the value of line property when creating lines object and provide AddLine(line) method to add a line from X++. Or use a non-generic collection.

    In fact, it's possible to create an instance of a generic collection (as I showed in Generic types in X++ (AX 7)), but as I said, a better approach is designing your class in a way that you don't need to work with generic types in X++.

  • RadekM Profile Picture
    172 on at

    Well I tried te below. Yet I can't assign the list or array to my lines.line. As far I know array is non generic but still I cant make it work. 

    The errors I get it Generic.List

    Error Cannot implicitly convert from type 'System.Collections.Generic.List' to type 'System.Collections.Generic.List'1.
    Error Cannot implicitly convert from type 'Array' to type 'System.Array'.

            Array Arr = new Array (Types::Class);
            int   j = 1;
    
            while(j <= 3)
            {
                Arr.value(j, this.createline());
                j  ;
            }
            lines.line = Arr;
    
    
            System.Collections.Generic.List MyLine = new System.Collections.Generic.List();
            MyLine.Add(this.createline());
            MyLine.Add(this.createline());
            MyLine.Add(this.createline());
            lines.line = MyLine;

  • Martin Dráb Profile Picture
    237,898 Most Valuable Professional on at

    The first part of your code makes no sense. The Array class in X++ is a completely different type than your List<line>. That's what the second error message is about.

    Where do you get the first error?

  • RadekM Profile Picture
    172 on at

    Sorry forgot to mention that for the test case I changed List<line> to Array line. The first part (line 1-9) is for Array line and the second part is for List<line> just added it all together so it may look confusing.

    Both error occours on the same line, when I try to set the value lines.line = Arr; / lines.line = MyLine;

  • Martin Dráb Profile Picture
    237,898 Most Valuable Professional on at

    If I understand you correctly, you're saying that the type of lines.is List<line>, but the following code fails at the third line with "Cannot implicitly convert from type 'System.Collections.Generic.List' to type 'System.Collections.Generic.List'1". Is that correct?

    mySchema.lines lines = new mySchema.lines();
    System.Collections.Generic.List MyLine = new System.Collections.Generic.List();
    lines.line = MyLine;

    Should it actually be List<myschema.line> instead of List<line>?

  • RadekM Profile Picture
    172 on at

    The lines class looks like below. And yes, the code fails on line 3

    Edit. chaning to MySchema.line actually didnt changed, the error is still the same

    mySchema.lines lines = new mySchema.lines();
    System.Collections.Generic.List MyLine = new System.Collections.Generic.List();
    lines.line = MyLine;

        [XmlType(TypeName = "Lines")]
        public class Lines
        {
            public String LinesNum { get; set; }        
            [XmlElement(ElementName = "Line")]
            public List Line{ get; set; }         
        }

  • Martin Dráb Profile Picture
    237,898 Most Valuable Professional on at

    CLR Interop is case-senstive therefore mySchema and MySchema are twodifferent names. The same applies to Lines vs lines and Line vs line.

    Assuming that the namespace is called mySchema, the code should look like this, if you insist on ignoring my recommendation to avoid generics in X :

    mySchema.Lines lines = new mySchema.Lines();
    System.Collections.Generic.List myLine = new System.Collections.Generic.List();
    lines.Line = myLine;

  • RadekM Profile Picture
    172 on at

    I have all names remade so it;s perfectly ideantical. Yet the problem is still there.

    What other non generic type could I use to archive my goal?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans