Tutorials & Guides

May 07 2012

A guide to creating class libraries in c#: The Caesar Cipher part four


So we can use our dll file in another project? Indeed. Start up a new console project (I loaded up another instance of visual studio) and call it CaesarTest. Now the important bit is to reference your newly created library. To do this, complete the following steps: Right click on references in the solution explorer and choose “Add reference” Click on browse Locate your dll file (Should be located in CaesarLib\bin\Debug\) and click ok So we can use it now? Yup, using our library is as simple as importing the namespace. Have a look at this simple program I put together to test the library This code produces the output: Is this secure? NO. The caesar cipher shown here is only used to demonstrate a very simple use case for creating a class library. It is easily broken and should NOT be used for any security purpose whatsoever. Summary So what did we learn about? We learnt what a class library is and why it is useful We learnt about the basic caesar cipher We learnt how to implement the caesar cipher as a class library And finally, we learnt how to bring an external library into a new project Thank you for following this tutorial, I hope it helped you in some way If anybody has any questions/feedback, please do not hesitate to contact me (You can use the contact buttons at the top of this page). Download The project files are available to download. Just use the button below.

Read more

May 07 2012

A guide to creating class libraries in c#: The Caesar Cipher part three


Finally it is code time The idea with a class library is that you define a bunch of public types, which are then available for use from any program that references the dll. We will call our class the “CaesarCipher” and it will have two simple methods, Encrypt(string plaintext) and Decrypt(string ciphertext). Have a look at the codelisting: Lets break it down…first the constructor Its pretty simple going. We initialise the _shiftValue variable, but we modulus by 26 to give us a number in the range of 0..25 so that we can easily apply it to the alphabet (If you didn’t know, the English alphabet has 26 letters ) Then we use String.Substring() to get the first part of our alphabet by grabbing the last bunch of characters (i.e. for a shift of 4, we would get WXYZ). Then we use Substring again to chop off these last few letters (with a shift of 4, we would end up with ABCDEFGHIJKLMNOPQRSTUV). The encrypt and decrypt methods are very similar. One thing to note is that we are converting all letters to upper case (so they are compatible with our alphabets) and ignoring all characters that are not letters. (indexOf returns -1 if the character is not found). The only difference between the two methods is that we swap the arrays around in the indexOf and Append calls. Just “swap the alphabets” to convert it to a decryption. So thats it? Yup. Build it and go to the output folder and you should find your DLL! As for how to use it? Well… that can only come in part 4

Read more

Apr 30 2012

“One program, Four ways” – A beginner’s guide to abstraction and interfaces in C# (Part four)


