# Tuesday, October 06, 2009
Do you like to have a neat and transparent code base? One of the foundations of this in .NET are regions (#region ...). A smart usage of regions significantly increase readability of the code base, but it can also diminished it if it is misused (typically example would be heavy region usage in methods).

I have find out the following class region structure really neat:

But there is the problem. Maintenance of this structure. But there exists a great tool to this instead of you - Regionerate.

Regionerate is an automatic layout enforcement tool for the C# programming language as it also stated on the tool's homepage. This tool is Add-in to Visual studio. It is really simple to use, just hit a keyboard shortcut "Ctrl+R" and the tool will do the rest or right click on the code edit panel:


Code became instantly Regionerated. But question arise. In what way code became regionerated? What are the names of the regions which members go into which region? Well there this utility really shines. You can totally customize it. I will blog about this in another post.

Regions in the upper picture were created with Regionerate tool. Did you notice the number of members in the end of the name of each region? Nice.

Out of the box region structure really did no fit me, so I customize it. You can find the customization file at the end of this blog post.

You can import customization file easily into the tool, just click "Regionerate this" and then "Settings" and just select the proper XML customization file. Tool have some problems with updating or changing customization file, sometimes change is not instantaneous.

It would be nice if you post your favorite region structure in comments.
 
CustomCodeLayout.xml (2.11 KB)
posted on Tuesday, October 06, 2009 12:55:30 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
# Sunday, September 13, 2009
On meta.stackoverflow.com Joel announced a need for countdown application for the coming DevDays, as I am planing to attend one, I have decided to make one. Maybe I will watch my counter at the conference. ;)

It looks like:



Source code: PeterStegnarDevDaysCountdown.zip (198.51 KB)

I have used WPF, .NET 3.5. The speciality of this solution is custom Stackoverflow digits. :)

Short manual:
-you can find exe in "PeterStegnarDevDaysCountdown\WPF_StackoverflowCountDown\bin\Debug"
-start the app with the following parameters, for example: 0900 "DevDays begins in %s"
-just hit an "Esc" key to exit

Core functionalitiy is, of course, countdown:

            if (second != 0)
            {
                second--;
            }
            else
            {
                second = 59;
                if (minute != 0)
                {
                    minute--;
                }
                else
                {
                    minute = 59;
                    if (hour != 0)
                    {
                        hour--;
                    }
                    else
                    {
                        throw new ArgumentNullException("No valid time entry!");
                    }
                }
            }


What do you think?

posted on Sunday, September 13, 2009 12:00:59 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
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