Here is a bit of code that uses the Keyword Optional.
Private Sub LLayer(ByRef Lname As String, Optional Lcolor As Integer, Optional Ltype As String)
Dim objLayer As AcadLayer
On Error GoTo ErrorHandler
If DoesLayerExist(Lname) = False Then
Set objLayer = ThisDrawing.Layers.Add(Lname)
'SHOW 2 WAYS TO ERROR TRAP THIS
' If Lcolor <> 0 Then
objLayer.color = Lcolor
' End If
' If Ltype <> "" Then
objLayer.Linetype = Ltype
' End If
End If
GoTo Clean_Up
ErrorHandler:
Select Case Err
Case -2145320939:
Err.Clear
MsgBox "Invalid Color call"
Resume Next
Case -2145386493:
Err.Clear
MsgBox "Invalid Linetype call"
Resume Next
Case Else:
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End Select
Clean_Up:
Set objLayer = Nothing
Lcolor = 0
Lname = ""
Ltype = ""
Exit Sub
End Sub
When we CALL this code, we can pass our arguments normally, but now if we do not know one of them or we do not wish to change from the default, we do not have too.
Here is 4 examples of code using the optional argumentsPublic Sub SetupDwgWithLayersAndLinetypes()
LLINE
Call LLayer("Layer1", acRed, "HIDDEN")
Call LLayer("Layer2")
Call LLayer("Layer3", acBlue)
Call LLayer("Layer4", , "DASHED")
End Sub
Each of the arguments must be separated by commas (,).
For Layer1, we are using all 3 arguments. Layer2 is just the name. Layer3 uses Name and Color. and Layer4 is Name and Linetype.
Notice in Layer4, we added a comma (,) to show that color was blank.