locked
Confusion with New keyword. RRS feed

  • Question

  • User1408399351 posted

    Hello,

    I am having confusions regaqrding use of 'new' keyword:-

    1) when useing a data reader; why dont we have to use: Dim rdr as new datareader=cmd.executeReader() ?

    2) when we are creating a new DataRow, we cannot use   Dim dr As New DataRow?

    Monday, November 25, 2013 2:27 AM

Answers

  • User541108374 posted

    Hi,

    the new keyword is being used to spawn up a new object, basically an instance of a given class.

    As ExecuteReader already provides an instance/object, rdr will reference that object hence no need for the use of New here.

    For 2 the constructor is marked Internal and as suggested is only to be used by framework classes not by you.

    The way to create a new DataRow is to make a DataTable first and there use the method NewRow() as explained in http://msdn.microsoft.com/en-us/library/system.data.datarow(v=vs.110).aspx.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2013 2:32 AM

All replies

  • User541108374 posted

    Hi,

    the new keyword is being used to spawn up a new object, basically an instance of a given class.

    As ExecuteReader already provides an instance/object, rdr will reference that object hence no need for the use of New here.

    For 2 the constructor is marked Internal and as suggested is only to be used by framework classes not by you.

    The way to create a new DataRow is to make a DataTable first and there use the method NewRow() as explained in http://msdn.microsoft.com/en-us/library/system.data.datarow(v=vs.110).aspx.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2013 2:32 AM
  • User-1716253493 posted
    I think because reader directly read from database, the reader not hold the values.
    Saturday, November 30, 2013 8:30 PM