Josh Davis
Josh Davis

Reputation: 133

Use foreach on a class not a collection of a class

So I have a this class with strings, floats, DateTimes, and data tables

public class Data : IEnumerator
{

    string m_PowerSwitch = "Not Tested",
        m_SerialNumber = "Not Tested",
        m_Reset = "Not Tested",
        m_WashPump = "Not Tested",

        m_PortB = "Not Tested",
        m_PortC = "Not Tested",
        m_PortD = "Not Tested",
        m_CurlyTube = "Not Tested",
        m_BypassTube = "Not Tested";

    float m_EC53115VMeasured = 0.0F,
        m_EC53165VMeasured = 0.0F,
        m_EC531624VMeasured = 0.0F,

        m_SolventLineBVolumeMeasured = 0.0F,
        m_SolventLineCVolumeMeasured = 0.0F,
        m_SolventLineDVolumeMeasured = 0.0F,
        m_CurlyTubeVolumeMeasured = 0.0F,
        m_BypassTubeVolumeMeasured = 0.0F;
} 

I want to use a foreach statement such as

        foreach (ASM001.ASM asm in P00001.Global.ListofAvailableASMs)
        {  
            if (asm.ASMData.EndTime == null)
                asm.ASMData.EndTime = endTime;

            foreach (object data in asm.ASMData)
            {
                if (data == "Not Tested")
                {
                    asm.ASMData.Result = "FAILED";
                }
                continue;
            }

but I have not been able to find any help of searching through the individual fields of a class, just on a list of the class type.

I am getting the error foreach statement cannot operate on variables of type 'ASM001.Data' because 'ASM001.Data' does not contain a public definition for 'GetEnumerator'

I was wondering if this was possible or if I was going to have to hard code checking each string field by name and returning true or false.

And just so you now there are a lot more strings than what I copied I would have to check, which is why I was wondering if there was a quicker way to do it.

Upvotes: 1

Views: 131

Answers (3)

James Wilkins
James Wilkins

Reputation: 7357

Possible LINQ version:

Data data = ...
FieldInfo[] fields = (from f in data.GetType().GetFields(BindingFlags.Instance|BindingFlags.NonPublic) where f.Name.StartsWith("m_") select f).ToArray();

Upvotes: 1

Pseudonym
Pseudonym

Reputation: 2072

From MSDN (the following example should build and run):

The following example retrieves the fields of MyClass and displays the field values.

using System;
using System.Reflection;

public class MyClass
{
    public string myFieldA;
    public string myFieldB; 
    public MyClass()
    {
        myFieldA = "A public field";
        myFieldB = "Another public field";
    }
}

public class FieldInfo_GetValue
{
    public static void Main()
    {
        MyClass myInstance = new MyClass();
        // Get the type of MyClass.
        Type myType = typeof(MyClass);
        try
        {
            // Get the FieldInfo of MyClass.
            FieldInfo[] myFields = myType.GetFields(BindingFlags.Public 
                | BindingFlags.Instance);
            // Display the values of the fields.
            Console.WriteLine("\nDisplaying the values of the fields of {0}.\n",
                myType);
            for(int i = 0; i < myFields.Length; i++)
            {
                Console.WriteLine("The value of {0} is: {1}",
                    myFields[i].Name, myFields[i].GetValue(myInstance));
            }
        }  
        catch(Exception e)
        {
            Console.WriteLine("Exception : {0}", e.Message);
        }
    }
}

Upvotes: 1

Dai
Dai

Reputation: 155055

Use Reflection (code paraphrased, this will not build)

Data data = ...
Type type = data.GetType();
FieldInfo[] fields = type.GetFields(...);
foreach(FieldInfo field in fields) {

    Console.WriteLine("{0} = {1}", field.Name, field.GetValue( data ) );
}

Upvotes: 3

Related Questions