tags:

views:

24

answers:

2

I am new to VB.Net and I'm a little confused why this line happened to be valid in VB:

DataGridView1.DataSource = ds.Tables("Customerslist")

DataSource is of type Object while Tables("Customerslist") is of type DataTable. How will I know what types of objects can be assigned to the Datasource property?

+2  A: 

DataTable derives from Object, so can be assigned to any Object variable.

From MSDN (DataSource):

The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:

  • The IList interface, including one-dimensional arrays.
  • The IListSource interface, such as the DataTable and DataSet classes.
  • The IBindingList interface, such as the BindingList class.
  • The IBindingListView interface, such as the BindingSource class.
Oded
So why did they make DataSource an Object instead of DataTable? And how will I know what are the objects that will populate the DataGridView. Does this mean that any type of object can be assigned to DataSource?
rajeem_cariazo
@rajeem_cariazo - in theory it could be an object of any type, but in practice it can only be one of the types listed as there will be some code that checks for these types (or more correctly these interfaces) before using the value. You could use your own class as long as it implemented one of the interfaces listed here.
ChrisF
Is there a way to know these Interfaces in the IDE?
rajeem_cariazo
+1  A: 

In .NET all classes are ultimately derived from object so field/property of type object can be used to store a reference to anything.

This can be useful when you need to store a reference to something that might be one of a number of potentially related types. It does mean though that when you come to use that reference you need to check what type it really is.

ChrisF