Scott Nicol
posted by Scott Nicol on November 15, 2013
Find me on: Scott Nicol on LinkedIn

CRM Development: The Inner Workings II

Impossible chemistry experiment

So, after reading the first CRM Development blog you now know how incredibly useful Methods and the concept of Encapsulation are. But how exactly do Methods work? What links the calling code to the executing code? Well, I’ll show you…

In the last blog, we discussed what the following code does:


(If you don’t know or can’t remember what this does, check out my original CRM Development blog)

Now then, we know that this code calls a method but in order to explain how, there’s a few things you need to know:

1. There are several ways of separating the main execution of code from the form design (if it is a forms application), the main three are:

  • Adding a new class to the code file (C#)
  • Adding a new C# file to the project
  • Adding a new dll (Dynamic Link Library) to the project (ideal for CRM usage – one dll can contain a huge amount of new functions for your CRM system)

2. Namespaces are containers for code chunks. Namespaces will contain Classes which contain the code itself. Think of this as folders like you see on your computer. Namespaces can span multiple code files or assemblies using the partial keyword.

3. A namespace can use the classes from another namespaces by using a direct reference (ie. <namespace>.<class>.<object>😉 or with a using statement (ie. Using <namespace> , then <class>.<object> anywhere in the C# file.)

Given this information, can you guess how our code works?
If you guessed that our code is split into another C# file, which is linked with a using statement, congratulations! You’ve won £1000! (…Not really, I’m just kidding.)

Here’s a simplified version of the form’s code showing only the relevant pieces:


Line 11 specifies that this C# file can use classes and objects from the namespace nValidatorMethods.

Line 13 declares a namespace (CPEmailValidator) which we will be working in.

Line 15 declares a class that we will be working in. More specifically, public means that the class can be accessed from anywhere in the same assembly, partial means that this section of code is not the entire Form1 class, and Form1 : Form means that this class is called Form1 and inherits objects and properties from the Form class.

Now I can show you the simplified code from the second C# file:


This is very similar to the structure of the first C# file. At the start we have specified the other namespace, meaning that both of these namespaces can now use each other’s objects and classes. We then declare the namespace we are working in (nValidatorMethods) and the class we are working in (Methods).

Note: the reason FindFile() is underlined in red is because the method is expecting to be set to a string by the child code, which I have deleted for simplicity.

Let’s take a step back to see what’s going on here then. This is what happens step by step when you click the Find File button:

1. You click the button.

2. The program publishes an event saying that the FindFile button has been clicked.

3. Our code (private void btnSelectFile_Click(…)) is subscribed to this event, and therefore knows the event has occurred.

4. As the corresponding event has occurred the event subscription executes the code it is responsible for (txtFile.Text = Methods.FindFile()).

5. The program looks globally (across any assemblies and namespaces it has access to) for a Methods class and finds it in the nValidatorMethods namespace.

6. The program orders the FindFile() Method in the Methods class to execute its child code.

7. FindFile() executes, and the code inside sets the return value of the method to be some text (for example, “CollierPickard.csv”.

8. Once FindFile() has finished executing, control goes back to the calling code (txtFile.Text = Methods.FindFile()).

9. The Text property of the txtFile field is set to the value of the FindFile() Method (which we know has been set to “CollierPickard.csv”.

10. Execution ends.

I hope this has given you a better understanding of the code structure behind this method of using custom CRM functions (via dll’s). The idea is very much to encapsulate the functions, so all the CRM system itself does it say “Go execute this complex function in Collier Pickard’s dll”. Which means that CRM administrators can still understand what is happening when they look at what the CRM system is doing.
If you have any questions, feel free to leave a comment below!

Download the Business Owners Guide

  • Leave a Reply