This is going to be about interfaces isn’t it? Heck yes! Interfaces are awesome. Lets take a look at an emerging pattern If you have a look at the string processing methods from part 2 and the string processing classes from part 3, you will notice something. All of the classes are about one method, which takes in a string and it gives back a string, and if you think about it, they kind of do the same job, but with a different output. I.E. THEY ALL PROCESS STRINGS! and from the outside, don’t really appear to be doing anything different. So why should we treat them differently? Answer: We shouldn’t! Let us unite all of our string processors under an interface Have a look at the following interface: So we have this interface, which is called IStringProcessor. It is time to bring all of our different string processing classes together, and give them one signature that works for all of them: Implementing the interface Implementing this interface in each of our 3 string handling classes will require a tiny bit of modification. Have a look at what I have done: And there we have it. All I have done is changed the signature of the method, and derived from the interface. That looks great, but what good does it do? Good question. By implementing the interface on each of our classes, we are giving the compiler a guarantee, and that guarantee goes like this “All of our classes that implement this interface will have a string processString(string input) method on them” This is huge, because we can now store objects using variables that are typed to IStringProcessor. This means that we don’t need to make our code dependant on any of our concrete classes because we can just make it dependant on the interface instead. Take a look at this new class I have added: Basically all this class does is store a list of stringProcessors and colors. And we can invoke the HandleString method and give it an input and when we do, it will go over each processor and display the result from each one. Notice how there is no reference to any of our string processing classes in this code. The only thing that this class cares about is that we are using IStringProcessors which is a type that all of our processors can be thought of as. This class gives us the ability to add a processor, using the AddProcessor method, but it calls for an IStringProcessor. How can we give it an IStringProcessor? You can’t instantiate interfaces…duh Indeed not. What it wants us to do is give it an object that fulfils IStringProcessor and we have 3 such classes. Check out the new program class: Now the fruits of our labour are beginning to ripen! Look at how much easier main is now. Sure it is still a bit of a middle-man, but the code is much clearer. Notice how on lines 17,18 and 19 the AddProcessor method happily accepts our concrete implementations of the IStringProcessor interface. The big advantage about that is we could have as many classes as we want that fulfil the IStringProcessor interface and the StringHandler class will automatically be compatible with them, with no modification. This means that we can continue expanding in a clean, none destructive way. A task for the reader I advise you to copy out this code and try this for yourself. Why not have a go at making your own string processor class. Here is how to do it: Make a new class, and implement the IStringProcessor interface Write the ProcessString method in your new class, so it fulfils the interface use the AddProcessor method of the handler object to add your processor to it, just like on lines 17, 18 and 19 Hang on a second buddy…You can’t combine processors in this version! Okay, you got me. You can’t combine processors with this version, but if you are smart about your design, you will see that there is a way to combine the processors that is way more elegant than what we were doing before. We simply write a new type of string processor, I call it the StringProcessorChain. Behold! : Using this type of string processor, we can chain multiple processors together and when we call ProcessString, it will go through each processor, and update the input with each one, and then return back the final result. Look at the new main method which makes use of this: The beauty of the interface in this case, is that even our StringProcessChain itself is an IStringProcessor and can thus be used with the StringHandler! Even though it does a more advanced form of string processing, combining a whole bunch of processors, it still fulfils the interface and therefore it can be treated just like a StringReverser or a StringVowelRemover. Summary Thank you for reading through this tutorial. I do hope you found time to follow my examples and have a bit of a play with them to see what results you can get. If I was you, I would try and do something cool. Imagine if you had a type of string processor that encrypted the string (:OOO HAX!). I think that would be pretty cool. Knowing how and when to refactor code and when to use interfaces is a huge part of object oriented design. I must stress though that it is not something that you will learn overnight and that it will take practice. Try and think of scenarios where the design would be bad, and then try and work out what you can do to make it better. Don’t be afraid to throw big chunks of code away if you think of a better way of doing it, it takes time to develop the intuition to solve and eventually avoid these problems. If you have any feedback on my tutorial, please do not hesitate to contact me! Kind regards, Andrew Thompson 2012 Download the project files The project files are available for download. They were produced in Microsoft Visual Studio 2010 Professional.

Read more

Apr 29 2012

Testing syntax highlighting plugin :)


So I have decided to maybe write some programming tutorials/guides on this website. This post is just to test the use of a syntax highlighting shortcode that I have just installed. Fun!

Read more

Apr 25 2012

Steak and ale pie


So today I thought I would try and make a steak and ale pie. I used casserole steak which is a fairly cheap cut with quite a bit of sinew but not too much fat. I cooked this with rosemary, thyme, bay leaf and red onions and flour. I added about 450ml of Fullers Champion Ale and 750 ml of beef stock (I used 2 Knorr rich beef stock pots for this). After about an hour I added some pearl barley and cooked for an hour or so more. I made the pastry using flour with butter and suet. When the beef was cooked I added some mustard and worcestershire sauce. I made two pies from it, one has been eaten and one is for later. Its pretty much stolen from Jamie Oliver, but it was epic! BRB..eating

Read more

Apr 04 2012

Chocolate Chip Cookies


Today I decided to try and make some epic chocolate chip cookies. I followed this recipe and used the food processor to do it.   The pictures were taken on my dodgy phone camera, I need an SD card for my camera   I do quite a lot of cooking/baking and I am always looking for ways to cut the price of ingredients down without compromising on the final taste as much as possible. To this end I substituted the butter and the chocolate in this recipe with the following items:   Tesco Baking Fat Tesco Milk Chocolate Cake Covering Using these 2 things allow me to save quite a bit of money and I haven’t heard any complaints so far and as you can tell from the photographs, the recipe given has quite a high yield and I expect these to last for approximately 17 seconds.

Read more