Saturday, 12 February 2011

Death to Object Test Bench in Visual Studio 2010

It seems support for the Object Test Bench has been removed from Visual Studio 2010 edition. The Object Test Bench was functionality (first introduced since the 2005 edition and also present in 2008 edition) that allowed graphical instantiation and invocation of class/methods. What made this so useful was that it avoided the need for creation of a driver class or direct instantiation and invocation in source code in other areas of the project. This made it good for testing purposes.

I was trying to use it one day and noticed it missing (which is a shame as i was used to using it in previous releases). Upon a search on the web to get to the bottom of what actually happened to it i stumbled upon a unfortunate list of RIP features of Visual Studio 2010 here. The Object Test Bench was included in a list of ommitted features. Apparently developers never really used it much and it was more a tool for academia purposes (such as its use in BlueJ), not actual commercial development, so the Microsoft Visual Studio development team decided to remove it for the 2010 edition based on feedback. It would appear most commercial developers tended to use the Immediate window instead and it is much better for real world testing.

It will be a much missed feature. Particularly because it brought back memories of BlueJ (the days i started off learning proper OOP at college). But as anything, times move on and new techniques move on with them.

Thursday, 16 December 2010

Back home and plans for the future

I spent the last 6 months travelling and seeing the world. Now that i am back in Ireland, I can properly focus my attention career wise and make it happen. I plan to become certified as a web developer and software developer in the .NET framework. I love programming and want to do it for a living. My degree in computer science reflects that passion. It however has not and does not guarantee me a job in this field. Nowadays there is a lot of people with degrees. Employers primarily need a way to distinguish one person from the other. Experience is one way in determining this. Another is becoming a Microsoft Certified Professional Developer (MCPD). A person with a MCPD will stand out more than someone who does not.

Today i got Visual Studio 2010 installed for the first time. I have been using Visual Studio 2008 previously, so it is good to finally get my hands on the 2010 edition. It has .NET framework 4.0 opposed to .NET 3.5 of Visual Studio 2008. This gives me an opportunity to get acquainted to the new features the update brings. I am looking forward to that.

I was taking a look on the Microsoft website here. I seen a few certifications for Visual Studio 2010 that eventually lead to credit towards a MCPD. There are four MCTS (Microsoft Certified Technology Specialist) I will have to do exam  70-516 and exam 70-513. Then depending on which MCPD certification (windows developer 4 or web developer 4) i will have to pick an additional MCTS and then i will be eligible for a MCPD exam. I will probably go for web developer 4 since going the web developer route makes more sense first anyway. I eventually plan to get both MCPD' s eventually.

Getting these exams will involve a lot of preparation and a lot of reading of books etc. I don' t mind though. This is the only way i am going to make my career path happen and i am prepared to put in the work necessary. These certifications will undoubtedly pave the way for me to land a job in this area.

Tuesday, 17 August 2010

LongDivision Web App update

I am in Sydney with a bit of time on my hands. I thought i would use this spare time to do something productive. It has been a while since i last wrote a blog on here so it is time for an update.

I decided to start working on my LongDivision web application again and add a few enhancements. Mainly decimal number support (the first version only supported whole numbers). A few days before i left for Australia i was working on adding these improvements. There was a few bugs before i left home. Although i have more less sorted them now (the major ones). The enhancements are listed below:

  • Works with decimal number inputs now (not just whole nos)
  • The output can be calculated in decimal as well to a max 5 decimal places.
  • Has the option to switch into remainder mode or none remainder (exact answer)
The remainder part can be activated via the use of a checkbox as can be seen below.

The input 12444.33 divided by 78.55 yields the following:


Bugs:
  • Minor drawing consistency problem with the red vertical lines. When divisor or dividend number input is extremely large, eg 18 digits or more long etc the vertical lines sometimes do not align consistently with the numbers on top. Some maths should fix it.
In addition to all this, the source code i have included is the solution which includes the project files for both the local application and the web application versions.

I will keep this blog posted on my progress or any future updates.

