ComponentOne List for WinForms
In This Topic
    Working with Collections
    In This Topic

    A C1List object has eight separate collections which govern its diverse objects. Each of these collections has an associated property within the C1List object which returns the collection object. This prevents the need for the programmer to enter the entire collection name when using the list in code. The following table outlines these mappings:

    Collection Associated Property
    C1DataColumnCollection Columns property
    C1DisplayColumnCollection DisplayColumns property
    GridStyleCollection Styles property
    SelectedColumnCollection SelectedCols property
    SelectedRowCollection SelectedIndices property
    SplitCollection Splits property
    ValueItemCollection Values property

    By default, the SplitCollection object contains one Split object. The GridStyleCollection object contains these default Style objects: Normal, Heading, Footing, Selected, Caption, HighlightRow, EvenRow, OddRow, and RecordSelector.

    You can reference an object in a collection using its zero-based index. You can read or set the Split object's properties as follows:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Read a Split object property.      
    variable = Me.C1List1.Splits(0).Property   
                                            
    ' Set a Split object property.    
    Me.C1List1.Splits(0).Property = variable
    

    To write code in C#

    C#
    Copy Code
    // Read a Split object property. 
    variable = this.c1List1.Splits[0].Property;
                                            
    // Set a Split object property. 
    this.c1List1.Splits[0]. = variable;
    

    You can create a reference to an object in a collection using the collection's Item method. The following code creates a reference to a list's default Split object:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Declare Split0 as a Split object. 
    Dim Split0 As C1.Win.C1List.Split
                                    
    ' Set Split0 to reference the first Split in the collection. 
    Split0 = Me.C1List1.Splits.Item(0)
    

    To write code in C#

    C#
    Copy Code
    // Declare Split0 as a Split object.   
    C1.Win.C1List.Split Split0; 
                                            
    // Set Split0 to reference the first Split in the collection.        
    Split0 = this.c1List1.Splits.Item[0];
    

    Note the use of the namespace qualifier in the preceding example. Using the namespace qualifier is recommended in order to resolve potential naming conflicts with other controls. For example, if you use another control in the same project that also defines an object named Split, the C1List namespace qualifier is required, as is the namespace qualifier for the other control.

    Since the Item method is implicit for collections, you can omit it:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Declare Split0 as a Split object. 
    Dim Split0 As C1.Win.C1List.Split
       
    ' Set Split0 to reference the first Split in the collection.    
    Split0 = Me.C1List1.Splits(0)
    

    To write code in C#

    C#
    Copy Code
    // Declare Split0 as a Split object. 
    C1.Win.C1List.Split Split0;
        
    // Set Split0 to reference the first Split in the collection.    
    Split0 = this.c1List1.Splits[0];
    

    You can now use Split0 to read or set the Split object's properties or to execute its methods:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Read a Split object property.    
    variable = Split0.Property
       
    ' Set a Split object property.    
    Split0.Property = variable
        
    ' Execute a Split object method.  
    Split0.Method (arg1, arg2, ...)
    

    To write code in C#

    C#
    Copy Code
    // Read a Split object property. 
    variable = Split0;
            
    // Set a Split object property.      
    Split0 = variable;
        
    // Execute a Split object method.    
    Split0.Method(arg1, arg2, ...);
    

    Very often, you need to read and set more than one of an object's properties. For example:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Read a Split object's properties.   
    variable1 = Me.C1List1.Splits(0,0).Property1    
    variable2 = Me.C1List1.Splits(0,0).Property2
       
    ' Set a Split object's properties.      
    Me.C1List1.Splits(0,0).Property1 = variable1
    Me.C1List1.Splits(0,0).Property2 = variable2
    

    To write code in C#

    C#
    Copy Code
    // Read a Split object's properties.   
    variable1 = this.c1List1.Splits[0, 0].Property1;    
    variable2 = this.c1List1.Splits[0, 0].Property2;
        
    // Set a Split object's properties.    
    this.c1List1.Splits[0, 0].Property1 = variable1;   
    this.c1List1.Splits[0, 0].Property2 = variable2;
    

    This code is very inefficient because of the number of times the object C1List1.Splits(0,0) is accessed. It is more efficient to create a single reference to the object up front and use it repeatedly:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Declare Split0 as a Split.      
    Dim Split0 As C1List.Split
     
    ' Set Split0 to reference the first Split in the collection.   
    Split0 = Me.C1List1.Splits.Item(0,0)
     
    ' Read a Split object's properties.       
    variable1 = Split0.Property1      
    variable2 = Split0.Property2
        
    ' Set a Split object's properties.       
    Split0.Property1 = variable1
    Split0.Property2 = variable2
    

    To write code in C#

    C#
    Copy Code
    // Declare Split0 as a Split.     
    C1List.Split Split0;
    
    // Set Split0 to reference the first Split in the collection.      
    Split0 = this.c1List1.Splits[0,0];
       
    // Read a Split object's properties.       
    variable1 = Split0.Property1; 
    variable2 = Split0.Property2;
     
    // Set a Split object's properties.       
    Split0.Property1 = variable1;     
    Split0.Property2 = variable2;
    

    This code is much more efficient and easier to read. If your Visual Studio application accesses collection objects frequently, you can improve the performance of your code significantly by adhering to these guidelines.

    Similarly, you can apply this technique to other objects and collections of C1List, and of Visual Studio in general. Of particular importance to the list are the C1DataColumn object and C1DataColumnCollection object (also applies to C1DisplayColumn object):

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Declare Cols as a Columns collection object, then set it to reference C1List1's C1DataColumnCollection object.  
    Dim Cols As C1List.C1DataColumnCollection   
    Cols = Me.C1List1.Columns
       
    ' Declare Col0 as a DataColumn object, then set it to reference the first Column object in the collection.    
    Dim Col0 As C1List.C1DataColumn     
    Col0 = Cols(0)
        
    ' Read and set the DataColumn object's Property1.   
    variable1 = Col0.Property1
    Col0.Property1 = variable1
    
    ' Execute the DataColumn object's Method1 (declared as a Sub).    
    Col0.Method1(arg1, arg2, ...)
    
    ' Execute the DataColumn object's Method2 (declared as a Function).     
    variable2 = Col0.Method2(arg1)
    

    To write code in C#

    C#
    Copy Code
    // Declare Cols as a Columns collection object, then set it to reference C1List1's C1DataColumnCollection object. 
    C1List.C1DataColumnCollection Cols;   
    Cols = this.C1List1.Columns;
     
    // Declare Col0 as a DataColumn object, then set it to reference the first Column object in the collection.     
    C1List.C1DataColumn Col0;  
    Col0 = Cols[0];
      
    // Read and set the DataColumn object's Property1.    
    variable1 = Col0.Property1;    
    Col0.Property1 = variable1;
     
    // Execute the DataColumn object's Method1 (declared as a Sub).     
    Col0.Method1(arg1, arg2, ...);
      
    // Execute the DataColumn object's Method2 (declared as a Function). 
    variable2 = Col0.Method2(arg1);
    

    Visual Basic also provides an efficient With...End With statement for setting multiple properties of an object without explicitly assigning it to a variable. For example, the following code sets multiple properties of the first column of a list (recall that collections are zero-based):

    To write code in Visual Basic

    Visual Basic
    Copy Code
    With Me.C1List1.Columns(0)   
     .Property1 = variable1  
     .Property2 = variable2   
    End With
    

    To write code in C#

    C#
    Copy Code
    this.c1List1.Columns[0].Property1 = variable1;   
    this.c1List1.Columns[0].Property2 = variable2;