cannot pass 'this' as a ref or out parameter because it is read-only


market

My class passes itself to a method in another class. This external method (last line of code below) changes the object I'm passing accordingly, returns control, and moves on in a happy fashion. But the last line ThisPaymentGateway.Process(ref this);says it at design time:

cannot pass 'this' as a ref or out parameter because it is read only***

There is no read-only property there. How can I fix this?

using System;

namespace Something
{
  namespace Finance
  {
    namespace Donations
    {
      public class Electronic1 : Donation
      {
        private PaymentGateway ThisPaymentGateway { get; set; } = new PaymentGateway();

        public void RunController()
        {
          if (DonorType.ToUpper() == "IND" && (PaymentMethod.ToUpper() == "CREDIT CARD" || PaymentMethod.ToUpper() == "ECHECK"))
          {
            ThisPaymentGateway.Process(ref this);
          }
        }
      }
    }
  }
}
Scott Pelham

Remove "ref" from call and handle method signatures

If you just want to "change the object" and then return, you don't need to pass it by ref anyway. Classes are already reference types, so if you change object properties in Process, it will change them in the source object.

Related


Cannot use ref or out parameter in lambda expression

scalp Why can't you use ref or out parameters in lambda expressions? I ran into the error today and found a workaround, but I'm still curious why this is a compile time error. CS1628 : Cannot use ref or out parameter 'parameter' in anonymous method, lambda exp

Cannot use ref or out parameter in lambda expression

scalp Why can't you use ref or out parameters in lambda expressions? I ran into the error today and found a workaround, but I'm still curious why this is a compile time error. CS1628 : Cannot use ref or out parameter 'parameter' in anonymous method, lambda exp

Cannot use ref or out parameter in lambda expression

scalp Why can't you use ref or out parameters in lambda expressions? I ran into the error today and found a workaround, but I'm still curious why this is a compile time error. CS1628 : Cannot use ref or out parameter 'parameter' in anonymous method, lambda exp

Variable cannot be overwritten because it is read-only

Paul Durst I am trying to learn Powershell. I have the following script: $cmd = { param($pid) Write-Host $pid } $processes = Get-Process -Name notepad foreach ($process in $processes) { $pid = $process.ID Start-Job -ScriptBlock $cmd -ArgumentLis

Variable cannot be overwritten because it is read-only

Paul Durst I am trying to learn Powershell. I have the following script: $cmd = { param($pid) Write-Host $pid } $processes = Get-Process -Name notepad foreach ($process in $processes) { $pid = $process.ID Start-Job -ScriptBlock $cmd -ArgumentLis

Variable cannot be overwritten because it is read-only

Paul Durst I am trying to learn Powershell. I have the following script: $cmd = { param($pid) Write-Host $pid } $processes = Get-Process -Name notepad foreach ($process in $processes) { $pid = $process.ID Start-Job -ScriptBlock $cmd -ArgumentLis

Variable cannot be overwritten because it is read-only

Paul Durst I am trying to learn Powershell. I have the following script: $cmd = { param($pid) Write-Host $pid } $processes = Get-Process -Name notepad foreach ($process in $processes) { $pid = $process.ID Start-Job -ScriptBlock $cmd -ArgumentLis

Variable cannot be overwritten because it is read-only

Paul Durst I am trying to learn Powershell. I have the following script: $cmd = { param($pid) Write-Host $pid } $processes = Get-Process -Name notepad foreach ($process in $processes) { $pid = $process.ID Start-Job -ScriptBlock $cmd -ArgumentLis

Pass NULL to ref/out parameter of COM interface method

influence If NULL is defined as COM, how can I pass it to a parameter of a COM interface method [In, Out] ref int pchEaten? For example, consider the following interface: [ComImport, Guid ("000214E6-0000-0000-C000-000000000046")] [InterfaceType (ComInterfaceTy

Pass read-only struct as "input" parameter?

Field According to all known laws of the in parameter modifier , any object passed will be passed by reference BUT, but cannot be modified by the called method. So I find Microsoft's advice on how to write safe, efficient C# code confusing: Declare a read-only