473,796 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.Pract ices.Enterprise Library.Data;



namespace com.onproject.c ontrols

{

/// <summary>

/// Summary description for ComboBox.

/// </summary>

public class ComboBox : System.Windows. Forms.ComboBox

{



public ComboBox(Databa se db)

{

using (IDataReader dr = db.ExecuteReade r

(CommandType.Te xt, "Select id, name,state,root _org_id," +

"original_i d from dbo.period"))

{

while (dr.Read())

{

this.Items.Add( new Win_Client.Peri od(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{retur n 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,in t _original_id)

{

this.id = _id;

this.name = _name;

this.state = _state;

this.root_org_i d = _root_org_id;

this.original_i d = _original_id;



}

public override string ToString()

{

return name;

}



}

}



Nov 17 '05 #1
6 2382
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/

Henry schrieb:
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.Pract ices.Enterprise Library.Data;

namespace com.onproject.c ontrols

{

/// <summary>

/// Summary description for ComboBox.

/// </summary>

public class ComboBox : System.Windows. Forms.ComboBox

{

public ComboBox(Databa se db)

{

using (IDataReader dr = db.ExecuteReade r

(CommandType.Te xt, "Select id, name,state,root _org_id," +

"original_i d from dbo.period"))

{

while (dr.Read())

{

this.Items.Add( new Win_Client.Peri od(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{retur n 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,in t _original_id)

{

this.id = _id;

this.name = _name;

this.state = _state;

this.root_org_i d = _root_org_id;

this.original_i d = _original_id;

}

public override string ToString()

{

return name;

}

}

}


Nov 17 '05 #2
object creation is not an expensive issue anyway.
Nov 17 '05 #3
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\Rep ort 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\Rep ort 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.Pract ices.Enterprise Library.Data;

namespace com.onproject.c ontrols

{

/// <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.Pract ices.Enterprise Library.Data application
block

public ComboBox(Databa se _db)

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

{

Database db = _db;

using (IDataReader dr = db.ExecuteReade r

(CommandType.Te xt, "Select id, name,state,root _org_id," +

"original_i d from dbo.period"))

{

while (dr.Read())

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

*

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

this.Items.Add( new Win_Client.Peri od(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.c ontrols.ComboBo x cmbPeriod;

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

Here is the rest of the code regarding cmbPeriod:

//

// cmbPeriod

//

this.cmbPeriod = new com.onproject.c ontrols.ComboBo x(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.Selec tedIndexChanged += new
System.EventHan dler(this.cmbPe riod_SelectedIn dexChanged);

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.

"Lars Behrmann" <la***********@ web.de> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
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/

Henry schrieb:
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.Pract ices.Enterprise Library.Data;

namespace com.onproject.c ontrols

{

/// <summary>

/// Summary description for ComboBox.

/// </summary>

public class ComboBox : System.Windows. Forms.ComboBox

{

public ComboBox(Databa se db)

{

using (IDataReader dr = db.ExecuteReade r

(CommandType.Te xt, "Select id,
name,state,root _org_id," +

"original_i d from dbo.period"))

{

while (dr.Read())

{

this.Items.Add( new Win_Client.Peri od(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{retur n 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,in t _original_id)

{

this.id = _id;

this.name = _name;

this.state = _state;

this.root_org_i d = _root_org_id;

this.original_i d = _original_id;

}

public override string ToString()

{

return name;

}

}

}

Nov 17 '05 #4
This is the important message:
G:\Projects\Rep ort 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 .

Nov 17 '05 #5
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 SelectedIndexCh anged 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.

Nov 17 '05 #6
yes its indeed referenced by default (in C#)
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1746
by: Jaime Ashander | last post by:
Hello, I've overloaded the istream operator >> to output the private data members of my class Word, i'm using the operator go get data from an input file with something like //--snip-- #include <iostream> #include <fstream> #include <cstdlib> int main(int argc, char* argv) { ifstream in(arcv);
10
2676
by: Benny Raymond | last post by:
I'm trying to change the way a treeview works just a bit, i'm pretty new to C# and now i'm running into overloading. I tried the following code and it's yelling at me saying that no overload method takes 0 arguments. #region tViewNodeCollection public class tViewNodeCollection : TreeNodeCollection { private tView _owner; private tViewNode _parent; /// <summary>
2
3299
by: sparks | last post by:
I had the bright idea of using if/else to call different constructors based on a web form and what the user entered. ======================================= IF I DECLARE this here everything works fine... ====>>>> Member member = new Member(); ======================================= public void Button1_Click(object sender,System.EventArgs e) { if (ContractNumber.Text =="")
2
3464
by: Tom | last post by:
How is the best way to overload the New constructor for a form? For instance, I want to be able to pass things to the constructor, or just default with the plain New. Maybe say something like: Dim x as New MyForm(parm01, parm02) Should I just override new? What is the best way to do this for a form? (The reason I am asking is it appears that the New for a form does a number of things, like initialize controls, etc)
7
1539
by: portroe | last post by:
How do you overload constructors in VB .net? thanks Portroe
6
2088
by: jay | last post by:
In the c++ primer ,i get a program. A class's name is TT,and it define the operator overload! TT first; //constructor TT second(30);//constructor TT thrid(40://constructor first=second.operator+; the question is the fourth line is all right?
6
3553
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a base class that is common to many other classes. public class Base .... end class I have 2 seperate classes that inherit from base
1
1733
by: Frederick Gotham | last post by:
The canonical way to write a copy-constructor is: MyClass::MyClass(MyClass const&); I've been wondering, however, if that should be: MyClass::MyClass(MyClass const volatile&); ? Just in case more type specifiers come into the language, we could have a
3
8300
by: needin4mation | last post by:
The code is taken from the book Professional C#: abstract class GenericCustomer { private string name; public GenericCustomer(string name) { this.name = name; }...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10201
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.