Results 1 to 5 of 5

Thread: Why this works?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    Why this works?

    Why the follwing code (bold section) does not make any exception??(System.NullReferenceException)
    VB Code:
    1. Dim adapt As New Data.OleDb.OleDbDataAdapter(slectiontext,myconnection)
    2. Dim ds As New DataSet()
    3. adapt.FillSchema(ds, SchemaType.Mapped)
    4. [b]Dim tb As DataTable = ds.Tables(0)
    5. MessageBox.Show(tb.Columns(0).ColumnName)[/b]
    but the follwing does:
    VB Code:
    1. Dim tb As DataTable
    2. Dim clm As New DataColumn("first")
    3. tb.Columns.Add(clm)
    I know i have to add a 'New' in the second, but why its not needed in the first?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256

    Re: Why this works?

    Originally posted by Lunatic3
    VB Code:
    1. Dim adapt As New Data.OleDb.OleDbDataAdapter(slectiontext,myconnection)
    2. Dim ds As New DataSet()
    3. adapt.FillSchema(ds, SchemaType.Mapped)
    4. [b]Dim tb As DataTable = ds.Tables(0)
    5. MessageBox.Show(tb.Columns(0).ColumnName)<b>Why the follwing code (bold section) does not make any exception??(System.NullReferenceException)</b>
    but the follwing does:
    VB Code:
    1. Dim tb As DataTable
    2. Dim clm As New DataColumn("first")
    3. tb.Columns.Add(clm)
    I know i have to add a 'New' in the second, but why its not needed in the first? [/B]
    In the first code snippet, tb is pointing to a table in a dataset thats aready instatiated, thats why its not needed in the first. In the second code snippet, tb is not pointing to anything.

    Understand what the New keyword does is create an object on the managed heap. It allocates the memory. Without the new keyword, you are just creating an object that points to nothing.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    So in the first code snippet tb is just a POINTER? Is this applicable for all types and objects? I mean can you define pointers to those objects in this way?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Basically all reference types are pointers under the hood, so that makes all objects reference types..NET just makes it so we dont have to deal with * and ->.

    There are two types. Value types and reference types. When you pass a variable by value, you are making a copy and passing it. When you pass by reference, you are passing a pointer to the object in memory. Take a look at this;

    VB Code:
    1. Dim i As Int32
    2. i = 5
    3. MessageBox.Show(i.ToString()) '5
    4. add(i)
    5. MessageBox.Show(i.ToString()) '12
    6.  
    7. Public Sub add(ByRef a As Int32)
    8.         a = 12 'any change here will reflect in the above variable since we are changing what a is pointing to which is i
    9. End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Thanks, I knew about passing byRef and byVal and their difference. But i didnt know this point:
    Basically all reference types are pointers under the hood
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

Posting Permissions

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



Click Here to Expand Forum to Full Width