Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

Class Diagram in VS.NET 2005

Rate me:
Please Sign up or sign in to vote.
2.94/5 (29 votes)
9 Oct 20064 min read 94.6K   377   29   14
Class Diagram in VS.NET 2005

Download sourcecode 

Introduction

Class Diagrams are newly added in Visual Studio 2005 edition. They provide designers, architects and developers a graphical interface to manipulate methods, properties, fields, events for datastructures like classes,interfaces,structs,etc. In this article we will use this designer to create these datastructures without touching the code and while we are interacting with this designer we should note that the code will be generated and updated in the background.

Let us now explore all the features of Class designer.

Creating Classes using Class Diagrams

1. Create a new Class Library called MyLib and add a class Diagram called MyClassDiagram to it as shown below.

Sample screenshot

2. Lets now add a new class called Computer to it. To add a class using the class designer right click in the class diagram.  On right click you will see a popup menu  as shown below. Select Add --> Class.

Sample screenshot


3."Add New Class" dialogue will be displayed. We will enter our Classname as "Computer", select the access as public and select the option to create a new file called as Computer.cs for our class. After entering all the details click on "OK".

Sample screenshot

4.Our Class  "Computer" will be added to the class diagram. Let us now add Method,Properties,Events and fields to out class.Select the class and Right Click on it to view the popup menu as shown below.
Sample screenshot

5. Select the appropriate option to add Method,Property,Field or event. Please note an option to add a constructor and destructor is also given. Let us now add property called Monitor. Right click on the class select Add --> Field. Add a field called as monitor as shown below.
Sample screenshot

6.After adding the field select the field, right click and select Encapsulate Field. Encapsulate Field dialogue will be displayed as shown below. Input the Property name as Monitor and check Preview Reference Changes and click on "OK". AFter clicking  "OK" Preview Reference Changes diaglogue is displayed. Click on "Apply" and Monitor property will be added.
Sample screenshot

Now let us see the code.

C#
using System;
using System.Collections.Generic;
using System.Text;

namespace MyLib
{
    public class Computer
    {
        private int monitor;

        public int Monitor
        {
            get { return monitor; }
            set { monitor = value; }
        }
    }
}

One obvious question that should come to your mind should be why didnt we add the property using add property option. The reason behind this is, using the add property option will only add a non-implemented property without any field.
Now let us add some methods to our class called Boot,LoadOS and LoginUser by selecting the class,right click and Add --> Method. After adding all the methods our class will appear as follows :-

Sample screenshot

7. After adding the methods add the code will be

C#
using System;
using System.Collections.Generic;
using System.Text; 
namespace MyLib
{
    public class Computer
    {
        private int monitor; 
        public int Monitor
        {
            get { return monitor; }
            set { monitor = value; }
        } 
        public void LoginUser()
        {
            throw new System.NotImplementedException();
        } 
        public void Boot()
        {
            throw new System.NotImplementedException();
        } 
        public void LoadOS()
        {
            throw new System.NotImplementedException();
        }
    }
}

8. Let us now change the Inheritance Modifier of our "Computer" class to abstract as shown below :
Sample screenshot

9.After the Computer class is made abstract note that the name of the class is displayed in Italics and below class name "Abstract Class" is displayed.

10, The is an old design saying code to an interface not to a class so let us create an interface called IComputer for our Class Computer. With Class Designer we can do this easily, VS Class Designer provides us with a opion called "Extract Interface",

11. RightClick on the class , select eXtract interface option. Extract Interface dialogue will be displayed. The Interface name should be displayed as IComputer and all the public members of the class will be displayed. Let us select all the methods displayed as click on ok. This interface will be created in a new file called IComputer.cs as shown below.

Sample screenshot

Sample screenshot

Let us view the code

C#
using System;
namespace MyLib
{
    interface IComputer
    {
        void Boot();
        void LoadOS();
        void LoginUser();
        int Monitor { get; set; }
    }
}

12. If you note the interface added does not have a class view. In order to view all the methods for the interface select the interface, right click and select show interface.
Sample screenshot

13. Let us now add 2 more classes called Laptop and Tablet. These two classes will inherit from the abstract class Computer.
Let us inherit both of them from Computer, Go to Main Menu View ---> Toolbox and select inheritance.
Sample screenshot

14. Click on the Inheritance option and then first click on the derived class and then on the base class. i.e. first click on the Tablet class and then on Computer abstract base class.
Sample screenshot

Code of tablet and Laptop classes is as follows :-

C#
namespace MyLib
{
    public class Laptop : Computer
    {
    }
}
C#
namespace MyLib
{
    public class Tablet : Computer
    {
    }
}

15.You can view a lot of shapes in the toolbox for creating class, enum,interface,abstract class,struct,delegate. There is a shape which allows you to add comments.

16.The class designer provides options to view method signatures and thier names and types. Select the Class Computer and goto menu option called Change Members Format. The current selection should be Display Name. If we want to view member names and types we should select the 2 option and to view the complete method signature we should select option 3.

Sample screenshot

17. One last cool feature of class designer is that it allows you to export the entire diagram as an Image. Go to Class Diagram and select the option "Export Diagram as Image" as shown below.

Sample screenshot

Conculsion

We have seen how to work with Classes and interfaces today. During this entire exercise we did not type any code. All the classes and interfaces, their methods,properties,fields were added through class designer without touching the code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
Namratha Shah a.k.a. Nasha is from orginally from Bombay, India but currently residing NJ, USA. She has to her credit, a Bachelor’s Degree in Microbiology and Biotechnology and a Master's in Computer and Software Applications (1999-2001) from Somaiya College Bombay. She started her career with C and C++ and then moved on to Microsoft Technologies. She has over 7.5 years experience in software architecture, design and development. She is a Certified Scrum Master and a member of the CORE .NET Architecture team. She has been Awarded with Microsoft’s Prestigious Most Valuable Professional (MVP) twice consecutively in years 2005 and 2006 in Visual C#.NET for her outstanding contributions to the .NET community.

Comments and Discussions

 
GeneralMy vote of 5 Pin
version_2.023-May-11 18:22
version_2.023-May-11 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.