Class, Constructor and Property

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.
 
P

Peter Duniho

[...]
(1) Why an empty constructor is used and what is its use?

So the class can be instantiated without specific initial values.
(2) If the second constructor is used to assign values, is there a
need to have a destructor too?

There's no such thing as a destructor in C#.
(3) After using a second constructor to initalize the variables, why a
property is used to "GET" and "SET" values.

Properties allow a specific form of encapsulation. A Google Groups
search in this newsgroup should turn up a number of threads offering
examples of use of properties, as well as threads discussing why one
would want to use the encapsulation properties offers.
(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.

There's no way to know without knowing more about the context. It all
just depends on whether you want the Products class to know how to
insert itself into a database or not.
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.

The main thing is to not blindly use the class properties as direct
elements in a string concatenation forming the SQL query. There are
parameter-based ways to construct and use a SQL query, and those would
be used in this case.

For further advice, your teacher may be a better source, since
presumably that person knows more about the specific context and what
you are supposed to be doing.

Pete
 
M

Michael Nemtsev, [MVP]

Hello RP,

R> (1) Why an empty constructor is used and what is its use?

It's the C# language design.
if you don't provide this, it's provided implicitly

R> (2) If the second constructor is used to assign values, is there a
R> need to have a destructor too?

no. what forced you to think so?

R> (3) After using a second constructor to initalize the variables, why
R> a property is used to "GET" and "SET" values.

it's a good practice to refers to your private variables via properties.
because it allows your to change properties easily and introduce additional
features, like validation end etc.

R> (4) I have a Windows Form with three Text Boxes: txtProductName,
R> txtUnitPrice, txtQuantity and a Command Button "Save". When the user
R> clicks the Save button, the values of the Text Boxes must get
R> assisgned to class variables and then the record should get inserted
R> in a table. I want to know whether the code to insert a record is to
R> be written in the above class itself or in the Windows Form.

it's better to provide different layers for your app, splitting logic on
these layers
So, winforms only for those what relates to UI, "data access layers" to connect
to DB and get data, "business layer" to provide extracted data to UI

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
 
G

Guest

(3) After using a second constructor to initalize the variables, why a
property is used to "GET" and "SET" values.

Because you may want to set the variables after initialization. If you did
not have get/set's and the variables were declared as private then the only
time you could set its values would be during initialization. Hope that helps.
 
L

Lew

RP said:
property is used to "GET" and "SET" values.

Tantr said:
Because you may want to set the variables after initialization. If you did
not have get/set's and the variables were declared as private then the only
time you could set its values would be during initialization. Hope that helps.

Sometimes you want a class to have only get properties, and to set them in the
constructor only. This is part of making a class of "immutable" objects, ones
whose values do not change after construction. Immutable objects can be safer
in multi-threaded code, among other advantages.

<http://www.informit.com/content/images/0321245660/items/wagner_item7.pdf>
 
A

Alun Harford

Your direct questions have been answered by other posters, so I just
thought I'd add some comments...
I found a code in a book of a class:
=====================================================
Class Products

class is a keyword, and must be lower-case (or the code won't compile).
{
string ProductName;
int UnitPrice;
int Quantity;

It's unusual to begin private member variables with a capital. This
normally indicates a property.
// Empty Constructor
public Products () { }

The empty constructor allows you to call: new Products();
You don't need to provide all of the values until later.
//Constructor
public Products(string _ProductName, int _UnitPrice, int _Quantity)
{
ProductName = _ProductName;
UnitPrice = _UnitPrice;
Quantity = _Quantity;
}

Most people would consider adding an underscore to the parameters of a
public method or constuctor a bad idea (as this is what other people
using your class will see). You can do:

public Products(string ProductName, int UnitPrice, int Quantity)
{
this.ProductName = ProductName;
this.UnitPrice = UnitPrice;
this.Quantity = Quantity;
}

Another option is to use the _ for your private member variables.
//Properties
public string vProductName

Beginning public property names is generally considered a bad idea.

<snip>

You might like to know that in C# 3 you can write this class as:

class Products
{
public Products() { }
public string ProductName { get; set;}
public int UnitPrice { get; set;}
public int Quantity { get; set;}
}

The book you are using doesn't seem very good - particularly if it
doesn't explain the answers to your questions. You're probably best
getting a better one.

Alun Harford
 
C

Christof Nordiek

Michael Nemtsev"; " said:
Hello RP,

R> (1) Why an empty constructor is used and what is its use?

It's the C# language design.
if you don't provide this, it's provided implicitly

Not in this case, because there is another constructor defined. The implicit
constructor is only created, if no constrocutor is explicitly defined at
all.

Christof
 
B

Ben Voigt [C++ MVP]

Christof Nordiek said:
Not in this case, because there is another constructor defined. The
implicit constructor is only created, if no constrocutor is explicitly
defined at all.

For "struct" types, the rule is of course the opposite, with a .NET-provided
default constructor provided in all cases (and you cannot redefine it).
 
R

RP

Can I include validations in a constructor to verify the values being
assigned to member variables, example:

if (NewMember.bplNo == null)
NewMember.bplNo = 0;
 
P

Peter Duniho

Can I include validations in a constructor to verify the values being
assigned to member variables, example:

if (NewMember.bplNo == null)
NewMember.bplNo = 0;

You can do whatever you like in a constructor. Other than it having a
specific syntax, for use when instantiating an object, it's pretty much
like any other method.

Now, the code you posted above will only work if "bplNo" is a nullable
type that can be assigned an int (e.g. "int?" or some reference type
that has an implicit conversion from int). Hopefully the reply to your
other thread explains that sufficiently for you.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top