Uredi

Deli z drugimi prek


AttributeCollection.Contains Method

Definition

Determines whether this collection of attributes has the specified attribute or array of attributes.

Overloads

Contains(Attribute)

Determines whether this collection of attributes has the specified attribute.

Contains(Attribute[])

Determines whether this attribute collection contains all the specified attributes in the attribute array.

Contains(Attribute)

Source:
AttributeCollection.cs
Source:
AttributeCollection.cs
Source:
AttributeCollection.cs

Determines whether this collection of attributes has the specified attribute.

public:
 bool Contains(Attribute ^ attribute);
public bool Contains (Attribute attribute);
public bool Contains (Attribute? attribute);
member this.Contains : Attribute -> bool
Public Function Contains (attribute As Attribute) As Boolean

Parameters

attribute
Attribute

An Attribute to find in the collection.

Returns

true if the collection contains the attribute or is the default attribute for the type of attribute; otherwise, false.

Examples

The following code example checks to see whether the collection has a BrowsableAttribute set to true. It assumes that button1 and textBox1 have been created on a form.

protected:
   void ContainsAttribute()
   {
      // Creates a new collection and assigns it the attributes for button1.
      AttributeCollection^ attributes;
      attributes = TypeDescriptor::GetAttributes( button1 );
      
      // Sets an Attribute to the specific attribute.
      BrowsableAttribute^ myAttribute = BrowsableAttribute::Yes;

      if ( attributes->Contains( myAttribute ) )
      {
         textBox1->Text = "button1 has a browsable attribute.";
      }
      else
      {
         textBox1->Text = "button1 does not have a browsable attribute.";
      }
   }
private void ContainsAttribute() {
    // Creates a new collection and assigns it the attributes for button1.
    AttributeCollection attributes;
    attributes = TypeDescriptor.GetAttributes(button1);

    // Sets an Attribute to the specific attribute.
    BrowsableAttribute myAttribute = BrowsableAttribute.Yes;

    if (attributes.Contains(myAttribute))
       textBox1.Text = "button1 has a browsable attribute.";
    else
       textBox1.Text = "button1 does not have a browsable attribute.";
 }
Private Sub ContainsAttribute
    ' Creates a new collection and assigns it the attributes for button.
    Dim attributes As AttributeCollection
    attributes = TypeDescriptor.GetAttributes(button1)

    ' Sets an Attribute to the specific attribute.
    Dim myAttribute As BrowsableAttribute = BrowsableAttribute.Yes

    If Attributes.Contains(myAttribute) Then
        textBox1.Text = "button1 has a browsable attribute."
    Else
        textBox1.Text = "button1 does not have a browsable attribute."
    End If
End Sub

Remarks

This collection has the specified attribute if the specified type of attribute exists in the collection, and if the value of the specified attribute is the same as the value of the instance of the attribute in the collection.

The difference between the Matches and Contains methods is that Matches calls the Match method on an attribute, and Contains calls the Equals method.

For most attributes, these methods do the same thing. For attributes that may have multiple flags, however, Match is typically implemented so that it returns true if any of the flags are satisfied. For example, consider a data binding attribute with the Boolean flags "SupportsSql", "SupportsOleDb", and "SupportsXml". This attribute may be present on a property that supports all three data-binding approaches. It will often be the case that a programmer needs to know only if a particular approach is available, not all three. Therefore, a programmer could use Match with an instance of the attribute containing only the flags the programmer needs.

See also

Applies to

Contains(Attribute[])

Source:
AttributeCollection.cs
Source:
AttributeCollection.cs
Source:
AttributeCollection.cs

Determines whether this attribute collection contains all the specified attributes in the attribute array.

public:
 bool Contains(cli::array <Attribute ^> ^ attributes);
public bool Contains (Attribute[] attributes);
public bool Contains (Attribute[]? attributes);
member this.Contains : Attribute[] -> bool
Public Function Contains (attributes As Attribute()) As Boolean

Parameters

attributes
Attribute[]

An array of type Attribute to find in the collection.

Returns

true if the collection contains all the attributes; otherwise, false.

Examples

The following code example compares the attributes in button1 and textBox1 to see whether the attributes for the button are contained in the attributes for the text box. It assumes that both button1 and textBox1 have been created on a form.

private:
   void ContainsAttributes()
   {
      // Creates a new collection and assigns it the attributes for button1.
      AttributeCollection^ myCollection;
      myCollection = TypeDescriptor::GetAttributes( button1 );
      
      // Checks to see whether the attributes in myCollection are the attributes for textBox1.
      array<Attribute^>^ myAttrArray = gcnew array<Attribute^>(100);
      TypeDescriptor::GetAttributes( textBox1 )->CopyTo( myAttrArray, 0 );
      if ( myCollection->Contains( myAttrArray ) )
      {
         textBox1->Text = "Both the button and text box have the same attributes.";
      }
      else
      {
         textBox1->Text = "The button and the text box do not have the same attributes.";
      }
   }
private void ContainsAttributes() {
   // Creates a new collection and assigns it the attributes for button1.
   AttributeCollection myCollection;
   myCollection = TypeDescriptor.GetAttributes(button1);

   // Checks to see whether the attributes in myCollection are the attributes for textBox1.
   Attribute[] myAttrArray = new Attribute[100];
   TypeDescriptor.GetAttributes(textBox1).CopyTo(myAttrArray, 0);
   if (myCollection.Contains(myAttrArray))
      textBox1.Text = "Both the button and text box have the same attributes.";
   else
      textBox1.Text = "The button and the text box do not have the same attributes.";
}
Private Sub ContainsAttributes()
    ' Creates a new collection and assigns it the attributes for button1.
    Dim myCollection As AttributeCollection
    myCollection = TypeDescriptor.GetAttributes(button1)
      
    ' Checks to see whether the attributes in myCollection are the attributes for textBox1.
    Dim myAttrArray(100) As Attribute
    TypeDescriptor.GetAttributes(textBox1).CopyTo(myAttrArray, 0)
    If myCollection.Contains(myAttrArray) Then
        textBox1.Text = "Both the button and text box have the same attributes."
    Else
        textBox1.Text = "The button and the text box do not have the same attributes."
    End If
End Sub

Remarks

This collection has the specified array of attributes if all the specified attribute types exist in the collection and if each attribute in the specified array is the same as an attribute in the collection.

See also

Applies to