Classes and constructors

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. AS2.0 & inner classes
      Greetings... Does ActionScript 2.0 allow the creation of inner classes? Thanks. Respectfully, ASP
    3. 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...
    4. #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: ...
    5. #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: ...
  3. #2

    Default 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...
    > 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!
    > >
    > >
    > >
    >
    >

    Null Guest

  4. #3

    Default 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...
    > 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...
    > > 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139