Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overloading a windows form constructor 1

Status
Not open for further replies.

sodakotahusker

Programmer
Mar 15, 2001
601
I want to have a form that normally is opened with no parameters and just presents a list of names users can choose from to populate the rest of the form for data entry purposes. Now I want to enter that same form but know specifically which name I want to choose from the DropDown. I was thinking I could create an overload of the constructor and pass in the value to that and have the code execute in the the called form?

something like public void new(string myValue)

I have never done something like this so maybe it is just not possible.

This alternative works just fine but I'd like to learn how to use the power of C# so I am exploring "Best Practices"


myForm f = new myForm();
f.Show();
f.cboList.Text = myvalue;

 
string nameOf = string.Empty;
// OverLoad calling original
internal frmFind(string name): this()
{
nameOf = name;
}
internal frmFind()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}

- free online Compare/Diff of snippets
 
Ok so let me analyze each new line

string.Empty - must be a way to initialize a variable instead of using = ""?

what is the purpose of :this() ?

If I call this form with the string paramter - then the Initialcomponent code will not be invoked unless I add it to the new overloaded contructor - correct (as well as any other code I might have in the original constructor)?



 
string.Empty - must be a way to initialize a variable instead of using = ""?
Using string.Empty is preferred because there's only a reference to a single empty string being used, rather than all the separate ""'s all over the place (each of which is a string object in it's own right). This reduces the memory used by your program, and lessens the load on the garbage collector when it cleans up short-term objects.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top