开发者

Assigning objects of different type

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