tawright915
tawright915

Reputation: 23

How is this defined?

Is this an array of class property or a property class array? And how do I access each row instead of each element in the class in C#? Thanks!

public partial class AdvancedSearchResult {

    private SearchResults[] searchResultsField;

    /// <remarks/>
    **[System.Xml.Serialization.XmlElementAttribute("SearchResults")]
    public SearchResults[] SearchResults { <-------NOT SURE HOW TO DEFINE THIS
        get {
            return this.searchResultsField;
        }
        set {
            this.searchResultsField = value;
        }
    }**


public partial class SearchResults {

    private int dIDField;

    private bool dIDFieldSpecified;

    private int dRevisionIDField;

    ...............etc

Upvotes: 2

Views: 102

Answers (3)

ispiro
ispiro

Reputation: 27633

If I understand you correctly:

  1. Don't call your property SearchResults because that's the class's name. Call it searchResults .
  2. Access a 'row' like this: searchResults[rowNumber].

searchResultsField is an array, of which every member is a SearchResults class instance. And so is (de facto) your property. So it's an array of class instances.

To access a field in your property you would use searchResults[rowNumber].fieldName (assuming they were public fields).

Due to your comment - Here's a simple example of reflection:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        MyClass myClass = new MyClass() { i = "a", j = "b" };
        FieldInfo[] infos = typeof(MyClass).GetFields();
        foreach (FieldInfo info in infos)
            Text += info.GetValue(myClass);
    }
}


class MyClass
{
    public string i;
    public string j;
}

And add at the top:

using System.Reflection;

Upvotes: 0

Atomic Star
Atomic Star

Reputation: 5507

SearchResults is a property of class AdvancedSearchResult. It gets/sets the private array of SearchResults items called searchResultsField.

Access it as follows:

var result=new AdvancedSearchResult();

foreach(var item in result.SearchResults)
{  var myId= item.dIDField; 
   var isSpecified=item.dIDFieldSpecified;
   ...
}

Upvotes: 0

Martin Mulder
Martin Mulder

Reputation: 12944

First, I would write your class a bit shorter:

public partial class AdvancedSearchResult 
{
    [System.Xml.Serialization.XmlElementAttribute("SearchResults")]
    public SearchResults[] SearchResults 
    {
        get 
        set 
    }
}

Second you can assign values:

AdvancedSearchResult results = new AdvancedSearchResult();
results.SearchResults = new[]
{
    new SearchResults { ... },
    new SearchResults { ... },
    etc.
}

To retrieve the values, you can use the index:

SearchResults firstResult = results.SearchResults[0];

Or use an enumerator:

foreach(SearchResults sr in results.SearchResults)
{
    // Something with sr.
}

Upvotes: 2

Related Questions