|
-
Jun 2nd, 2003, 04:26 PM
#1
Thread Starter
Frenzied Member
Why this works?
Why the follwing code (bold section) does not make any exception??(System.NullReferenceException)
VB Code:
Dim adapt As New Data.OleDb.OleDbDataAdapter(slectiontext,myconnection)
Dim ds As New DataSet()
adapt.FillSchema(ds, SchemaType.Mapped)
[b]Dim tb As DataTable = ds.Tables(0)
MessageBox.Show(tb.Columns(0).ColumnName)[/b]
but the follwing does:
VB Code:
Dim tb As DataTable
Dim clm As New DataColumn("first")
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
-
Jun 2nd, 2003, 05:13 PM
#2
Frenzied Member
Re: Why this works?
Originally posted by Lunatic3
VB Code:
Dim adapt As New Data.OleDb.OleDbDataAdapter(slectiontext,myconnection)
Dim ds As New DataSet()
adapt.FillSchema(ds, SchemaType.Mapped)
[b]Dim tb As DataTable = ds.Tables(0)
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:
Dim tb As DataTable
Dim clm As New DataColumn("first")
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.
-
Jun 2nd, 2003, 05:28 PM
#3
Thread Starter
Frenzied Member
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
-
Jun 2nd, 2003, 07:24 PM
#4
Frenzied Member
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:
Dim i As Int32
i = 5
MessageBox.Show(i.ToString()) '5
add(i)
MessageBox.Show(i.ToString()) '12
Public Sub add(ByRef a As Int32)
a = 12 'any change here will reflect in the above variable since we are changing what a is pointing to which is i
End Sub
-
Jun 3rd, 2003, 12:50 AM
#5
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|