Click here to Skip to main content
15,920,618 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
can anyone please guide me about the true functionalty of a constructor.
I studied it from the lots of places but still cannot understand completely when it is used.
Please give some example how we can use them in .net application and when it is sufficient to use them.
Posted
Updated 2-Mar-11 1:36am
v2

You use a constructor whenever you create an instance of a class.

So whenever you see the new keyword, you are looking at a call to a constructor. It may be a default constructor, with no visble code, but it will be called.

Since everything in C# is derived from a single class (the object class) everything you use in you application has a constructor which is called when the instance is created, where it is a string, a Form, or a SQL database connection.

What do you do with a constructor? Set up your instance so that it will work if nothing else is done to it. Or set up internal fields via parameters.

This is a big subject, and difficult to cover at a large remove. I would suggest that you ask your tutor: he will at least be able to see your reactions and explain confusing parts again.
 
Share this answer
 
Comments
codegeekalpha 2-Mar-11 7:46am    
actully most of the time i leave constuructor empty.. once i use it.. i define a fuction for getting a data from combo box.. and put that fuction in constructor...

so when i debug the applcation.. i just.. get the values from database in combobox..
before i put the code in a button event.. and when i press button i get the valuse in combobox..
but lator i make a function for getting data from access database and then put it the fuction call in constructor.. then i getting it filled.. as soon as the appliction run..
OriginalGriff 2-Mar-11 8:20am    
"most of the time i leave constuructor empty"
Most of the time you will leave it empty, or remove the empty method, because you don't need it.
"before i put the code in a button event."
That's right: as soon as an instance of your class is created (by you or by the framework for you) the constructor will be called.
It is used to initialize the state of the object itself (either using passed parameters or default values). I.e. it is an opportunity, for the developer, to put the object in a well known state, before it is actually used.
Suppose you're coding your own Stack class. Probably it has a tos (top of the stack) member variable. The Stack class constructor should set tos=0, putting the object in a well know state (empty stack) after contruction.
:)
 
Share this answer
 
For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.

For examples you can search google. There are lots of them.
 
Share this answer
 
v2
This question is very inappropriate.

Is is simply not practical to ask question like that. I means your knowledge is still too premature; so you need to read and learn by you own first, which is much more effective on your level. After some learning, you will be ready to get much more effective help from Questions and Answers.

Please see my recommendations here: I have a problem with my program. Please help![^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900