- Ronan

Friday, 4 June 2010

LongDivision ASP.NET Web-service application

Meanwhile i been reading "Beginning ASP.NET 3.5: in C# and VB" by Imir Spaanjaars in recent weeks in my quest to try and pick up web development. It seems a good read.

Here is my very first stab at writing a web service. It is a web service that utilises a class that i wrote for automating long division calculation. It is coded in C#. I felt such an application is a perfect way to introduce and familiarize myself to ASP.NET.


In school long division was common and useful back in the day for dividing very large numbers when a calculator was not available or allowed. However, it was always a pain to use and the calculations always took a while to work out, especially if the numbers were very large.

Basically we have a divisor and dividend. My application takes both these numbers as input, validates them and produces an automated end result showing the workings (just like it would be done manually by hand). The below is the input screen:


When the calculate button is pressed with the two input values in place, the result is generated and presented to the user (as a PNG image). Below is what the user will see.


I use the System.Drawing namespace to draw the output. This is drawn onto a bitmap and then converted to a PNG image suitable for view in a browser. The drawing of the output makes anything we are displaying more readable and presentable (especially any output data). Also, it enables us to draw the division lines and vertical lines etc.

I felt this application is quite useful and original in a sense because i have not seen any web services on the net at the current moment that automates LongDivision. The source code can be downloaded here if anyone is keen on seeing how it works.


Moving on with times ( ASP.NET )

For too long now i have been focusing my programming attention developing standalone applications. Nowadays it would appear most of the money to be made is in the web development segment. It has been this way for a very long time now. I suppose i can blame my lack of moving on the fact that when i first started learning to program it was at a time when standalone applications was all there was really, a time before web development (at least in an object oriented sense anyway) gained prominence. It was a habit more than anything. ASP.NET had not been invented. ASP (classical ASP) had merely just been invented or had not existed.


There is still a need for local application development in today' s society, but nowhere near on the mass scale that web development has brought about. Especially with ASP.NET. Nowadays there is a huge need for not just dynamic behaving web sites (they behave more like standalone apps) but web applications/services that can be invoked through them.


My main ambition is to move on to developing for profit and not just as a hobby. The best way to achieve this is without a doubt to move on into web development! The internet has really exploded in the last 10 years with the need for e-commerce, dynamic sites such as web shops and various booking systems that make money. Developers are needed to make them.


There is two ways to achieve this, via PHP (a scripting platform and more geared at open source) or ASP.NET (a true programming platform). I chose ASP.NET because in my opinion it is more superior. It is more recent, better for dealing with complex sites, is backed by a strong framework (.NET), a powerful IDE (integrated development environment), websites behave more like standalone apps as well and most importantly i can use C# (a language i have strong faith in its capabilities and develop standalone apps in anyway) to develop the sites in. This should not only make the transitioning more straightforward but give me access to an area where profit can be made since ASP.NET web development market is the most highly sought after at the minute.

Thursday, 29 April 2010

Serialization in C#, shortcomings, advantages and evolution (.NET 1.0 to 3.5)


One topic i like to get into about when it comes to C# is serialization. I am a strong believer in the power this has brought to solving programming problems in a very trivial manner. Today i am going to discuss about serialization issues in C#. Particularly multi dimensional arrays and some disadvantages/advantages of using various serialization classes. First of all i will give a run down on serialization in .NET using C#.

Serialization is a concept in programming (particularly .NET in this discussion anyway) that can be used to construct an already instantiated object/data structure into an output (
in the form of a stream of bytes which could be XML or binary). This output is said to be serialized and can be used to reconstruct that object into an identical object again with the same state etc. Reconstructing this output (serialized) data into an actual object instance again is called deserializing. This is particularly useful for web services and local applications that are network based. We can easily save data structures to disk or share them over a network and allow network applications to communicate with one another easily.

