.Net Class Diagrams
A class diagram is an effective way to map out and clarify
the design of a .Net object. Here’s an
example:
A Class Diagram
Example
|
|
|
|
|
|||||
|
ExcelWorker
|
|
|
Class Name
|
|||||
|
|
||||||||
|
|
|
|
||||||
|
|
|
|
|
|||||
|
int customerId
|
|
|
Class Field Names with Data Types
|
|||||
|
string itemName
|
|
|||||||
|
|
|
|
||||||
|
|
|
|
|
|||||
|
String
|
ConnectToExcel
|
|
Class Methods and Return Values
|
|||||
|
DataTable
|
ReadExcel
|
|||||||
|
Boolean
|
UpdateExcel
|
|||||||
|
|
||||||||
|
|
|
|
How to Build from the
Class Diagram
1. Add a Class to the Project
In the solution explorer, right click on the project, select
add class and provide a descriptive name.
In my example, the class is named ExcelWorker since it will be working
with data in a spreadsheet.
2. Add the Class Fields
Although it is not always apparent up front how many
variables and what type will be needed, the class diagram indicates that we at
least need an int and a string variable named customerId and customerName
respectively. Class variables are known
as “fields.”
These are declared at the top of the class within the first
class brace:
class ExcelWorker
{
public int
CustomerId;
public string
CustomerName;
}
These can be initialized in one of two ways: either after
the object has been initialized …
ExcelWorker builder = new ExcelWorker();
builder.CustomerId = 123;
builder.CustomerName = "Bob";
or they can be initialized when the object is initialized:
ExcelWorker builder = new ExcelWorker() {CustomerId =
123, CustomerName = "Bob"};
The Importance of
Class Fields
The fields will keep track of the data
for each instance of the class. For example,
if a second ExcelWorker object named ratchet is declared and its fields
initialized, the builder and ratchet class objects will have separate field
values.
ExcelWorker ratchet = new ExcelWorker() {CustomerId =
456, CustomerName = "Page"};
The CustomerId and CustomerName values for builder will be
123 and “Bob” respectively and 456 and “Page” for ratchet.
3. Add the Class Methods and Return Types
The class diagram indicates that three methods are needed in
the class:
Return Type
|
Method Name
|
String
|
ConnectToExcel
|
DataReader
|
ReadExcel
|
Boolean
|
UpdateExcel
|
The ConnectToExcel method declares and uses a connection
string, database connection and database command in order to connect to the
excel workbook. The ReadExcel method
uses the open connection along with a DataReader to read and loop through the
data in the excel sheet. Finally,
UpdateExcel is used to make updates to the worksheet using the command object.
4. Declare a New Instance of the Object
The plain vanilla way to
instantiate an object:
ExcelWorker builder = new
ExcelWorker();
The second way:
ExcelWorker builder;
builder = new ExcelWorker();
And of course the third way which initializes the class
fields as mentioned previously:
ExcelWorker builder = new ExcelWorker() {CustomerId =
123, CustomerName = "Bob"};
5. Static Classes and Methods
If the entire class is declared
as static - public static
int Counter(int
theNumber)
- then it will not be necessary to declare a new class
object. Instead the class and its
methods, which also must be static, can be referenced directly in the code. In this example, the ReadExcel method of the
ExcelWorker class is called directly and the int 456 is passed to it.
private void
button1_Click(object sender, EventArgs e)
{
ExcelWorker.ReadExcel(456);
}
Also, if only the ReadExcel method is declared as static,
then it can be referenced directly in the code but the other methods,
ConnectToExcel and UpdateExcel, if not static, must be referenced by creating a
new instance of the class.
class ExcelWorker
{
public int
customerId;
public string
customerName;
public static int ReadExcel(int
theNumber)
{
return 123 + theNumber;
}
}
private void
button1_Click(object sender, EventArgs e)
{
ExcelWorker.ReadExcel(456);
}
So, Why Not Make All
Methods Static?
If you declare multiple instances of a class, then each
object will be able to keep up with distinct data via the class fields. For example:
I might need two instances of the ExcelWorker class bulderOne and
builderTwo. The first object might need
a string, customerName value of “Bob” while the second is named “Joe”. If the class is static, it would not maintain
separate values for the objects since two separate objects do not exist.
Summary
Class diagrams are useful in capturing the “mental model” of
a class and mapping out its desired functionality through the detail of
necessary fields and methods. Although
it is not always clear up front what methods and fields will be needed, this
pre-planning process can help flesh out the needed functionality for the class
and it may reveal some caveats in the model.
It should lead to more robust, quality code and time savings down the
road.