1

i'm new to C#,

I'm trying to use a function that as been created here

Actually i don't know how to return a list as array as a parameter. This function right here will use the "write" method of Odoo API.

    public void SetFieldValue(string field, object value)
    {
        var fieldAttribute = _fieldsResult.FirstOrDefault(f => f.FieldName == field);
        if (fieldAttribute == null) return;

        fieldAttribute.Changed = fieldAttribute.Changed == false;

        fieldAttribute.Value = value;
    }

So i did like this but it don't works :

  foreach (var result in results)
  {
       result.SetFieldValue("payment_method_ids", _NewPaymentLine.ToArray());
       result.Save();
  }

_NewPaymentLine is a List that i filled list this :

var _NewPaymentLine = new List<object>();
_NewPaymentLine.Add(new object[] { 4, Payment_Ids.GetField("id").Value });

All i can say its that the input value is a System.Object[] type and the output value is a System.Int32[] type.

Actually, what i return with the SetFieldValue is a System.Object[], but i think i have to return an System.Int32[], so the question is, how can i do to make my List of System.Object[] into System.Int32[]

Thanks in advance, i hope that someone can help me.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Gabin
  • 61
  • 8
  • 2
    What is the question? I don't really get it. How to return an array from a method in C#? – Dominik Jul 21 '21 at 08:22
  • Actually, what i return with the SetFieldValue is a System.Object[], but i think i have to return an System.Int32[], so the question is, how can i do to make my List of System.Object[] into System.Int32[] – Gabin Jul 21 '21 at 08:25
  • You will have to create a new array. You cannot "cast" the existing array. Do something like `int[] resultArray = Array.ConvertAll(inputArray, x => (int)x);` where `inputArray` is your object array - or if you need to convert a `string` (or whatever else your object might be), use something like `Convert.ToInt32(x)` instead of `(int)x`. – Dominik Jul 21 '21 at 08:30
  • So, i tried to do it but i cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List[]' because my inputArray is a list – Gabin Jul 21 '21 at 09:03
  • Your error says that you have a `List` however an array of `List` (`List[]`) is expected. This sounds weird... maybe a mistake on your side? – Dominik Jul 21 '21 at 09:05

1 Answers1

2

Not sure if this is what you pretend, but you have to create a function to convert from your Object to Int

public List<int> DoConvert(List<Object> objectsList){
    List<int> intList = new List<int>();
    foreach(var object in objectsList)
    {
        intList.Add(object.MyNumber)       //Input your logic here
    }
    return intList;
}
Dominik
  • 1,623
  • 11
  • 27
Henrique Pombo
  • 537
  • 1
  • 6
  • 20
  • 1
    `object` will for sure not have `.MyNumber`. This might be a bit missleading to a beginner. I suggest to have some kind of "sample convert" of `object`. Also you're missing a function name. – Dominik Jul 21 '21 at 08:42
  • @Dominik, `RpcField` doesn't appear defined on the link he provided, I assumed it would have other properties, and he only wanted to save some of them, hence my "Input your logic here". Also while creating a more abstract function with List return could not achieve what he pretended, since I assumed he would need to access specific properties. – Henrique Pombo Jul 21 '21 at 08:52
  • Hello thanks for your answer, it was a problem similary to it – Gabin Jul 22 '21 at 12:35