Ask a Question related to ASP.NET General, Design and Development.
-
Marina #1
Re: Classes and constructors
You are not required to have a constructor with every class.
Constructors should be used to ensure that your object is in a consistent
state. For example, for a FileStream object, you want to make sure the user
supplies the path to the file to be opened. Otherwise, it doesn't make sense
to have a FileStream object. If the only constructor available is one that
takes a file path string - then users will have to use it. This will ensure
that your FileStream object is always valid - always pointing to some file.
And you constructor does some low level stuff to find the file, etc.
On the other hand, maybe you are writing a Stack object. A Stack object
doesn't necessarily need to be initialized with anything. So maybe here,
you don't define a constructor (in which case there is a default no
parameter constructor that gets created).
It really just depends on what you are doing. Constructors are meant for
initializing objects, and making sure that they have a valid state.
"Null" <farthumstill@hotmail.com> wrote in message
news:udy0QYrWDHA.1872@TK2MSFTNGP12.phx.gbl...> Hello - I have a basic question:
>
> Do Classes always require a Constructor? Generally, what should (and
> shouldnt) go in the constructor?
>
> Any feedback/instruction would be greatly appreciated.
>
> Thanks in advance.
>
> Cheerios!
>
>
>
Marina Guest
-
C++ global constructors in xsub?
Rather than bog this post down with my particular details, I'll just ask the basic question: Is it possible to write a dynamically-loaded xsub... -
AS2.0 & inner classes
Greetings... Does ActionScript 2.0 allow the creation of inner classes? Thanks. Respectfully, ASP -
Copy constructors
I have been reading up in C++ about copy constructors, is it possable to use them in perl. I want to delete an object from one array and put it in... -
#17637 [Ana->Csd]: constructors in classes (Back to PHP3)
ID: 17637 Updated by: sniper@php.net Reported By: Arvid at knast dot dk -Status: Analyzed +Status: ... -
#17637 [Ana]: constructors in classes (Back to PHP3)
ID: 17637 Updated by: helly@php.net Reported By: Arvid at knast dot dk Status: Analyzed Bug Type: ... -
Null #2
Re: Classes and constructors
Marina,
Thanks for your feedback. That's great info for me.
So, just to make sure I understand, you said that constructors are for
initializing objects and making sure they have a valid state;
In your example, to summarize, say I was writing a class that used the
filestream object - I would want to include filepath in the constructor and
this would ensure that the object has a valid state -- but what about
initializing objects? Does that mean that in a class that uses other
classes, it is in the constructor that the object initialization should take
place?
Once again, thank you for your feedback. It's much appreciated.
"Marina" <zlatkinam@nospam.hotmail.com> wrote in message
news:uhTX0drWDHA.1492@TK2MSFTNGP12.phx.gbl...user> You are not required to have a constructor with every class.
>
> Constructors should be used to ensure that your object is in a consistent
> state. For example, for a FileStream object, you want to make sure thesense> supplies the path to the file to be opened. Otherwise, it doesn't makethat> to have a FileStream object. If the only constructor available is oneensure> takes a file path string - then users will have to use it. This willfile.> that your FileStream object is always valid - always pointing to some> And you constructor does some low level stuff to find the file, etc.
>
> On the other hand, maybe you are writing a Stack object. A Stack object
> doesn't necessarily need to be initialized with anything. So maybe here,
> you don't define a constructor (in which case there is a default no
> parameter constructor that gets created).
>
> It really just depends on what you are doing. Constructors are meant for
> initializing objects, and making sure that they have a valid state.
>
> "Null" <farthumstill@hotmail.com> wrote in message
> news:udy0QYrWDHA.1872@TK2MSFTNGP12.phx.gbl...>> > Hello - I have a basic question:
> >
> > Do Classes always require a Constructor? Generally, what should (and
> > shouldnt) go in the constructor?
> >
> > Any feedback/instruction would be greatly appreciated.
> >
> > Thanks in advance.
> >
> > Cheerios!
> >
> >
> >
>
Null Guest
-
Marina #3
Re: Classes and constructors
I am not quite sure what you mean - are you talking about classes that have
instance of other classes as members?
In that case, say you have a data access object. It handles your data in
some way. It needs an connection object to get to the database. In this
case, you may want to use a constructor to initialize your connection member
variable. That way, when subsequently, a developer calls GetData (some
method that presumably fetches data), the connection object is all set and
ready to go.
Otherwise, if you rely on the developer to remember to call some method or
set some property to provide the connection information, he/she may forget,
and end up getting errors. However, if the constructor for your data access
object requires a connection string (that you then subsequently use the
initialize a connection object that is inside your data access object), then
this will not happen. It also makes sense, since presumably, providing a
connection should really be a one time event. Same with providing a path to
a file for a FileStream object - without this information the objet has no
meaning.
Sometimes you will want more then one constructor. Example: a point class.
You may provide a default constructor (no parameters), and in it, just
initialize X and Y to 0. You may provide a constructor that takes 2 int
parameters: X and Y. Then you set the internal class members to those
values. So you have a lot of flexibility with the kinds of functionality you
provide, and this all really depends on what type of class you are writing.
"Null" <farthumstill@hotmail.com> wrote in message
news:eY7xDlrWDHA.1744@TK2MSFTNGP12.phx.gbl...and> Marina,
>
> Thanks for your feedback. That's great info for me.
>
> So, just to make sure I understand, you said that constructors are for
> initializing objects and making sure they have a valid state;
> In your example, to summarize, say I was writing a class that used the
> filestream object - I would want to include filepath in the constructortake> this would ensure that the object has a valid state -- but what about
> initializing objects? Does that mean that in a class that uses other
> classes, it is in the constructor that the object initialization shouldconsistent> place?
>
> Once again, thank you for your feedback. It's much appreciated.
>
>
> "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
> news:uhTX0drWDHA.1492@TK2MSFTNGP12.phx.gbl...> > You are not required to have a constructor with every class.
> >
> > Constructors should be used to ensure that your object is in ahere,> user> > state. For example, for a FileStream object, you want to make sure the> sense> > supplies the path to the file to be opened. Otherwise, it doesn't make> that> > to have a FileStream object. If the only constructor available is one> ensure> > takes a file path string - then users will have to use it. This will> file.> > that your FileStream object is always valid - always pointing to some> > And you constructor does some low level stuff to find the file, etc.
> >
> > On the other hand, maybe you are writing a Stack object. A Stack object
> > doesn't necessarily need to be initialized with anything. So maybefor> > you don't define a constructor (in which case there is a default no
> > parameter constructor that gets created).
> >
> > It really just depends on what you are doing. Constructors are meant>> > initializing objects, and making sure that they have a valid state.
> >
> > "Null" <farthumstill@hotmail.com> wrote in message
> > news:udy0QYrWDHA.1872@TK2MSFTNGP12.phx.gbl...> >> > > Hello - I have a basic question:
> > >
> > > Do Classes always require a Constructor? Generally, what should (and
> > > shouldnt) go in the constructor?
> > >
> > > Any feedback/instruction would be greatly appreciated.
> > >
> > > Thanks in advance.
> > >
> > > Cheerios!
> > >
> > >
> > >
> >
>
Marina Guest



Reply With Quote

