VERSION 1.0 CLASS
BEGIN
  MultiUse = -1   'True
END
Attribute VB_PredeclaredId = False
Attribute VB_Creatable = True
Attribute VB_Exposed = True
Attribute VB_GlobalNameSpace = True
Attribute VB_Name = "mpxAssignments"
' MPX-Assignments-X5.cls
' ======================

' Assignments collection Class Module
' Copyright (c) Mindjet LLC 2002, 2003

Option Explicit
'#Uses "MPX-Project-X5.Cls" ' mpxProject object Class module
'#Uses "MPX-Tasks-X5.Cls" ' mpxTasks collection class module
'#Uses "MPX-Task-X5.Cls" ' mpxTask object class module
'#Uses "MPX-Resources-X5.Cls" ' mpxResources collection Class module
'#Uses "MPX-Resource-X5.Cls" ' mpxResource object Class module
'#Uses "MPX-Assignment-X5.Cls" ' mpxAssignment object Class module

Dim i_AssignmentCount As Integer ' Assignments count
Dim o_Assignments() As mpxAssignment ' Assignments array

Private Sub Class_Initialize()
	i_AssignmentCount = 0
	Erase o_Assignments
End Sub

Private Sub Class_Terminate()
' Destroy Assignments collection
Dim i_1 As Integer
	For i_1 = 1 To i_AssignmentCount
		Set o_Assignments(i_1) = Nothing ' destroy task object
	Next i_1
	Erase o_Assignments
	i_AssignmentCount = 0
End Sub

Function Add(ByVal i_TaskIndex As Integer, ByVal i_ResourceIndex As Integer) As Integer
' Add a task/resource assignment
' Indexes refer to the task and resource indexes within this local project object
' Return the index number of this assignment
' Returns 0 if index numbers are not valid
Dim i_1 As Integer
Dim b_Found As Boolean
Dim i_TaskID As Long
Dim i_ResourceID As Long

	If (i_TaskIndex <= 0) Or (i_TaskIndex > o_Project1.Tasks.Count) Or _
		(i_ResourceIndex <= 0) Or (i_ResourceIndex > o_Project1.Resources.Count) Then
		Add = 0 ' cannot add this assignment
		Exit Function
	End If
	' Fetch the IDs for these objects
	i_TaskID = o_Project1.Tasks.Item(i_TaskIndex).ID
	i_ResourceID = o_Project1.Resources.Item(i_ResourceIndex).ID
	' Check this assignment does not already exist
	b_Found = False ' assume not
	For i_1 = 1 To i_AssignmentCount
		If o_Assignments(i_1).TaskID = i_TaskID And _
			o_Assignments(i_1).ResourceID = i_ResourceID Then
			b_Found = True
			Exit For ' leave loop
		End If
	Next i_1
	If b_Found Then
		Add = i_1 ' return this assignment
	Else ' add new assignment
		i_AssignmentCount = i_AssignmentCount + 1
		ReDim Preserve o_Assignments(1 To i_AssignmentCount)
		Set o_Assignments(i_AssignmentCount) = New mpxAssignment ' create new assignment object
		o_Assignments(i_AssignmentCount).TaskID = i_TaskID
		o_Assignments(i_AssignmentCount).ResourceID = i_ResourceID
		Add = i_AssignmentCount ' return index of new assignment entry
		o_Project1.Tasks.Item(i_TaskIndex).HasResources = True ' this task has resources
	End If

End Function

Property Get Item(ByVal i_index As Integer) As mpxAssignment
' Return an Assignment object from collection
	If (i_index < 1) Or (i_index > i_AssignmentCount) Then
		Err.Raise(1000,,"Invalid Assignments collection index",,)
	Else
		Set Item = o_Assignments(i_index)
	End If
End Property

Property Get Count() As Integer
' Return number of tasks in the tasks collection
	Count = i_AssignmentCount
End Property
