Passing an object from F # to a C # method that takes it as an out parameter doesn't work?

One of the libraries I use defines this in C #:

public ushort GetParameterSet(string name, out ParameterSet parameterSet)

I am trying to call this from F #:

let parameterSet = new ParameterSet();
let retVal = currentPanel.GetParameterSet(name, ref parameterSet);

However, despite the fact that the Set parameter is set to an instance of this class with valid data in the C # method, it does not change in F #.

What am I missing here?

+3
source share
2 answers

Try

// let parameterSet = null;
let retval, parameterSet = currentPanel.GetParamterSet(name);

You should not pass an instance as a parameter refwhen the method expects an output parameter (which, when called, should be an unassigned reference preceded by a keyword out).

+4
source

, , : ref F #, #. F #, 'a ref . - contents, :

type 'a ref = { mutable contents : 'a }

. , :

let x = 0
let y = ref x

y, x. y int ref, contents , x ( 0). :

y.contents <- 1

x. contents

F # 'a ref. , :

y.contents <- y.contents + 1

:

y := !y + 1

, := contents, ! .

ref . Reference Cells MSDN.

F # , 'a ref, , byref ( ref out # byref IL). , , contents ref . ref parameterSet ref, , , . , :

let parameterSet = ref(new ParameterSet())
let retVal = currentPanel.GetParameterSet(name, parameterSet)
...
// use parameterSet.contents as needed

let mutable , &, byref:

let mutable parameterSet = new ParameterSet()
let retVal = currentPanel.GetParameterSet(name, &parameterSet)
+11

Source: https://habr.com/ru/post/1715108/


All Articles