ref Arguments

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 );

.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)

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext