Writing iron python method with ref or out parameter
Asked Answered
A

2

8

i need to translate following C# method to the same IronPhyton method

private void GetTP(string name, out string ter, out int prov)
{
  ter = 2;
  prov = 1;
}
Aeolic answered 18/5, 2010 at 12:41 Comment(0)
M
6

In python (and consequently in IronPython) you cannot change a parameter that is not mutable (like strings)

So you can't directly traslate the given code to python, but you must do something like:

def GetTP(name):
  return tuple([2, 1])

and when you call it you must do:

retTuple = GetTP(name)
ter = retTuple[0]
prov = retTuple[1]

that is the same behaviour when in IronPython you call a C# method containing out/ref parameters.

In fact, in that case IronPython returns a tuple of out/ref parameters, and if there's a return value is the first in the tuple.

EDIT: actually it's possible to override a method with out/ref parameters, look here:

http://ironpython.net/documentation/dotnet/dotnet.html#methods-with-ref-or-out-parameters

Mainsail answered 18/5, 2010 at 13:8 Comment(1)
i wanted to explain problem simple as possible, that is why i used that example method. Real problem is that this method overrides method in base class and i must provide same signature in IronPhyton to override base method. I tried your way but i doesnt help...Aeolic
D
2

Python script something like this should work:

ter = clr.Reference[System.String]()
prov = clr.Reference[System.Int32]()

GetTP('theName', ter, prov)

print(ter.Value)
print(prov.Value)
Dysphemism answered 20/7, 2016 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.