I have a class like this
public class Tbl
{
public string Name {get; set}
public anyClass Datasource {get; set;} //I don't know what to use there
}
//Usage:
List<anyClass> anyList = GetList(); // Assuming I had a list
Tbl Table = new Tbl();
Table.Name = "Table1";
Table.Datasource = anyList;
Here, my propblem is making the Datasource can accept any input Class. How can I declare the Datasource for Tbl class right way?
Thanks very much
The collection of properties assigned to a class defines the class. A class can have multiple properties. For example, objects classified as computers have the following properties: Hardware ID, Manufacturer, Model, and Serial Number.
It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter. The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property.
A property can be declared inside a class, struct, Interface.
Properties do not name the storage locations. Instead, they have accessors that read, write, or compute their values. For example, let us have a class named Student, with private fields for age, name, and code.
If it was Tbl<T>
, you might choose to expose IList<T>
as the DataSource
:
public class Table<T>
{
public string Name {get; set}
public IList<T> DataSource {get; set;}
}
For non-generic data you might choose to use the non-generic IList
; however, in the core framework it is fairly routine to use object
as a DataSource
, as this allows use of both IList
and IListSource
(an abstraction around obtaining a list).
You use the type Object
:
public object Datasource { get; set; }
If you want to use generics to specify the type:
public class Tbl<T> {
public string Name { get; set }
public T Datasource { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With