Re: Data Object Collection Inheritance
From: Steve Willcock (steve_at_N-O-S-P-A-Mwillcockconsulting.com)
Date: 08/10/04
- Next message: Aaron: "Custom Event Handler"
- Previous message: Fraser Michael: "Re: Help with locking system"
- In reply to: George Prado: "Data Object Collection Inheritance"
- Next in thread: George Prado: "Re: Data Object Collection Inheritance"
- Reply: George Prado: "Re: Data Object Collection Inheritance"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 10 Aug 2004 23:13:51 +0100
George, you can fix this by setting the Add method in the base collection
class to be virtual and then overriding it in the SubClass - i.e.
public override int Add(object value)
{
SubClassData[] arrCollection = new SubClassData[0];
arrCollection = (SubClassData[])this.DataCollection.Clone();
this.DataCollection = new SubClassData[arrCollection.Length + 1];
arrCollection.CopyTo(this.DataCollection,0);
this.DataCollection[this.DataCollection.Length - 1] = (SubClassData)value;
return 1;
}
On another note, you might want to have a look at the CollectionBase class
which helps to simplify a lot of the code you're writing here -
http://www.csharphelp.com/archives/archive256.html this is an example of
it's usage.
-- Steve Will*** (MCSD for Microsoft.NET) http://www.willcockconsulting.com/ "George Prado" <George Prado@discussions.microsoft.com> wrote in message news:7DF4DC7C-16DD-4542-9963-867571D1B963@microsoft.com... > I am new to the collections objects in C# and have a question to ask of those > who know how collections work in list boxes. I have put together a sample app > to show what is going on. My issue is I have a collection of data objects > and I cannot link inherited properties to my list control. If the property > lies in the base data object I can get it but not if it is in the inherited > object. All those properties are invisible to the list control. Load my code > below for an example of what I am talking about. Any help would be greatly > appreciated! I am also open to suggestion if any one knows of a better way > to create business object. > > George Prado > > Form1.cs: > > using System; > using System.Drawing; > using System.Collections; > using System.ComponentModel; > using System.Windows.Forms; > using System.Data; > > namespace CollectionSample > { > /// <summary> > /// Summary description for Form1. > /// </summary> > public class Form1 : System.Windows.Forms.Form > { > private System.Windows.Forms.ListBox listBox1; > private System.Windows.Forms.Button cmdID; > private System.Windows.Forms.Button cmdText; > /// <summary> > /// Required designer variable. > /// </summary> > private System.ComponentModel.Container components = null; > > public Form1() > { > // > // Required for Windows Form Designer support > // > InitializeComponent(); > > // > // TODO: Add any constructor code after InitializeComponent call > // > } > > /// <summary> > /// Clean up any resources being used. > /// </summary> > protected override void Dispose( bool disposing ) > { > if( disposing ) > { > if (components != null) > { > components.Dispose(); > } > } > base.Dispose( disposing ); > } > > #region Windows Form Designer generated code > /// <summary> > /// Required method for Designer support - do not modify > /// the contents of this method with the code editor. > /// </summary> > private void InitializeComponent() > { > this.listBox1 = new System.Windows.Forms.ListBox(); > this.cmdID = new System.Windows.Forms.Button(); > this.cmdText = new System.Windows.Forms.Button(); > this.SuspendLayout(); > // > // listBox1 > // > this.listBox1.Location = new System.Drawing.Point(40, 16); > this.listBox1.Name = "listBox1"; > this.listBox1.Size = new System.Drawing.Size(240, 173); > this.listBox1.TabIndex = 0; > // > // cmdID > // > this.cmdID.Location = new System.Drawing.Point(24, 216); > this.cmdID.Name = "cmdID"; > this.cmdID.Size = new System.Drawing.Size(112, 23); > this.cmdID.TabIndex = 1; > this.cmdID.Text = "Link to ID"; > this.cmdID.Click += new System.EventHandler(this.cmdID_Click); > // > // cmdText > // > this.cmdText.Location = new System.Drawing.Point(168, 216); > this.cmdText.Name = "cmdText"; > this.cmdText.Size = new System.Drawing.Size(128, 23); > this.cmdText.TabIndex = 2; > this.cmdText.Text = "Link to AnotherColumn"; > this.cmdText.Click += new System.EventHandler(this.cmdText_Click); > // > // Form1 > // > this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); > this.ClientSize = new System.Drawing.Size(328, 266); > this.Controls.Add(this.cmdText); > this.Controls.Add(this.cmdID); > this.Controls.Add(this.listBox1); > this.Name = "Form1"; > this.Text = "Form1"; > this.Load += new System.EventHandler(this.Form1_Load); > this.ResumeLayout(false); > > } > #endregion > > /// <summary> > /// The main entry point for the application. > /// </summary> > [STAThread] > static void Main() > { > Application.Run(new Form1()); > } > > private void Form1_Load(object sender, System.EventArgs e) > { > objNewCollection = new SubClassCollection(); > this.listBox1.DataSource = objNewCollection.Collection; > } > > private CollectionSample.SubClassCollection objNewCollection; > > private void cmdID_Click(object sender, System.EventArgs e) > { > this.listBox1.DisplayMember = "ID"; > } > > private void cmdText_Click(object sender, System.EventArgs e) > { > this.listBox1.DisplayMember = "AnotherColumn"; > } > } > > public abstract class BaseData > { > protected System.Data.DataRow drCurrent; > > public BaseData() > { > > } > > public int ID > { > get > { > //return (int)drCurrent["id"]; > return 1; > } > } > /*All base functionality for data objects > * > * > * > */ > } > > public sealed class SubClassData : CollectionSample.BaseData > { > public SubClassData() > { > //load data row with data > } > > public string AnotherColumn > { > get > { > //return (string)this.drCurrent["AnotherColumn"]; > return "I Don't Get This Text In The List"; > } > } > /*All specific functionality for data objects > * > * > * > */ > } > > public abstract class BaseDataCollection : System.Collections.IEnumerable, > System.Collections.IList > { > protected BaseData[] DataCollection; > > public BaseDataCollection() > { > > } > > public BaseData[] Collection > { > get > { > return this.DataCollection; > } > } > > #region IEnumerable Members > > public System.Collections.IEnumerator GetEnumerator() > { > return new DataEnumerator(this); > } > > #endregion > > #region IList Members > > public bool IsReadOnly > { > get > { > // TODO: Add BaseDataCollection.IsReadOnly getter implementation > return false; > } > } > > public object this[int index] > { > get > { > return this.DataCollection[index]; > } > set > { > // TODO: Add BaseDataCollection.this setter implementation > } > } > > public void RemoveAt(int index) > { > // TODO: Add BaseDataCollection.RemoveAt implementation > } > > public void Insert(int index, object value) > { > // TODO: Add BaseDataCollection.Insert implementation > } > > public void Remove(object value) > { > // TODO: Add BaseDataCollection.Remove implementation > } > > public bool Contains(object value) > { > // TODO: Add BaseDataCollection.Contains implementation > return false; > } > > public void Clear() > { > // TODO: Add BaseDataCollection.Clear implementation > } > > public int IndexOf(object value) > { > // TODO: Add BaseDataCollection.IndexOf implementation > return 0; > } > > public int Add(object value) > { > BaseData[] arrCollection = new BaseData[0]; > arrCollection = (BaseData[])this.DataCollection.Clone(); > this.DataCollection = new BaseData[arrCollection.Length + 1]; > arrCollection.CopyTo(this.DataCollection,0); > this.DataCollection[this.DataCollection.Length - 1] = (BaseData)value; > return 1; > } > > public bool IsFixedSize > { > get > { > // TODO: Add BaseDataCollection.IsFixedSize getter implementation > return false; > } > } > > #endregion > > #region ICollection Members > > public bool IsSynchronized > { > get > { > // TODO: Add BaseDataCollection.IsSynchronized getter implementation > return false; > } > } > > public int Count > { > get > { > return this.DataCollection.Length; > } > } > > public void CopyTo(Array array, int index) > { > // TODO: Add BaseDataCollection.CopyTo implementation > } > > public object SyncRoot > { > get > { > // TODO: Add BaseDataCollection.SyncRoot getter implementation > return null; > } > } > > #endregion > > #region Data Enumerator > > private class DataEnumerator : System.Collections.IEnumerator > { > private int position = -1; > private BaseDataCollection Items; > > public DataEnumerator(BaseDataCollection Collection) > { > this.Items = Collection; > this.position = -1; > } > > #region IEnumerator Members > > public void Reset() > { > this.position = -1; > } > > public object Current > { > get > { > return this.Items[this.position]; > } > } > > public bool MoveNext() > { > bool boolReturns = false; > if(!this.position.Equals(this.Items.Count)) > { > this.position++; > boolReturns = true; > } > return boolReturns; > } > > #endregion > } > > #endregion > } > > public sealed class SubClassCollection : BaseDataCollection > { > public SubClassCollection() > { > this.DataCollection = new SubClassData[0]; > this.LoadCollection(); > } > > private void LoadCollection() > { > //Get the data objects you want in collection > this.Add(new SubClassData()); > this.Add(new SubClassData()); > this.Add(new SubClassData()); > this.Add(new SubClassData()); > this.Add(new SubClassData()); > //add as many business objects as needed in collection. > } > } > } > > >
- Next message: Aaron: "Custom Event Handler"
- Previous message: Fraser Michael: "Re: Help with locking system"
- In reply to: George Prado: "Data Object Collection Inheritance"
- Next in thread: George Prado: "Re: Data Object Collection Inheritance"
- Reply: George Prado: "Re: Data Object Collection Inheritance"
- Messages sorted by: [ date ] [ thread ]