In.NET can create and use a special element, called a property, although at firstglance appear to be any variable, you can hide functionality as complex as weneed it.
Suppose we create a class Sale,which put a product, unit price, quantity purchased, the tax value and thetotal amount. What then would wethink the class is to create variables for the user to modify data andfunctions that calculate the others. Somethinglike this:
public class Sale {
public string Product;
public decimal unitPrice;
public int quantity;
public decimal iva () {
return unitPrice * 0.16;
}
public decimal importeTotal () {
return (unitPrice + vat ()) * amount;
}
}
Problem solved, apparently. Now, suppose we have a whole list ofSales, obtained from a database, and we want to insert into a DataGridView. What do we do? Well, we can create a DataTable and beinserting data one by one.
DataTable dt = new DataTable ();
/ / Create columns for the DataTable
foreach ( Sale sale in List < For Sale >) {
DataRow dr = dt.NewRow ();
dr [ "Product" ] = venta.producto;
dr [ "UnitPrice" ] = venta.precioUnitario;
dr [ "Quantity" ] = venta.cantidad;
dr [ "VAT" ] = venta.iva ();
dr [ "ImporteTotal" ] = venta.importeTotal ();
}
Now we have our table full, andwe can use within the Grid.
But to our surprise (anddisgust, after much work), there are easier ways to do this by defining thesevariables and methods and properties .
A property is a kind ofcombination between what is a variable and a method as it is defined like thesecond, but is used as the first.
string variable
public string Property {
get {
return variable;
}
September {
variable = value ;
}
}
A property has two sections: get , which returns a value, when theproperty is consulted, and in September , which receives a value within thekeyword value, the same type of property, and is used within the same.
property = "hello" / / get invoke
string str = property; / / is invoked in September
Here we can see the differencevariables. We use a property todisplay the value of a variable inaccessible from outside the class, whichmight have been modified internally. Ourclass may be for sale then this:
public class Sale {
private string _Products;
public string Product {
get { return _Products;}
September {_Products = value ;}
}
private decimal _precioUnitario;
public decimal unitPrice {
get { return _precioUnitario;}
September {_precioUnitario = value ;}
}
private int _cantidad;
public int number {
get { return _cantidad;}
September {_cantidad = value ;}
}
public decimal Tax {
get { return _precioUnitario * 0.16;}
}
public decimal importeTotal {
get { return (_precioUnitario + VAT) * _cantidad;} / / We can even call a property from another!
}
}
ImporteTotal property taxes andno section
in September
, as they are calculated valuesbased on other data. If we fail in September
,the property will be read-only , and if we omit get
,will be write-only .Now, how do we resolve thematter to fill a grid with a list of sales? Allwe have to do is assign that list as DataSource of the grid, mapping thecolumns of this to the names of the properties and go. No need to fill a DataTable as a Listis considered a data source, and all properties for sale that has the class canbe used as columns.
Problem solved, and in a snap.
Note: Private variables declaredbefore each property (_Products, _cantidad, etc..) store the correspondingproperty value, since it can not store a value. But in C # 3.0 (the Visual Studio2008) and higher, you need not be declared a private variable to store thevalue of the property. If you arenot going to perform special functions on the properties, we can declare"hiding" the contents of the sections get and set. That is, we do this:
public string Product {
get ;
September ;
}
I repeat: This is only for C #3.0 and up. For C # 2.0 is stillnecessary to have the property values in separate variables, whether public or private.
0 comments:
Post a Comment