Arguments
The code inside a method often needs information passed to it.
You can define this information by adding parameters to the declaration allowing you to pass in arguments.
When a variable is passed to a subroutine or function it is called an argument.
The value used in place of the parameter when we call the method is called the argument.
Passing by Value
The default when you are passing in arguments is by value.
public Method(int Number1)
Passing by Reference
public Method(ref int Number1)
Added in .NET 4.0
Handlying parameters by reference
This is indicated by prefixing the parameter with the "ref" modifier
static void PassByRef(ref myobject cal1)
{
val1.member = 40;
}
The ref keyword on a method parameter causes a method to refer to the same variable that was passed as an input parameter for the same method. If you do any changes to the variable, they will be reflected in the variable.
You can even use ref for more than one method parameters.
namespace TestRefP
{
public class myClass
{
public static void Method_RefTest(ref int iVal1 )
{
iVal1 += 2;
}
public static void Main()
{
int i; // variable need to be initialized
i = 3;
Method_RefTest(ref i );
System.Console.WriteLine(i);
}
}
}
.NET 3.5 and earlier
The Excel PIA can accept parameters passed by value.
The Word PIA however can only accept parameters are passed by reference.
When using VB this is handled automatically.
When using the Word API with C# optional parameters must be passed by reference using the "ref" keyword.
When using the Word API with C# you must also pass a variable containing the actual value.
You cannot pass literal values to Word methods.
The default in VBA is to pass all parameters by reference
Both VB and C# default to passing parameters by value
Most methods in Word expect each of the its methods to be pasted using the ref keyword.
In C# you cannot pass literal values to most Word methods:
object objValue = Word.wdArrangeStyle.wdTiled
Application.Windows.Arrange(ref objValue)
Passing Out
public Method(out int Number1)
Added in ??
Very similar to reference parameters but with two main differences:
1) use the keyword "out"
2) the variable does not have to be assigned before the method is called
What about object types ?
Always ByRef regardless unless you manually clone / serialise the objects first in memory
The out parameter can be used to return the values in the same variable passed as a parameter of the method. Any changes made to the parameter will be reflected in the variable.
public class mathClass
{
public static int Mehod_TestOut(out int iVal1, out int iVal2)
{
iVal1 = 10;
iVal2 = 20;
return 0;
}
public static void Main()
{
int i, j; // variable need not be initialized
System.Console.WriteLine(Method_TestOut(out i, out j));
System.Console.WriteLine(i);
System.Console.WriteLine(j);
}
}
Named Arguments
Added in .NET 4.0
Also referred to as Named Parameters
The named parameters feature does not affect the declaration of the method
You can use the named parameter feature when you call a method but including the names of the paramaters
The order of parameters can be different from the declaration
In combination with optional parameters you can leave out one or more of the optional parameters
This feature was introduced to support legacy APIs (especially Office) and should be used sparingly in new code
public void Folder_Create(string sFolderName = "C:\temp\",
string sSubFolderName = "newfolder")
{
}
public void main()
{
Folder_Create("C\temp\one\", "two");
Folder_Create(sFolderName: "C\temp\one\", "two");
Folder_Create(sFolderName: "C\temp\one\", sSubFolderName: "two");
Folder_Create(sSubFolderName: "two", sFolderName: "C\temp\one\");
Folder_Create(sSubFolderName: "two");
}
This allows you to pass arguments to methods using named arguments which means that the ordering of the arguments is irrelvant
This complements optional parameters
Provide the name of the argument followed by a colon and then the vvalue you want to pass in
The rest of the arguments will contain their default values
public bool Folder_Rename(string sFolderName,
string sNewFolderName)
{
}
public void main()
bool value = Folder_Rename(sFolderName: "C\temp\one\",
sNewFolderName: "C:\temp\two\")
}
Optional / Default
public Method(int Number1 = 10)
Added in .NET 4.0
Also referred to as Optional Parameters
Optional parameters allow you to omit the argument when you want to use the parameters default value.
Optional parameters cannot be marked as "ref" or "out".
In earlier version you had to submit System.Type.Missing
you can make parameters optional by assigning them default values
Optional parameters must always follow mandatory ones
When calling methods with optional parameters you can leave out one or more of the optional parameters
When an optional parameter is not provided the default value is used
This feature was introduced to support legacy APIs (especially Office) and should be used sparingly in new code
A large number of the Microsoft Office methods allow 20 or more optional parameters.
When parameters are not submitted their default values are used instead.
Optional parameters in VBA appear at the end of a declaration and it is the same in C#.
public bool Folder_Exists(string sFolderPath,
bool bInformUser = false)
{
}
static int Sum(int var1 = 1, int var2 = 2)
{
return var1 + var2;
}
Sum(); // 1 + 2 = 3
Sum(4); // 4 + 2 = 6
Sum(4,5); // 4 + 5 = 9
Overloading
Default parameters were only introduced in .NET 4.0
Prior to that you had to use a different approach if you wanted to achieve the same thing.
This alternative was to use method overloading
string MyMethod (int start)
string MyMethod (int start, int finish)
.NET 3.5 and earlier
C# does not support optional parameters because it supports method overloading.
System.Type.Missing
C# does not support optional parameters so you must pass "System.Type.Missing" instead
This is automatically declared for you in the following files:
ThisAddIn.Designer.cs
Sheet1.Designer.cs
Sheet2.Designer.cs
Sheet3.Designer.cs
ThisDocument.Designer.cs
[private global::System.Object missing = global::System.Type.Missing]
Excel
Optional parameters in VBA appear at the end of a declaration.
When parameters are ignored they will accept their default values.
Therfore every parameter defined by a method must be passed to the caller.
Excel.Workbook objWorkbook = Application.Workbooks.Open(@"C;\temp\myfile.xlsx",
System.Reflection.Missing.Value (* 14)
You can only pass the System.Type.Missing value for parameters that accept reference types
For value type paramaters you must pass in the actual default value.
If an optional paramater is an enumeration rather than an object, then you must specify a corresponding enumeration value.
Excel.Range objRange;
objRange.Sort(objRange,
Excel.xlSortOrder.xlAscending,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
Excel.xlSortOrder.xlAscending
System.Reflection.Missing.Value,
Excel.xlSortOrder.xlAscending
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
Excel.xlSortOrientation.xlSortColumns
Excel.xlSortOrientation.xlSortColumns
Excel.xlSortOrientation.xlSortColumns);
Another example where there are lots of optional parameters is Application.WorksheetFunction. In fact these functions require 29 arguments.
Because Optional parameters are not strongly typed any mismatches between the type passed in and the type expected will not be found until runtime.
Wrapper for Populating Optional Paramaters
In some cases though if a method is called a lot it might be useful to create a wrapper method to automatically populate the optional parameters.
First declare the delegate
private delegate double WorksheetFunction_Delegate(
object objValue1,
object objValue2,
..
object objValue30);
The create the wrapper function
private double WorksheetFunction_Call(
WorksheetFunction_Delegate delFunction,
object objValue,
params object[] aParamValues)
{
object[] arguments = new object[29];
aParamValues.CopyTo(arguments, 0);
for (int icount = aParamValues.Length; icount < arguments.Length; icount++)
{
arguments[icount] = System.Reflection.Missing.Value;
}
return delFunction(
objValue,
arguments[0],
arguments[1],
arguments[28],
}
Once the delegate and wrapper function have been created calling a built-in worksheet function is relatively straightforward.
This calls a worksheet function that accepts 1 argument.
WorksheetFunction_Delegate fn_MIN = new WorksheetFunction_Delegate(Application.WorksheetFunction.Min);
System.Windows.Forms.MessageBox.Show(WorksheetFunction_Call(
fn_MIN, objRange, new object[] {System.Reflection.Missing.Value }).ToString();
This calls a worksheet function that accepts 3 arguments.
System.Windows.Forms.MessageBox.Show(WorksheetFunction_Call(
fn_333, objRange1, new object[] {objRange2, objRange3 }).ToString();
Word
Optional parameters in Word are treated the same as optional parameters in Excel.
It is slightly more complicated in Word though because you have to pass most of the parameters by reference.
For more information on passing them by reference, refer to Classes > Arguments > ref
You must declare a variable, set it to System.Type.Missing and then pass that variable by reference
If the missing variable has been predefined you can just use that
object missing = System.Type.Missing;
Normally when you pass variables by reference its value can be changed
Passing System.Type.Missing however as a paramater actually sends a special COM value indicating the value is actually missing.
This special case only applies when you are using COM Interop to an unmanaged object model
string sfilename = "C:\temp\myfile.docx"
Application.Documents.Open(ref sfilename,
ref System.Reflection.Missing.Value (*15)
Params Arguments
You can pass the params keyword to pass a variable list of parameters.
This allows you to pass a variable list of paramaters
Declare your last parameter in your list as an array type
static void VarArgs(int val1, params int[] vals)
{
foreach (int I in vals)
{
}
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext