GPPointFileInfo\GPInputFileFormat.vb
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\GPInputFileFormat.vb
' Copyright 2008 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
' 

Imports Microsoft.VisualBasic
Imports System
Imports ESRI.ArcGIS.Geoprocessing
Imports ESRI.ArcGIS.Geodatabase

Namespace GPPointFileInfo
  Public Class GPInputFileFormat
        Implements GPPointFileInfo.IGPInputFileFormat
    Private Shared _parameterIndex As Integer

    Private _fileFormat As String

    Public Sub New(ByVal parameterValueArray As IParameterValueArray)
      Dim gpValue As IGPValue = parameterValueArray.GetGPValue(_parameterIndex)

      Dim fileFormat As String = GetFileFormat(gpValue)

      ValidateFileFormat(fileFormat)

      _fileFormat = fileFormat
    End Sub

    Public Shared Function GetGPParameterEdit() As IGPParameterEdit
      Dim gpParameterEdit As IGPParameterEdit = New GPParameterClass()

      Dim gpDataType As IGPDataType = New GPStringTypeClass()

      Dim gpCodedValueDomain As IGPCodedValueDomain = GetGPCodedValueDomain()

            Dim gpValue As IGPValue = gpCodedValueDomain.Value(0)

      Dim gpDomain As IGPDomain = TryCast(gpCodedValueDomain, IGPDomain)

      gpParameterEdit.Name = "in_input_file_format"
      gpParameterEdit.DisplayName = "Input File Format"
      gpParameterEdit.DataType = gpDataType
      gpParameterEdit.Value = gpValue
      gpParameterEdit.Direction = esriGPParameterDirection.esriGPParameterDirectionInput
      gpParameterEdit.ParameterType = esriGPParameterType.esriGPParameterTypeRequired
      gpParameterEdit.Domain = gpDomain

      Return gpParameterEdit
    End Function

    Private Shared Function GetGPCodedValueDomain() As IGPCodedValueDomain
      Dim gpCodedValueDomain As IGPCodedValueDomain = New GPCodedValueDomainClass()

      gpCodedValueDomain.AddStringCode("XYZ", "XYZ")
      gpCodedValueDomain.AddStringCode("GENERATE", "GENERATE")
      gpCodedValueDomain.AddStringCode("LAS", "LAS")

      Return gpCodedValueDomain
    End Function

    Public Shared Sub SetGPParameterIndex(ByVal index As Integer)
      _parameterIndex = index
    End Sub

        Public ReadOnly Property FileFormat() As String Implements IGPInputFileFormat.FileFormat
            Get
                Return _fileFormat
            End Get
        End Property

    Private Function GetFileFormat(ByVal gpValue As IGPValue) As String
      Dim fileFormat As String

      Dim gpString As IGPString = TryCast(gpValue, IGPString)

      fileFormat = gpString.Value

      Return fileFormat
    End Function

    Private Sub ValidateFileFormat(ByVal fileFormat As String)
      Dim gpCodedValueDomain As IGPCodedValueDomain = GetGPCodedValueDomain()

      Dim gpString As IGPString = New GPStringClass()

      gpString.Value = fileFormat

      Dim gpValue As IGPValue = TryCast(gpString, IGPValue)

      Dim name As String

      name = gpCodedValueDomain.FindName(gpValue)

      If name Is Nothing Then
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File Format. Input File Format Does Not Fall Within Domain Of Valid Values." & ObjectToString.GetNewline() & "fileFormat: " & ObjectToString.GetToString(fileFormat))
      End If
    End Sub
  End Class
End Namespace