Question System.Array' does not contain definition for 'FirstOrDefault

Eric_03

Member
Joined
Jul 25, 2022
Messages
14
Location
France
Programming Experience
Beginner
Hi,

My code
C#:
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
 
 
 
namespace ExcelWorkbook1
{
    partial class ActionsPaneControl1 : UserControl
    {
        public ActionsPaneControl1()
        {
            InitializeComponent();
        }
 
        /*private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Opération terminée");
        }*/
 
        private void ActionsPaneControl1_Load(object sender, EventArgs e)
        {
            tabControl1.Appearance = TabAppearance.FlatButtons;
            tabControl1.ItemSize = new System.Drawing.Size(0, 1);
            tabControl1.SizeMode = TabSizeMode.Fixed;
 
            SetDefault();
        }
        private bool SetDefault()
        {
            Microsoft.Office.Interop.Excel.Worksheet sh = Globals.ThisWorkbook.Application.Worksheets["Feuil3"];
            Microsoft.Office.Interop.Excel.Range listRng = sh.Range["C15:C22"];
            //Microsoft.Office.Interop.Excel.Range rng;
            Control myCtrl;
 
            foreach (Microsoft.Office.Interop.Excel.Range rng in listRng )
            {
                myCtrl = this.Controls.Find(rng.Value, true).FirstOrDefault();    <= error ?
                myCtrl.Text = rng.Value.Offset(0, 1);
                myCtrl.ForeColor = System.Drawing.Color.Gray;
            }
 
            return true;
        }
System.Array' does not contain definition for 'FirstOrDefault

Thanks

Eric
 
Last edited by a moderator:
Solution
(y) "KO" is not an English expression for "not OK

Solution:

C#:
        private bool SetDefault()
        {
            Microsoft.Office.Interop.Excel.Worksheet sh = Globals.ThisWorkbook.Application.Worksheets["Feuil3"];
            Range listRng = sh.Range["C15:C22"];
            foreach (Range rng in listRng )
            {
                string myKey = rng.Value;
                Control myCtrl = Controls.Find(myKey, true).FirstOrDefault();
                string myValeur = rng.get_Offset(0, 1).Value2;
                myCtrl.Text = myValeur;
                myCtrl.ForeColor = System.Drawing.Color.Gray;
            }

            return true;
        }

thank you very much,

Eric
That's correct and expected. System.Array does not expose IEnumerable<T>. Since FirstOrDefault() is a LINQ extension method for IEnumerable<T>, the error is spot on.
 
Isn't that a forms UserControl? Controls.Find returns a Control array, type Control[]
 
Is that a compilation error or a run-time exception? Presumably the former. If you break this:
C#:
myCtrl = this.Controls.Find(rng.Value, true).FirstOrDefault();
into two lines:
C#:
var temp = this.Controls.Find(rng.Value, true);

myCtrl = temp.FirstOrDefault();
what is the type of temp? You do have System.Linq imported so I would expect that Enumerable.FirstOrDefault<T> would be accessible. What type of project is this and what framework is it targeting?
 
thank you very much,

C#:
private bool SetDefault()
        {
            Microsoft.Office.Interop.Excel.Worksheet sh = Globals.ThisWorkbook.Application.Worksheets["Feuil3"];
            Range listRng = sh.Range["C15:C22"];
            foreach (Range rng in listRng )
            {
                string myKey = rng.Value;
                Control myCtrl = Controls.Find(myKey, true).FirstOrDefault();  <= is OK
                myCtrl.Text = rng.Value.offset(0,1);                           <= is KO offset(0,1)
                myCtrl.ForeColor = System.Drawing.Color.Gray;
            }

            return true;

C ------------------------------D

Plage.png


Panel.png

Frame.png


Eric
 
Last edited:
no is resolved.

the following line of code:

C#:
myCtrl.Text = rng.Value.offset(0,1);   <= is KO

Excuse me, my english is very poor.

Eric
 
But what has that got to do with the subject of this thread? You asked about FirstOrDefault and you seem to be saying that that is now OK. If so then the issue you asked about is indeed resolved. If you have a different issue then you should create a new thread, with a title that summarises the new issue and a post that contains ALL and ONLY the information relevant to the new issue.

By the way, "KO" is not an English expression for "not OK". When I saw that, I thought you meant OK but had just made a mistake when typing.
 
(y) "KO" is not an English expression for "not OK

Solution:

C#:
        private bool SetDefault()
        {
            Microsoft.Office.Interop.Excel.Worksheet sh = Globals.ThisWorkbook.Application.Worksheets["Feuil3"];
            Range listRng = sh.Range["C15:C22"];
            foreach (Range rng in listRng )
            {
                string myKey = rng.Value;
                Control myCtrl = Controls.Find(myKey, true).FirstOrDefault();
                string myValeur = rng.get_Offset(0, 1).Value2;
                myCtrl.Text = myValeur;
                myCtrl.ForeColor = System.Drawing.Color.Gray;
            }

            return true;
        }

thank you very much,

Eric
 
Solution
Back
Top Bottom