Saturday 21 January 2012

Writing Object Orientated VB.net Code for Civil3D (Part 2)

Over the last 12 months I have come to the conclusion that in going from VBA to VB.net you need to start employing some software architecture and Object Orientated Programming (OOP) techniques to progress your coding projects past small routines to the next level with larger numbers of classes and greater complexity.
So over the the last few months instead of writing more code I have been investigating how to write better code. To clarify my thoughts I typically write things down so plan to do this as a number of blog postings, being self taught of course the way I think about thinks my not be strictly correct or just plain wrong. So feel free to post a constructive comments explaining things more correctly, or differently.
Now having investigated the subject I bit I have come to the conclusion that there are two ways to write code either from the bottom up or the top down.
Bottom Up MethodThis is amateur method that I have been using for a while, I have an immediate problem, know now to open the new VB.net IDE and type some traditional VB language, so can piece together some sort of working code routine.
Top Down MethodThis (i guess more professional) method works by mapping out how your code will work using Unified Modelling Language (UML) Case and Class diagrams. You could do this before starting any code and depending on what UML package you use you can generate the outline of your code automatically.
So before we can start Refractoring any code we need to understand what a Class is and what UML Case and Class diagrams are to plan our attack.
What is a Class?
From the MSDN website
“A class is simply a template of a type of object; think of it as a blueprint that describes the object. Just as a single blueprint (aka Class) can be used to build multiple buildings (aka objects), a class can be used to create multiple copies of an object.”
So what does this mean in plain English and how do we write I class in VB.net? There are a number of videos on the internet explaining classes and how they are written but these two are quite good.

VB.NET Working With Classes: OOP, Object creation and structures (1)VB.NET Working With Classes: OOP, Object creation and structures (2)

After watching the videos hopefully you have an understand what a basic class is and the power of VB.Net intellisense, its icons and drop downs in helping you write code.
One thing I have struggled with is remembering what all the little icons mean. To refresh myself I have pasted together below some screen shots off the internet to refer too.
intellisense Icons 2
What are UML Case and Class Diagrams?UML Diagrams are basically a standardised way to document the structure of your code. I have found a couple of introductory video’s on UML using the program Software Ideas Modeller, here and here by Mike Lively, he also has created a series of UML Reference Cards.
If you work through the example given by Mike you can generate the outline of the VB.net code automatically by going to the Tools menu Generate Source Code.

Below is a screen capture of the vb.net code generated.
ohjnrwts

Notes:
  1. Importing code to automatically generate class diagrams with Software Ideas Modeller currently does not work with .net version 4 or a Debug build just a Release build.
  2. NClass is another UML modeller that has less options but lets drawing diagrams with the standard vb.net icons for methods and properties.
Reference BooksVisual Basic .NET class design handbook
Head first object-oriented analysis and design

1 comment:

  1. Also check out Extensions and inheritance, very useful tools when programming. Extensions especially if you use the same project for multiple code projects.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...