Solved

Indexed properties on custom class

Posted on 2008-11-03
2
415 Views
Last Modified: 2013-12-17
I have a custom class (ControlPropertiesBase) whith the following fields (see code).
Several other classes (derived from WebControls) have the property ControlProperties, which is derived from the ControlPropertiesBase, plus other properties / fields.

Is it possible to access the properties via indexing like myControl.Properties["TabOrder"], and if so how can I do that?
[Serializable]
public class ControlPropertiesBase
{
	public string Name;
	public string Label;
	public string LabelTranslated;
	public int TabOrder;
	public bool Compulsary;
	public bool ReadOnly;
	public bool Visible;
}
 
public class DropDownListControl : DropDownList
{
	[Serializable]
	public class ControlProperties : ControlPropertiesBase
	{
		public ControlProperties() { }
		public Hashtable Items;
	}
 
	private ControlProperties _properties = new ControlProperties();
	public ControlProperties Properties
	{
		get { return _properties; }
		set { _properties = value; }
	}
 
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		this._properties = new ControlProperties();
	}
}

Open in new window

0
Comment
Question by:Albert Van Halen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 1

Expert Comment

by:Nullable
ID: 22871528
u can use this code :)



this._properties = new ControlProperties();
 
_properties.Label = "Nullable";
 
string labelvalue = _properties.GetType().GetField("Label").GetValue(_properties).ToString();

Open in new window

0
 
LVL 1

Accepted Solution

by:
Nullable earned 500 total points
ID: 22871633
u Can use this code

i test it and it will be work



public class ControlProperties : ControlPropertiesBase
        {
            public ControlProperties() { }
            public Hashtable Items;
            public object this[string index]
            {
                get
                {
                    return this.GetType().GetField(index).GetValue(this).ToString();
                }
                set
                {
                    this.GetType().GetField(index).SetValue(this, value);
                }
            }
        }
 
// Use code :
 
string labelvalue = _properties["Label"] as string;

Open in new window

0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question