# Wednesday, August 26, 2009
How to get the file extension from string file name?

Well brute force approach would be something like that:

string fileExtension = args.FileName.Split('.').GetValue(args.FileName.Split('.').Length - 1).ToString();

Pretty ugly, in spite of one liner code.

Well it turns out that the framework has support for this functionality:

string fileExtension = System.IO.Path.GetExtension(fileName);

Much better, right?

It is good to remember that .NET Framework contain a lot of functionality that you would not think about at first place. That's why is good to check every time is something smells that it could be in .NET Framework. You may find it.

posted on Wednesday, August 26, 2009 4:43:41 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
# Tuesday, August 25, 2009

Roy Osherove is giving an hands-on TDD Masterclass in the UK, September 21-25. Roy is author of "The Art of Unit Testing" (http://www.artofunittesting.com/), a leading tdd & unit testing book; he maintains a blog at http://iserializable.com (which amoung other things has critiqued tests written by Microsoft for asp.net MVC - check out the testreviews category) and has recently been on the Scott Hanselman podcast (http://bit.ly/psgYO) where he educated Scott on best practices in Unit Testing techniques. For a further insight into Roy's style, be sure to also check out Roy's talk at the recent Norwegian Developer's Conference (http://bit.ly/NuJVa). 

Full Details here: http://bbits.co.uk/tddmasterclass

bbits are holding a raffle for a free ticket for the event. To be eligible to win the ticket (worth £2395!) you MUST paste this text, including all links, into your blog and email Ian@bbits.co.uk with the url to the blog entry.  The draw will be made on September 1st and the winner informed by email and on bbits.co.uk/blog

posted on Tuesday, August 25, 2009 8:34:12 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
# Tuesday, August 11, 2009
A jQuery library quickly emerging really popular - and for a reason. I think it is one of the best JavaScript frameworks. You can do fancy stuff really fast and clean.

You can start a Getting Started with jQuery. On the jQuery homepage you can find a lot of nice tutorials. If you have not try jQuery, please do. You will be amazed.

Bassically what jQuery brings to web delopement is that it separates HTML and behaviour. Similarly what CSS brought: separation of presentation and HTML.

There are a great tool named Firebug, which really helps with web development. This tool surely demand a separate post. You can inspect a web page, play with CSS on the fly, debug, use a console ... Here it comes a jQuery. You can write a jQuery instruction right into the console, and it will be applied on the fly. Amazing. All you have to do is to "jQuerify" a web page.

Another great thing to know when you work with jQuery is what the selector will actually select. You can try a SelectorGadget tool which shows the most appropriate selector for the selected element(s). This tool is so great also because all you have to do to run it, just click a "link" when you are on the desired page to be check. Great!

More on Firebug and jQuery in the great video:

posted on Tuesday, August 11, 2009 7:20:20 AM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
# Saturday, August 08, 2009
I have implemented a very simple validation site. You just put an XML and XSD to it and it tells you if your XML document is valid. Simple.

Just vist XML Validation site and try your XML against XSD. Please notice that it is version 0.01 so I presume you know what that means. :)

When and if it will be updated is depended on the planet polarization.


Related:
http://blog.stegnar.com/2009/06/03/SimpleXMLValidationWithNET35.aspx

posted on Saturday, August 08, 2009 9:30:32 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
# Wednesday, July 08, 2009
A very nice thinking came by me from Rockford Lhotka. It is about future of OS. I quite agree with him. Soon we will have just thin client and everything will be in cloud.

http://www.lhotka.net/weblog/OSEvolution.aspx

Just think how many "cloud" things are you already using? A lot. Today is totally possible to have installed just a browser in you machine, and you can do (almost) everything.

posted on Wednesday, July 08, 2009 8:53:17 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
# Friday, June 12, 2009
I am looking for the clean, elegant and smart solution to remove namespaces from all XML elements. How would function to do that look like?

I have used StackOverflow. If you do not know this site, I can describe it as a really smart Q&A site, dedicated for programmers.

So I was searching for really great solution for removement of namespaces in XML. Based on one programmer answer I have created a solution which I really likes. You can clearly see, when you have to deal with tree data structure (which XML for sure is) recursion is simply the best approach to do anything with it:
private static XElement RemoveAllNamespaces(XElement xmlDocument)
{
    if (!xmlDocument.HasElements)
    {
        XElement xElement = new XElement(xmlDocument.Name.LocalName);
        xElement.Value = xmlDocument.Value;
        return xElement;
    }
    return new XElement(xmlDocument.Name.LocalName, 
xmlDocument.Elements().Select(el => RemoveAllNamespaces(el))); }
Do you likes? I love it. :)

