Question How to convert an Action into a Func that returns a value

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
I am using the following statements, provided by Varian, to connect to the Velocity Engine and consequently be able to use the Velocity API library
C#:
  Action<bool> orThrow = result => OrThrow(result, engine);
          
            orThrow(engine.loginToGrid(UserName, Password, GRID_IP, GRID_PORT, VelocityDataBase));

My code worked with a previous release of Velocity API.
I have just linked the code with the ".dll" from the new Velocity API release and recompiled my code that now dies precisely on the shown "orThrow" statement.
The input parameters are the same as before as they depend on the user installation.
Clearly, something happens that triggers a not handled exception.
To try to understand the cause I would need to print out the content of the variable "result" but since it is inside an Action, I can't do that by using the debugger.
I wonder whether it is possible to transform the two above-listed statements into a function that returns the "result".
Thank you very much
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,554
Location
Sydney, Australia
Programming Experience
10+
The value of result is just going to be true or false. is that really going to help you that much?

Anyway, if you want to see the value of result, why don't you just change your action?
C#:
Action<bool> orThrow = result =>
{
    Debug.WriteLine(result);
    OrThrow(result, engine);
};
I would think that you'd also be able to put a breakpoint on the original OnThrow call and examine result then, but I'm not 100% sure about that.
 

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
The value of result is just going to be true or false. is that really going to help you that much?

Anyway, if you want to see the value of result, why don't you just change your action?
C#:
Action<bool> orThrow = result =>
{
    Debug.WriteLine(result);
    OrThrow(result, engine);
};
I would think that you'd also be able to put a breakpoint on the original OnThrow call and examine result then, but I'm not 100% sure about that.
I tried your suggestion but the compiler is complaining on "orThrow" usage inside the Action block

C#:
             Action<bool> orThrow = result =>
            {
                Debug.WriteLine(result);
                OrThrow(engine.loginToGrid(UserName, Password, GRID_IP, GRID_PORT, VelocityDataBase));
            };

It complains about the parameters passed to "orThrow"
 
Top Bottom