There is 3 main classes used for serialization in C#. They are SoapFormatter, BinaryFormatter and DataContractSerializer (.NET 3.5). There is also XmlSerializer but that is beyond the scope of this article and i am deliberatelly avoiding it because it is unuseful in the fact it cannot serialize private data members of a class. So anyway, the three classes have fundamental differences and advantages.

SoapFormatter is a serializer based on the Simple Object Access Protocol. This protocol was invented as a specification for storing and exchanging structured information in XML over networks via e.g. HTTP. It is common in web services today and since it uses XML and SOAP it is an open standard with active parsers in a lot of languages. This means cross platform support for it is good.

A brief simple example of serialization of a string using SoapFormatter:

string strMyName="Ronan", strNameDeserialized="";

MemoryStream nameStream = new MemoryStream();
SoapFormatter formatterName = new SoapFormatter();

// serialize our data structure into a stream of SOAP XML bytes

formatterName.Serialize(nameStream, strMyName);

nameStrm.Position = 0;

// deserialize the serialized output of bytes into a string again
strNameDeserialized=(string)formatterName.Deserialize(nameStream);

BinaryFormatter on the other hand is s serializer that is less open. The constructed output is in binary, which makes it less suitable for deploying on a network. This is because binary uses NULL bytes in it which would break anything null terminated that is string parsed. It is proprietary by Microsoft which means you will not be able to serialize/deserialize the object unless you are using a .NET language. However, it is still useful in the sense that the serialized object output is much more compact than SoapFormatter, even though the output is not as readable.

An example of serialization of a string using BinaryFormatter:

string strMyName="Ronan", strNameDeserialized="";

MemoryStream nameStream = new MemoryStream();
BinaryFormatter formatterName = new BinaryFormatter();

// serialize our data structure into a stream of binary bytes
formatterName.Serialize(nameStream, strMyName);

nameStrm.Position = 0;

// deserialize the serialized output of bytes into a string again
strNameDeserialized=(string)formatterName.Deserialize(nameStream);

To serialize instances of our own custom classes the class definition has to be prefixed with [Serializable] attribute for it to be serializable. Most inbuilt classes in .NET library have this set by default, so this is why we do not do it in our examples. [NoneSerialized()] can also be used on class members not to be serialized. Also sometimes this needs used on data members in a class that is being serialized because sometimes certain data member' s types are not serializable, other times we just dont want a certain data member' s state to be remembered so we dont serialize it). Information about all this can be found at the msdn site.


SoapFormatter generic support limitation?

All is good and well except for one main problem. Since .NET 2.0 generics were introduced into the language. This was a great feature, however the new language feature did not bring with it the needed updated SoapFormatter support to facilitate the serializiation of classes that were either generic or had generic members ! Microsoft to this day discontinued support for SoapFormatter. To see a demonstration of what i mean, either run the below code or place a breakpoint on the catch statement and run it. It should yield figure a.

List<string> strItemsList = new List<string>();

strItemsList.Add("FirstItem");
strItemsList.Add("SecondItem");

