Optional parameters must specify a default value.

429
3
09-20-2011 01:57 PM
AndrewHayden1
Occasional Contributor
I'm modifying the createDBF developer sample function with VB 2008 Express (Create a new DBASE file).  I think I've got everything worked out except for dealing with an "Optional parameters must specify a default value" error that comes up, in regard to the Optional pFields parameter.  Can anyone get me on track with what the default might look like?
Optional pFields As IFields = ???
 


Public Function createDBF(strName As String, strFolder As String, Optional pFields As IFields) As ITable
        ' createDBF: simple function to create a DBASE file.
        ' note: the name of the DBASE file should not contain the .dbf extension
        '
        On Error GoTo EH

        ' Open the Workspace
        Dim pFWS As IFeatureWorkspace
        Dim pWorkspaceFactory As IWorkspaceFactory
        Dim fs As Object
        Dim pFieldsEdit As IFieldsEdit
        Dim pFieldEdit As IFieldEdit
        Dim pField As IField

        pWorkspaceFactory = New ShapefileWorkspaceFactory
        fs = CreateObject("Scripting.FileSystemObject")
        If Not fs.FolderExists(strFolder) Then
            MsgBox("Folder does not exist: " & vbCr & strFolder)
            Exit Function
        End If

        pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0)

        ' if a fields collection is not passed in then create one
        If pFields Is Nothing Then
            ' create the fields used by our object
            pFields = New Fields
            pFieldsEdit = pFields
            pFieldsEdit.FieldCount_2 = 1

            'Create text Field
            pField = New Field
            pFieldEdit = pField
            With pFieldEdit
                .Length_2 = 30
                .Name_2 = "TextField"
                .Type_2 = esriFieldType.esriFieldTypeString
            End With
            pFieldsEdit.Field_2(0) = pField
        End If

        createDBF = pFWS.CreateTable(strName, pFields, Nothing, Nothing, "")

        Exit Function
EH:
        MsgBox(Err.Description, vbInformation, "createDBF")

    End Function
0 Kudos
3 Replies
DubravkoAntonic
New Contributor III
This attribute is read only.
Comment these two lines and try again.
pFieldsEdit.FieldCount_2 = 1
0 Kudos
AndrewHayden1
Occasional Contributor
That property is actually WriteOnly. 

I ended up just making the optional parameter required and doing a call something like this:

createDBF("Test", "c:\", Nothing)


I'm not sure if there's a better way to do it, or a better way to add additional fields, but it works.  Any suggestions to improve it?

    Public Function createDBF(ByVal strName As String, ByVal strFolder As String, ByVal pFields As IFields) As ITable
        ' createDBF: simple function to create a DBASE file.
        ' note: the name of the DBASE file should not contain the .dbf extension
        '
        On Error GoTo EH

        ' Open the Workspace
        Dim pFWS As IFeatureWorkspace
        Dim pWorkspaceFactory As IWorkspaceFactory
        Dim fs As Object
        Dim pFieldsEdit As IFieldsEdit
        Dim pFieldEdit As IFieldEdit

        pWorkspaceFactory = New ShapefileWorkspaceFactory
        fs = CreateObject("Scripting.FileSystemObject")
        If Not fs.FolderExists(strFolder) Then
            MsgBox("Folder does not exist: " & vbCr & strFolder)
            Exit Function
        End If

        pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0)

        ' if a fields collection is not passed in then create one
        If pFields Is Nothing Then
            ' create the fields used by our object
            pFields = New Fields
            pFieldsEdit = pFields
            pFieldsEdit.FieldCount_2 = 2 'Add 2 fields

            'Create First Field
            Dim pField0 As IField
            pField0 = New Field
            pFieldEdit = pField0
            With pFieldEdit
                .Length_2 = 30
                .Name_2 = "TextField0"
                .Type_2 = esriFieldType.esriFieldTypeString
            End With
            pFieldsEdit.Field_2(0) = pField0

            'Create Second Field
            Dim pField1 As IField
            pField1 = New Field
            pFieldEdit = pField1
            With pFieldEdit
                .Length_2 = 30
                .Name_2 = "TextField1"
                .Type_2 = esriFieldType.esriFieldTypeString
            End With
            pFieldsEdit.Field_2(1) = pField1

        End If

        createDBF = pFWS.CreateTable(strName, pFields, Nothing, Nothing, "")

        Exit Function
EH:
        MsgBox(Err.Description, vbInformation, "createDBF")

    End Function
0 Kudos
AlexanderGray
Occasional Contributor III
Something like this should work:
Public Function createDBF(strName As String, strFolder As String, Optional pFields As IFields = Nothing) As ITable


But Optional parameters are considered bad form, overloading the function is cleaner:

Public Function createDBF(strName As String, strFolder As String, pFields As IFields) As ITable
  'Do stuff

End Function

Public Function createDBF(strName As String, strFolder As String) As ITable
  Dim defaultFields as IFields = Nothing 'Can change this to a real set of fields if necessary
  return createDBF(strName, strFolder, defaultFields)  'Call the function with the default value

End Function
0 Kudos