I was totally convinced that exists some more smart .NET framework function, with which I could help. But I was wrong. The best .NET can do is something like this:
XmlDocument doc = new XmlDocument()
doc.Load(ms);
doc.DocumentElement.Attributes.RemoveAll();
All this function do, is to remove all attributes on root element only ... You could repeat this on every node with help of XPath, but since those selected nodes are immutable, this is little help.

posted on Friday, June 12, 2009 9:12:44 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
# Friday, June 05, 2009
Usually the explanation of object reference or pointer goes it is an address which reference something. Well in high level overview we can simplify this to that explanation, but today we well look the difference between the object reference and pointer and we well see why reference in not nearly the same thing as pointer.

Address
First we will look what the address is. Every data cell in memory have address, as every person have home address, basically that simple it is. Address just happen to be a property of some specific memory implementation, we could just have other way to access it.

Reference
Reference is really simple programming concept. It just reference to something. You have NO control over it. You can just reference and dereference it.

Pointer
Pointer is an abstract programming concept, which points somewhere and supports arithmetic operations. Strictly looking, pointer "per se" have no information, it just know where can find it. And you have control over it (arithmetic operations). What is connection between pointer and address? Well Pointer is not an address. Pointer is just implemented with address, because it just perfectly suits to what Pointer needs. To summarize, Pointer can do everything what can Reference plus more.

To better understanding I have made some simple diagrams, how looks those two concept in .NET:



posted on Friday, June 05, 2009 1:29:52 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
# Wednesday, June 03, 2009
I know, .NET 3.5 framework will soon became "old" framework, because .NET 4.0 is due to release. Anyway it is still the latest, and today we will look how to validate good old XML document with a given XML schema.

It the old days a XML validation took quite some code. For better understanding what I mean, look an XML validation article on CodeProject.

Now just look at this beauty:
        public bool CheckXmlAgainstGivenSchema(XmlHolder xmlHolder)
        {
            XDocument xmlDocument = XDocument.Parse(xmlHolder.XmlDocument);

            XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
            xmlSchemaSet.Add("myNamespace", XmlReader.Create(new StringReader(xmlHolder.XmlSchema)));

            bool isValid = true;
            xmlDocument.Validate(xmlSchemaSet, (obj, eventArgs) => { isValid = false; });

            return isValid;
        }
It is interesting that this small code snippet represent a lot of .NET 3.5 technology (LINQ to XML, Extension methods and Lamda expressions). This just show us how useful are those "new" concepts.

First we use XML manipulation class "XDocument", which allow us a really nice and smooth XML manipulation, compare to old XmlDocument. XDocument class have nice feature called "Validate", which turns out that this is just we have needed. :)

Validate method in an framework extension method and all it need is XML Schema, as you would expect of course and an event handler, which handle the situation if XML validation is failed.

We pass XML Schema as "XmlSchemaSet", this type is some kind of collection of the schemas. Validator just looks for a proper schema based on proper namespace and XML document elements. A passing delegate is nicely solved with lambda expression. Instead of it we could declare event handler and pass just a method reference (delegate).

posted on Wednesday, June 03, 2009 6:05:08 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
# Tuesday, June 02, 2009
I have added a Twitter counter for all you Twetties out there. If you would like to be informed about new blog posts, you will find there announcements, among other.

If you wish to be even more up to date, you can subscribe to the RSS feed. I have created one on the Feedburner is spite my blog already provide one (with feedburner you get nice counter and more control over feeds). RSS feed is a nice approach of reading blogs, since computer do the checking work for new content insted of reader.

UPDATE: Oh yeah, my home site, have been completely renewed over weekend. You can check it out. Currently is more or less boring, because it contains almost no content, really. But I have plans. :) I will not revel them completely, you can already see now I will have more "open" blog (not just about software development) on it. When it will something interesting happen, I will post it. Then I (will) have picture gallery, same applies here as for personal blog. And more (I hope). Stay tuned. :)

posted on Tuesday, June 02, 2009 9:54:15 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
Have you ever asked yourself, why C# string type works somehow different than other reference types?
  • First, when you declare them, you don't have to use a reserved word, which is used for reference type initialization - "new". You can also easily "overwrite"* them.
  • Second, when it is passed as a parameter to method, it does not reflect possible changes of this parameter inside a method (as reference types would normally do).
So what this animal really is? To explain string behaviour in deep, you have to know all about:
- immutability
- value type vs. reference types
- parameter passing
- C# reserved word "implicit"
- ...

All these interesting topic I will cover in the following days.

*As straight forwards as it may seems, we will see this is not really true.

posted on Tuesday, June 02, 2009 1:09:08 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback