次の方法で共有


ConstraintCollection.IndexOf メソッド (Constraint)

指定した Constraint のインデックスを取得します。

Overloads Public Function IndexOf( _
   ByVal constraint As Constraint _) As Integer
[C#]
public int IndexOf(Constraintconstraint);
[C++]
public: int IndexOf(Constraint* constraint);
[JScript]
public function IndexOf(
   constraint : Constraint) : int;

パラメータ

戻り値

Constraint がコレクション内に存在する場合はその制約の 0 から始まるインデックス。それ以外の場合は -1。

解説

IndexOf メソッドを使用して、 Contains メソッドまたは Remove メソッドで使用するインデックスを返します。

使用例

[Visual Basic, C#, C++] IndexOf メソッドを使用して Constraint のインデックスを返す例を次に示します。コレクションからその制約を削除する前に、 Contains メソッドにインデックスが渡され、コレクションにその制約が含まれているかどうかが確認されます。

 
Private Sub RemoveConstraint _
(myConstraints As ConstraintCollection, myConstraint As Constraint)
    Try
        If myConstraints.Contains(myConstraint.ConstraintName) Then
            If myConstraints.CanRemove(myConstraint) Then
                myConstraints.RemoveAt _
                (myConstraints.IndexOf(myConstraint))
            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#] 
private void RemoveConstraint(ConstraintCollection myConstraints, Constraint myConstraint){
   try{
      if(myConstraints.Contains(myConstraint.ConstraintName)) {
         if(myConstraints.CanRemove(myConstraint)) {
            myConstraints.RemoveAt(myConstraints.IndexOf(myConstraint));
         }
      }
   }
   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++] 
private:
 void RemoveConstraint(ConstraintCollection* myConstraints, Constraint* myConstraint){
    try{
       if(myConstraints->Contains(myConstraint->ConstraintName)) {
          if(myConstraints->CanRemove(myConstraint)) {
             myConstraints->RemoveAt(myConstraints->IndexOf(myConstraint));
          }
       }
    }
    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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

ConstraintCollection クラス | ConstraintCollection メンバ | System.Data 名前空間 | ConstraintCollection.IndexOf オーバーロードの一覧 | Contains | Remove | CanRemove