Solved

Indexed properties on custom class

Posted on 2008-11-03
2
410 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now