Class, Constructor and Property - II

  • Thread starter Thread starter RP
  • Start date Start date
R

RP

Refer to my original post at:

http://groups.google.com/group/micr...Class,+Constructor,+Property#2b202ff938fed637

I want to know:

(1) Is it necessary to define SET property if the constructor is
already there?

(2) When the constructor has been instantiated, I want to insert the
values in a table. Where the code to insert records must reside, in
the class itself or in the Button_Click event of Windows Form.

(3) Where I can find information about C# naming convention which is
used as a standard by developers?
 
RP said:
Refer to my original post at:

http://groups.google.com/group/micr...Class,+Constructor,+Property#2b202ff938fed637

I want to know:

(1) Is it necessary to define SET property if the constructor is
already there?
No. Only if you want to change the property *after* the object has been
constructed
(2) When the constructor has been instantiated, I want to insert the
values in a table. Where the code to insert records must reside, in
the class itself or in the Button_Click event of Windows Form.
I would not implement it inside the class, as it ties the class to a
specific purpose. however, I can't tell if the button click is a good
place to do so.
(3) Where I can find information about C# naming convention which is
used as a standard by developers?

http://msdn2.microsoft.com/en-us/library/czefa0ke(vs.71).aspx

Can be enforced using FxCop. If you are using VS 2005, open the project
properties and navigate to "Code Analysis". If not, you can download the
FxCop tool.

HTH,
Andy
 
I am still not following why a constructor and a property [with
GET ... SET] is used simultaneously. I noticed it in many examples. I
am new to OOP. The class which I created [see my original post] uses
both a constructor and properties. After setting values in property, I
am puzzled where to use a constructor and for what?
 
There is no requirement to have a property in a class, and there is no
correlation from a constructor to a property. A property is simply a way of
encapsulating the desire to change a value of a facet of the object.

Some things, such as binding, only work with properties and not members.

Properties can be used to mask certain pieces of code behind the scenes.
For example, if you have a property on an object of class TicTacToeCell,
called CellValue, you may desire in the property set method, to check the
board to see if it causes a "win". If you did this with members rather than
properties, it would be somewhat more difficult.

I hope this helps a little.

RP said:
I am still not following why a constructor and a property [with
GET ... SET] is used simultaneously. I noticed it in many examples. I
am new to OOP. The class which I created [see my original post] uses
both a constructor and properties. After setting values in property, I
am puzzled where to use a constructor and for what?

No. Only if you want to change the property *after* the object has been
constructed
 
I am still not following why a constructor and a property [with
GET ... SET] is used simultaneously. I noticed it in many examples. I
am new to OOP. The class which I created [see my original post] uses
both a constructor and properties. After setting values in property, I
am puzzled where to use a constructor and for what?

A constructor is only used once per instance of your class. You use it
with the "new" operator to create the instance. A constructor can take
parameters, which are used for the initialization of the class. In
some cases, these parameters are used directly to initialize data
within the class that is exposed as properties, in other cases the
parameters are used for other purposes (examples: provide a default
capacity in a collection, specify a path for a file-based class,
provide flags that define how an instance should behave, etc.)

You can put pretty much whatever you want as a parameter for a
constructor. Any piece of data that your class needs in order to be
initialized correctly is fair game.

Properties, on the other hand, are single, individual characteristics
of an instance. A class might allow initialization of a property via a
constructor, but for properties that are read/write there is no
requirement to do so. The value of such a property could be changed at
any time, via the property itself or via other means.

Constructors and properties are not mutually exclusive. They each have
specific roles in the definition of a class, and while they sometimes
relate to each other as I described above, they are basically two
different things, intended for two different purposes.

Pete
 
Back
Top