Consider the following snippet:
Public Sub LayerTestExtraTyping()
Dim Ylayer As AcadLayer
Dim Wlayer As AcadLayer
Set Ylayer = ThisDrawing.Layers.Add("Layer1")
Ylayer.color = acYellow
Set Wlayer = ThisDrawing.Layers.Add("Layer2")
Wlayer.color = acGreen
End Sub
If you look close, you see that we typed the exact same thing twice for the creation of 2 layers. We declared 2 objects, we set both objects, and we set the color of the objects. For just 2 layers, you can see the duplicate effort already. What we need is a function/sub that would take a list of arguments (Name, Color, & Linetype) and process the list so we do not have to retype the exact same code over and over. Looking at this snippet, you will see variable names that Im passing in from another sub.
Public Sub LLayer(ByRef Lname As String, Lcolor As Integer, Ltype As String)
Dim objLayer As AcadLayer
Set objLayer = ThisDrawing.Layers.Add(Lname)
objLayer.color = Lcolor
objLayer.Linetype = Ltype
Set objLayer = Nothing
Exit Sub
End Sub
OK, so how does this work? Well, we have to
CALL the sub and pass the arguments to it. From the help file...
Syntax [Call] name [argumentlist]
You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.
^This will be very important to us in a few minutes.^
ArgumentList - Optional. Comma-delimited list of variables, arrays, or expressions to pass to the procedure. Components of argumentlist may include the keywords ByVal or ByRef to describe how the arguments are treated by the called procedure. However, ByVal and ByRef can be used with Call only when calling a DLL procedure.
ByVal Optional. Indicates that the argument is passed by value.
ByRef Optional. Indicates that the argument is passed by reference. ByRef is the default in Visual Basic.
by value
A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.
by reference
A way of passing the address of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed. Unless otherwise specified, arguments are passed by reference.
So, we need another sub to call the layer creation sub.
Public Sub test()
Call LLayer("Layer1", acWhite, "HIDDEN")
Call LLayer("Layer2", acGreen, "DASHED")
End Sub