简体   繁体   中英

Access objects in a list, perform a foreach using their properties with F#

I've got a list of objects, to make this question generic lets say I have the object of type 'myType'. myType has two properties x and y, they are both integers. I also have a list of 'myType' objects called 'myList'.

I want to perform something on each object in this list, I'll give an example.

  override form.OnPaint e = 
      let g = e.Graphics in
      for myType in myList do
      g.FillRectangle(Brushes.Black, x, x, x, x) // instead of x I want to use the values from myType's properties.

So in C# I would simply do a

foreach (myType x in myList) 
{
    g.FillRectangle(Brushes.Black, x.x, x.y ...etc)
}

Whereas now in F# the foreach syntax is different and I'm not sure it can access the specific objects properties like the given example in C#.

Any ideas on how to do this will be greatly appreciated, keep in mind I'm new to F# and still have alot to learn so the syntax is still a bit iffy for me.

我想你要

mylist |> Seq.iter (fun x -> g.FillRectangle(Brushes.Black,x.x,x.y,...))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM