Why can't I overload this constructor .. or can I?

H

Henry

I was trying to derive a class from System.Windows.Forms.ComboBox. My goal was to create a class that loaded its own data. I did not want to create too many objects, so I tried to share a Database object from the calling class, but the compliler doesn't like it when I try to provide and overrided contructor. See the code below:

Do I have to stick with the standard ComboBox constructor?
If so, how else could I share a common reference to my data source?

My goal is to encapsulate the data with the object and get the data manipulation code out of the Windows form in which the object is used. I'd like to do this for all my controls as much as possible. I am concerned about duplicating things like data connections and so I was trying to figure out a way to use a common object for that purpose.

using System;
using System.Windows.Forms;

using Microsoft.Practices.EnterpriseLibrary.Data;



namespace com.onproject.controls

{

/// <summary>

/// Summary description for ComboBox.

/// </summary>

public class ComboBox : System.Windows.Forms.ComboBox

{



public ComboBox(Database db)

{

using (IDataReader dr = db.ExecuteReader

(CommandType.Text, "Select id, name,state,root_org_id," +

"original_id from dbo.period"))

{

while (dr.Read())

{

this.Items.Add(new Win_Client.Period(dr.GetInt32(0), dr.GetString(1), dr.GetInt32(2),

dr.GetInt32(3), dr.GetInt32(4)));



}

}





}



}

}



I want to use the period.id field as the index for the ComboBox.Items collection, so that I can pass that value to other controls.



I am still not quite sure how to do that.



I made the combobox display the period.name field by overriding the ToString method of the Period class.



//sample of the Period class that I am using to populate

// the combobox.

using System;



namespace Win_Client

{

/// <summary>

/// Summary description for Period.

/// </summary>

public class Period

{

//private fields

private int id;

private string name;

private int state;

private int root_org_id;

private int original_id;



//public properties

public int Id{get{return id;} set{id= value;}}

public int State{get{return state;} set{state= value;}}

public int Root_Org_Id{get{return root_org_id;} set{root_org_id= value;}}

public int Original_Id{get{return original_id;} set{original_id= value;}}



// constructor populates the fields

public Period(int _id, string _name, int _state,

int _root_org_id,int _original_id)

{

this.id = _id;

this.name = _name;

this.state = _state;

this.root_org_id = _root_org_id;

this.original_id = _original_id;



}

public override string ToString()

{

return name;

}



}

}
 
L

Lars Behrmann

Hi Henry

i don't see where the problem is. It should be
no problem to derive from the ComboBox as you
do it and of course so you will have your own
constructor.
What error message do you get ?

Cheers
Lars Behrmann

_________________
Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/
 
H

Henry

Well, I have been struggling with this some more and I am the same error
message which said something to the effect that I couldn't overload the
contructor...
The errors I am getting now are:
G:\Projects\Report Manager\Win Client\ComboBox.cs(27): The type or namespace
name 'dr' could not be found (are you missing a using directive or an
assembly reference?) ...( five of these)

and

G:\Projects\Report Manager\Win Client\ComboBox.cs(19): The type or namespace
name 'IDataReader' could not be found (are you missing a using directive or
an assembly reference?) (one of these)

Here is my revamped sub-classed ComboBox

using System;

using System.Windows.Forms;

using Microsoft.Practices.EnterpriseLibrary.Data;





namespace com.onproject.controls

{

/// <summary>

/// Summary description for ComboBox.

/// </summary>

public class ComboBox : System.Windows.Forms.ComboBox

{

// I overroad the ComboBox constructor to pass in a Database Object

// from the Microsoft.Practices.EnterpriseLibrary.Data application
block

public ComboBox(Database _db)

// I don't know if this was necessary, but I decided to use a
private copy

{

Database db = _db;

using (IDataReader dr = db.ExecuteReader

(CommandType.Text, "Select id, name,state,root_org_id," +

"original_id from dbo.period"))

{

while (dr.Read())

{ /****************************************************

*

****************************************************/

this.Items.Add(new Win_Client.Period(dr.GetInt32(0),
dr.GetString(1), dr.GetInt32(2),

dr.GetInt32(3), dr.GetInt32(4)));



}

}



}

}

}



In Form1 here are my initial declarations of the control at the top of the
Form1 class

private com.onproject.controls.ComboBox cmbPeriod;

Database db = DatabaseFactory.CreateDatabase("soComply");

Here is the rest of the code regarding cmbPeriod:

//

// cmbPeriod

//

this.cmbPeriod = new com.onproject.controls.ComboBox(db);

this.cmbPeriod.Location = new System.Drawing.Point(88, 120);

this.cmbPeriod.Name = "cmbPeriod";

this.cmbPeriod.Size = new System.Drawing.Size(121, 21);

this.cmbPeriod.TabIndex = 0;

this.cmbPeriod.Text = "cmbPeriod";

cmbPeriod.SelectedIndexChanged += new
System.EventHandler(this.cmbPeriod_SelectedIndexChanged);

I have attempted to initialize the cmbPeriod in the Form1 constructor and in
the Form1_Load event handler, both with the same results.

This is making me wonder about the Database parameter that gets passed in...
I think the IDataReader should exist from the passed in reference, but it is
not recognized.

My next attempt will be to put the data loading code into a separate public
method and explicitly invoke that after the other stuff is done.

If you have any ideas about this method does not work, please let me know.
 
B

Bruce Wood

This is the important message:
G:\Projects\Report Manager\Win Client\ComboBox.cs(19): The type or namespace name 'IDataReader' could not be found (are you missing a using directive or an assembly reference?) (one of these)

The other five, I think, are consequences of this one.

The IDataReader interface is defined in the System.Data namespace. Have
you tried putting

using System.Data;

at the top of your .cs file? I can't recall whether the System.Data.dll
library is included automatically as a reference in new projects. If
not, you may have to bring up the Solution Explorer, right-click on
References for your project, select Add Reference... and add
System.Data.dll.
 
B

Bruce Wood

As a semi-related aside, let me tell you how I solved this same
problem.

I created a derived combo box that understood how to display arbitrary
objects: I allowed a setter for the Items property, and supplied a
DisplayFormat property in which I could supply format strings based on
properties of the item, for example:

{SpeciesCode} - {Description}

which displays the result of the SpeciesCode property, followed by a
space, a dash, and a space, followed by the result of the Description
property.

I then created my own data binding: a specialized object that would
link this combo box to a properties of a "model" object (one property a
collection of objects for the combo box contents, the other property a
single object representing what's selected in the combo box).

Both the combo box and the "model" object raise events whenever
something changes. The binding listens to the events and manipulates
the "other side" (the combo box or the "model") whenever something
changes.

For example, the user changes the combo box selection. The binding
handles the SelectedIndexChanged event, reads the currently selected
object from the combo box, and sets the model property for the current
selection. Or, a caller adds an item to the collection in the model,
which raises an event, which the binding handles and refreshes the
combo box with the new collection of items to offer.

It works well, and the combo box (presentation) and the model (business
layer) are clearly separated, with the binding object mediating between
them.

In my forms' OnLoad methods, I just connect controls to the model via
bindings, and from then on I just deal with the model: the bindings
take care of presentation and responding to use gestures.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top