Object instantiations

Discussion in 'ASP .Net' started by Guest, Jan 11, 2005.

  1. Guest

    Guest Guest

    Hello All,

    Can someone please answer my question on how the following objects would be
    instantiated?

    - - - - - - - - - - - -

    public class CustomPaging
    {
    private int _PageSize = 10; // Default page size
    secure.WebServices.General objGeneral = new secure.WebServices.General();
    private DataSet _DataSet = new DataSet();
    private DataTable _DataTable = new DataTable();

    public CustomPaging()
    {
    //
    // TODO: Add constructor logic here
    //

    }

    ........

    method1()
    { }

    method2()
    {}

    } // end of class


    - - - - - - - - - -- - - - - - -

    In the above piece of code, I am instantiating few variables and objects
    just after public class CustomPaging. My question is if suppose I create an
    object of this class and call method1 followed by method2....how many times
    would the objects be created?

    I know that had I kept the initialization code in the constructor, they
    would be initialized only once, i.e., when the object is created. But how
    about in this case where they are not initialized in the constructor?
     
    Guest, Jan 11, 2005
    #1
    1. Advertisements

  2. Guest

    Karl Seguin Guest

    Only once. I want to say it's the same as initializing them in the
    constructor (and you won't see any differences, it'l behave the same) but
    there are probably design differences or very technical differences I'm not
    aware of...

    Karl
     
    Karl Seguin, Jan 11, 2005
    #2
    1. Advertisements

  3. Guest

    bruce barker Guest

    the main difference is they are created before the class constructor is
    called.

    -- bruce (sqlwork.com)



    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message | Only once. I want to say it's the same as initializing them in the
    | constructor (and you won't see any differences, it'l behave the same) but
    | there are probably design differences or very technical differences I'm
    not
    | aware of...
    |
    | Karl
    |
    | --
    | MY ASP.Net tutorials
    | http://www.openmymind.net/
    |
    |
    | | > Hello All,
    | >
    | > Can someone please answer my question on how the following objects would
    | be
    | > instantiated?
    | >
    | > - - - - - - - - - - - -
    | >
    | > public class CustomPaging
    | > {
    | > private int _PageSize = 10; // Default page size
    | > secure.WebServices.General objGeneral = new
    | secure.WebServices.General();
    | > private DataSet _DataSet = new DataSet();
    | > private DataTable _DataTable = new DataTable();
    | >
    | > public CustomPaging()
    | > {
    | > //
    | > // TODO: Add constructor logic here
    | > //
    | >
    | > }
    | >
    | > ........
    | >
    | > method1()
    | > { }
    | >
    | > method2()
    | > {}
    | >
    | > } // end of class
    | >
    | >
    | > - - - - - - - - - -- - - - - - -
    | >
    | > In the above piece of code, I am instantiating few variables and objects
    | > just after public class CustomPaging. My question is if suppose I create
    | an
    | > object of this class and call method1 followed by method2....how many
    | times
    | > would the objects be created?
    | >
    | > I know that had I kept the initialization code in the constructor, they
    | > would be initialized only once, i.e., when the object is created. But
    how
    | > about in this case where they are not initialized in the constructor?
    | >
    | >
    |
    |
     
    bruce barker, Jan 11, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.