using (MemoryStream memStrm = new MemoryStream())
{

SoapFormatter formatterName = new SoapFormatter();

try
{
formatterName.Serialize(memStrm, strItemsList);
memStrm.Position = 0;
List strItems2List = (List<string>)formatterName.Deserialize(memStrm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Figure A.




I encountered this downfall to a painful disadvantage in one of my projects. Particularly the battleships application i wrote for college one year. In Visual studio 2005 there was no fix to this except to force yourself to not use generics for anything that would be serialized. Which could mean refactoring your code if you already had generic classes in place. This takes away the benefits of generics altogether !

New hope with DataContractSerializer (support for generic serialization)


However, there is light at the end of the tunnel. Since Visual Studio 2008 was released, Microsoft released .NET 3.5. Since work on SoapFormatter was discontinued (although still bundled with the software), there was a new replacement class included by the name of DataContractSerializer. This class, by comparison offered serialization of generics at last ! Although this took 3 years to come about, i thought i would go about and refactor an old college project i had done to see if it would fix the serialization of generics problem that had plagued my application. Upon doing so, i discovered that infact this class also had its flaws too.

No support for multi dimensional arrays ?


Central to the functioning of my battleships project design was multi dimensional arrays, also known as rectangular arrays in C#.
It turns out that with my playing around i found out to my dismay that DataContractSerializer did not support serialization of rectangular arrays. Below is sample code that declares a rectangular array to invoke such an outcome to demonstrate it:

int[,] myArray = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

using (MemoryStream memStrm = new MemoryStream())
{
DataContractSerializer ser = new DataContractSerializer(typeof(int[,]));

try
{
// serialize

ser.WriteObject(memStrm, myArray);

memStrm.Position = 0;
//deserialize
int[,] myArray2 = (int[,])ser.ReadObject(memStrm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

The actual exception being caught can be seen in figure b:

Figure B.



How to overcome this ?


I found this odd because SoapFormatter had previously serialized multi dimensional arrays no problem. It would appear that with one step forward gained in one area with DataContractSerializer, one step backward came in another area. Which is a shame really since i really needed multi dimensional arrays. At the time, .NET 3.5 was only new. DataContractSerializer was only new and no one knew much about it, nevermind it' s shortcomings ! For a year or two after, when the .NET 3.5 framework was more widely adopted and known about, i was randomly browsing around the net to see if anyone else had run into this shortcoming. There was a thing on a Microsoft Msdn forum that discusses the problem on August 2009, it can be found here. In it a Microsoft designer of the .NET framework acknowledges the shortcomings and discusses some workarounds for the limitation. It turns out that DataContractSerializer includes support for Jagged arrays and not Rectangular arrays. So a bit of code refactoring and changing to jagged arrays should fix this.

With a few minor differences and the fact we have to explicitly define the size of each second dimension of the jagged array seperately because jagged arrays are essentially arrays of arrays and each index of the first dimension can have a separatelly defined index count for the second dimension.

For example, for the above piece of sample code to actually work with dataContractSerializer it would be changed to:



int[][] myArray = new int[][] { new int[] { 2, 3, 4 }, new int[] { 5, 6, 7, 8, 9 } };

using (MemoryStream memStrm = new MemoryStream())
{
DataContractSerializer ser = new DataContractSerializer(typeof(int[][]));

try
{
// serialize
ser.WriteObject(memStrm, myArray);
memStrm.Position = 0;
//deserialize
int[][] myArray2 = (int[][])ser.ReadObject(memStrm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Alternatively, SoapFormatter will serialise rectangular arrays no problem at all as long as generics are not being used. Although in my oppinion generics are essential and it is too much of a price to drop them just to be able to serialise multi dimensional arrays. It is also too much of a price (assuming the project is large) to refactor, drop all multi dimensional arrays and change them to their jagged array equivalent. The link i included above about the forum includes code by a Microsoft engineer that shows how to convert multidimensional array into a jagged array on the fly ! This will evidently not be as fast as a solution purely based on jagged arrays as it incurs some overhead but it temporarily solves the problem i suppose and is well worth a read. For anyone implementing something that is going to be serialized using DataContractSerializer i would recommend making sure you are using jagged arrays and not multi dimensional arrays. It really will save a lot of hassle having to refactor it later.

BinaryFormatter unaffected

Also, do bare in mind that BinaryFormatter does also support generics and multi dimensional arrays. Although due to the limitation above and the fact it is not suited for network application or none Microsoft OS/ Languages, it really has limited application. Although it is still perfectly suitable for for example storing application configuration data to disk (so state or field data can be reloaded at a later time) on a windows machine. Although all serializers can be used for this and are not just restricted to network use !
A common trick i use it for is to store a file with the serialized data so i can pause my application, close the application and open it again at a later date to continue off from where i was.

Visual Studio 2010 is almost here and with it, it brings .NET framework 4.0. I have not had time to try it out yet or get a copy of it. I am looking forward to getting my hands on a copy though, getting comfortable with the new features and improvements (if any) that are made on serialization. When i do i may write another blog about it.

- Ronan