R
RP
I found a code in a book of a class:
=====================================================
Class Products
{
string ProductName;
int UnitPrice;
int Quantity;
// Empty Constructor
public Products () { }
//Constructor
public Products(string _ProductName, int _UnitPrice, int _Quantity)
{
ProductName = _ProductName;
UnitPrice = _UnitPrice;
Quantity = _Quantity;
}
//Properties
public string vProductName
{
get
{
return ProductName;
}
set
{
ProductName = value;
}
}
public int vUnitPrice
{
get
{
return UnitPrice;
}
set
{
UnitPrice=value;
}
}
public int vQuantity
{
get
{
return Quantity;
}
set
{
Quantity=value;
}
}
}
========================================================
I want to know:
(1) Why an empty constructor is used and what is its use?
(2) If the second constructor is used to assign values, is there a
need to have a destructor too?
(3) After using a second constructor to initalize the variables, why a
property is used to "GET" and "SET" values.
(4) I have a Windows Form with three Text Boxes: txtProductName,
txtUnitPrice, txtQuantity and a Command Button "Save". When the user
clicks the Save button, the values of the Text Boxes must get
assisgned to class variables and then the record should get inserted
in a table. I want to know whether the code to insert a record is to
be written in the above class itself or in the Windows Form.
Please illustrate the insert code where class variables are used and
values of variables are passed to insert query in a manner where SQL
Injection Attack is not possible.
I am using C# 2005 and SQL Server 2005.
=====================================================
Class Products
{
string ProductName;
int UnitPrice;
int Quantity;
// Empty Constructor
public Products () { }
//Constructor
public Products(string _ProductName, int _UnitPrice, int _Quantity)
{
ProductName = _ProductName;
UnitPrice = _UnitPrice;
Quantity = _Quantity;
}
//Properties
public string vProductName
{
get
{
return ProductName;
}
set
{
ProductName = value;
}
}
public int vUnitPrice
{
get
{
return UnitPrice;
}
set
{
UnitPrice=value;
}
}
public int vQuantity
{
get
{
return Quantity;
}
set
{
Quantity=value;
}
}
}
========================================================
I want to know:
(1) Why an empty constructor is used and what is its use?
(2) If the second constructor is used to assign values, is there a
need to have a destructor too?
(3) After using a second constructor to initalize the variables, why a
property is used to "GET" and "SET" values.
(4) I have a Windows Form with three Text Boxes: txtProductName,
txtUnitPrice, txtQuantity and a Command Button "Save". When the user
clicks the Save button, the values of the Text Boxes must get
assisgned to class variables and then the record should get inserted
in a table. I want to know whether the code to insert a record is to
be written in the above class itself or in the Windows Form.
Please illustrate the insert code where class variables are used and
values of variables are passed to insert query in a manner where SQL
Injection Attack is not possible.
I am using C# 2005 and SQL Server 2005.