Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

How to set values in a list of objects

(0) ShareShare
ReportReport
Posted on by 76

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;
    }

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: How to set values in a list of objects

    You can get more about ArrayList, including the containing assembly, in ArrayList documention.

    Sorry about the bug, it's partially because I wrote code directly here (without a compiler) and partially because of bad naming on your side. The best approach will be distingushing those two Lines elements by choosing a better name for one of them (based on the business domain, which is unknown to me). Line isn't a good name, because it implies that the property holds a single line, not a collection of lines.

  • RadekM Profile Picture
    RadekM 76 on at
    RE: How to set values in a list of objects

    I tried to add System.Collections.ArrayList yet I can't find it, even installed System.Collections.NonGeneric that is added to reference in my project yet again I cant call it with 'using'.

    Does 'Lines' name is right? It gives me error member names cannot be the same as their enclosing type'. When I change it to Line the error is gone.

    private readonly List<Line> Lines { get; } = new List<Line>();

    Line dones not contain definition for Add. Does the Add method come for System.Collections.ArrayList?

  • Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: How to set values in a list of objects

    The non-generic list in .NET is called System.Collections.ArrayList.

    But note that you class may use a generic collection while exposing a non-generic interface. For example:

    public class Lines
    {
    	private readonly List Lines { get; } = new List();
    
    	public void AddLine(Line line)
    	{
    		Lines.Add(line);
    	}
    }

  • RadekM Profile Picture
    RadekM 76 on at
    RE: How to set values in a list of objects

    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?

  • Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: How to set values in a list of objects

    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
    RadekM 76 on at
    RE: How to set values in a list of objects

    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
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: How to set values in a list of objects

    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
    RadekM 76 on at
    RE: How to set values in a list of objects

    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
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: How to set values in a list of objects

    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
    RadekM 76 on at
    RE: How to set values in a list of objects

    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;

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,836 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans