Visual Cobol Managed & interfaces

[Migrated content. Thread originally posted on 01 December 2011]

Hello,

I need to implement a little class inside a form, and should implement the interface 'ICompare':

class ListViewItemComparer : IComparer

I do not know how to code this in Visual COBOL, sorry.

Regards
  • The syntax for interfaces in managed COBOL is "implements type". So, your example should look like this:

    class-id ListViewItemComparer public implements type IComparer
  • Thank you.

    Now I am trying to translate the last of these methos:
        // Implements the manual sorting of items by columns.
        class ListViewItemComparer : IComparer
        {
            private int col;
            public ListViewItemComparer()
            {
                col = 0;
            }
            public ListViewItemComparer(int column)
            {
                col = column;
            }
            public int Compare(object x, object y)
            {
                return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
            }
        }

    I find it extremely tricky. Arrived at this point, I do not know how to continue:
          $set ilusing"System.Collections"
           class-id MngListView.ListViewItemComparer public implements type IComparer.

           
           working-storage section.
           01  myColumn             pic 9.

           method-id InstanceMethod.
           local-storage section.
           procedure division.

               goback.           
           end method.
           
           method-id ListViewItemComparer final public.
           procedure division.
               set myColumn to 0.
           end method.
           

           method-id ListViewItemComparer final public.
           linkage section.
           01 newColumn         pic 9.
           procedure division using by value newColumn.
               set myColumn to newColumn.
           end method.

           method-id Compare final public.
           working-storage section.
           01 resultCompare    type Int32.
           01 string1          type String.
           01 string2          type String.
           01 item1            type ListViewItem.
           01 item2            type ListViewItem.
           linkage section.
           01 x                type Object.
           01 y                type Object.
           01 result           type Int32.
           procedure division using by value x, y returning result.
               set item1 to x as type ListViewItem.
               set item2 to y as type ListViewItem.
               *>set string1 to item1::SubItems(myColumn) as String.
               
               *>return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
               set result to resultCompare.
           end method.
           
           end class.

    Could you give me a hand on this subject? I am afraid I am stuck.

    Thank you.
  • The problem you are having is in your ListViewItemComparer. The interface defines the parameter and return values to be of type int. the COBOL equivalent of this would be binary-long but you are using pic 9 which is incompatible.
  • Verified Answer

    Something like this might work:


          $set ilusing"System.Collections"
           class-id MngListView.ListViewItemComparer implements type IComparer.

           working-storage section.
           01 mycolumn binary-long.
           method-id New.
           local-storage section.
           procedure division.
               set mycolumn to 0
               goback.
           end method.

           method-id New.
           local-storage section.
           procedure division using lnkcolumn as binary-long.
               set mycolumn to lnkcolumn
               goback.
           end method.

           method-id Compare.
           local-storage section.
           01 lsx   type ListViewItem.
           01 lsy   type ListViewItem.
           procedure division using by value #x as object #y as object returning return-item as binary-long.
              set lsx to #x as type ListViewItem
              set lsy to #y as type ListViewItem
              set return-item to type String::Compare(lsx::SubItems::Item(mycolumn)::Text, lsy::SubItems::Item(mycolumn)::Text)
           end method.
         






  • Hello Chris,

    Thanks a lot for your help! I was really at my wits end with that issue.

    It worked out perfectly.

    The adaptation to this new language is really hard, but it is also worth learning it.

  • You are welcome.

    We agree completely that the new syntax is well worth the effort of learning.

    As with anything, the more you use it the easier it will become :-)