سؤال

I am trying to use a 3rd party C# library in F#. The C# author overloaded a field I am trying to set so that the object itself receives the value. With apologies for the shorthand and incomplete code fragment, the C# looks like this:

public class cls1 { public List<cls2> prop1; }
public class cls2 { private double[,] prop2;
                    public object this[int r,int c] 
                      {set {this.prop2[r,c]=value;} }
                  }

To set cls2.prop2, this works in C#:

cls1.prop1[0][0, 0] = 0.0

In F# this fails with the error "Invalid expression on left of assignment":

cls1.prop1[0][0, 0] <- 0.0

Can someone offer a hint as to a path forward? Thanks.

هل كانت مفيدة؟

المحلول

The correct F# syntax is:

   cls1.prop1.[0].[0, 0] <- 0.0

From Arrays page on MSDN:

You can access array elements by using a dot operator (.) and brackets ([ and ]).

نصائح أخرى

You're assigning to an indexed property. There are two ways to refer to an indexed property:

x.[0, 0] //array syntax

and

x.Item(0, 0) //method syntax

The former only works if the property is named Item, which is the case for any indexed property defined in C#. In F#, however, the name can be arbitrary.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top