This documentation is archived and is not being maintained.

ConstraintCollection.Contains Method

Indicates whether the Constraint object specified by name exists in the collection.

[Visual Basic]
Public Function Contains( _
   ByVal name As String _
) As Boolean
[C#]
public bool Contains(
 string name
);
[C++]
public: bool Contains(
 String* name
);
[JScript]
public function Contains(
   name : String
) : Boolean;

Parameters

name
The ConstraintName of the constraint.

Return Value

true if the collection contains the specified constraint; otherwise, false.

Remarks

Use the Contains method to determine if the specified Constraint exists before attempting to remove it from the collection. You can also use the CanRemove method to determine if a Constraint can be removed.

Example

[Visual Basic, C#, C++] The following example determines whether the specified Constraint exists in the ConstraintCollection prior to its deletion.

[Visual Basic] 

 Public Shared Sub RemoveConstraint(myConstraints As ConstraintCollection, myConstraint As Constraint)
    Try
        If myConstraints.Contains(myConstraint.ConstraintName) Then
            If myConstraints.CanRemove(myConstraint)
                myConstraints.Remove(myConstraint.ConstraintName)
            End If
        End If
    Catch e As Exception
        ' Process exception and return.
                Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
                log.Source = "My Application"
                log.WriteEntry(e.ToString())
                Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
    End Try
 End Sub


[C#] 
public static void RemoveConstraint(ConstraintCollection myConstraints, Constraint myConstraint)
{
    try
    {
        if(myConstraints.Contains(myConstraint.ConstraintName)) 
        {
            if(myConstraints.CanRemove(myConstraint)) 
            {
                myConstraints.Remove(myConstraint.ConstraintName);
            }
        }
    }
    catch(Exception e) 
    {
        // Process exception and return.
                System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
                log.Source = "My Application";
                log.WriteEntry(e.ToString());
                Console.WriteLine("Exception of type {0} occurred.", e.GetType());
    }
}

[C++] 
public:
    static void RemoveConstraint(ConstraintCollection* myConstraints, Constraint* myConstraint)
    {
        try
        {
            if (myConstraints->Contains(myConstraint->ConstraintName))
            {
                if (myConstraints->CanRemove(myConstraint))
                {
                    myConstraints->Remove(myConstraint->ConstraintName);
                }
            }
        }
        catch (Exception* e)
        {
            // Process exception and return.
            System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
            log->Source = S"My Application";
            log->WriteEntry(e->ToString());
            Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
        }
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

ConstraintCollection Class | ConstraintCollection Members | System.Data Namespace | Remove

Show: