Convert string or int to enum

Enum’s a great, but you may be wondering how you can turn an integer or string value into the corresponding enum. For example you may have an api that’s being sent XML or JSON, and then you need to turn one of the values within that into an enum to set on an object in your code.

Well it’s very simple. If you have a string do this:

YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);

or if you have an int do this (you could also just do the first example with a ToString() on the end.

YourEnum foo = (YourEnum)yourInt;

HTML5 Series – Flexbox

If you’ve ever done any WPF, Silverlight, Windows Phone or Windows 8 development using xmal then you will appreciate how good it is to have controls like the StackPanel and Grid, and that HTML is rather lacking in its layout functionality. True you can produce most layouts but it just seems needlessely harder.

Thankfully HTML5 improves on this situation by introducing some new CSS properties.
The first is flexbox. When you define a div as a flexbox you can set its containing items to either space themselves out evenly, or you can set ratio values on each item to say how big it should be compared to the other items in the flexbox and they will stretch to fill the flebox.

Examples
In this example the flexbox is set to distributive which means the containing items will keep there size but distribute themselves evenly in the flexbox.

The key CSS is where the class example 1 sets the display to -ms-flexbox which makes example 1 div a flexbox container, and -ms-flex-pack: distribute, which set the flex mode to distribute the containing items evenly.

    <style type="text/css">
        .Example1 {
            display:-ms-flexbox;
            -ms-flex-pack:distribute;
            width:400px;
            border:solid 1px black;
        }
        .Example1 div:nth-child(1) {
            background-color: red;
            min-width:80px;
            min-height:80px;
        }
        .Example1 div:nth-child(2) {
            background-color: green;
            min-width:80px;
            min-height:80px;
        }
        .Example1 div:nth-child(3) {
            background-color: blue;
            min-width:80px;
            min-height:80px;
        }

    </style>
 <div class="Example1">
        <div></div>
        <div></div>
        <div></div>
    </div>

Flexbox-Distribute

In the next example the containing items have had ratios set so that the second item is twice the width of the first and the third remains at a fixed size.

The key CSS to look at here is on the child div’s styles where it says -ms-flex: 1 or -ms-flex: 2.

    
    <style type="text/css">
        .Example2 {
            display:-ms-flexbox;
            width:400px;
            border:solid 1px black;
        }
        .Example2 div {
            min-width: 80px;
            min-height: 80px;
        }
        .Example2 div:nth-child(1) {
            background-color: red;
            -ms-flex: 1 0 auto;
        }
        .Example2 div:nth-child(2) {
            background-color: green;
            -ms-flex: 2 0 auto;
        }
        .Example2 div:nth-child(3) {
            background-color: blue;
            min-width:80px;
        }
    </style>
    
    <div class="Example2">
        <div></div>
        <div></div>
        <div></div>
    </div>

Flexbox-Ratios

The third example shows how these techniques also work vertically as well as horizontally. See -ms-flex-direction:column on the containing div style.

    <style type="text/css">
        .Example3 {
            display:-ms-flexbox;
            -ms-flex-direction:column;
            width:400px;
            border:solid 1px black;
        }
        .Example3 div {
            min-width: 80px;
            min-height: 80px;
        }
        .Example3 div:nth-child(1) {
            background-color: red;
            -ms-flex: 1 0 auto;
        }
        .Example3 div:nth-child(2) {
            background-color: green;
            -ms-flex: 2 0 auto;
        }
        .Example3 div:nth-child(3) {
            background-color: blue;
            min-width:80px;
        }
    </style>
    <div class="Example3">
        <div></div>
        <div></div>
        <div></div>
    </div>

Flexbox-Verticle

Lastly in the fourth example I’ve shown how items can be set to wrap when they fill up a line with -ms-flex-wrap:wrap;

    <style type="text/css">
        .Example4 {
            display:-ms-flexbox;
            -ms-flex-wrap:wrap;
            width:400px;
            border:solid 1px black;
        }
        .Example4 div {
            min-width: 80px;
            min-height: 80px;
        }
        .Example4 div:nth-child(1) {
            background-color: red;
        }
        .Example4 div:nth-child(2) {
            background-color: green;
        }
        .Example4 div:nth-child(3) {
            background-color: blue;
        }
        .Example4 div:nth-child(4) {
            background-color: yellow;
        }
        .Example4 div:nth-child(5) {
            background-color: orange;
        }
        .Example4 div:nth-child(6) {
            background-color: violet;
        }
    </style>
    <div class="Example4">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>

Flexbox-Wrap

HTML5 Series – Forms

Forms haven’t really changed in functionality since they were first invented, but with HTML5 the browser is going to add a lot of the functionality you’ve previously been hand coding through JavaScript/jQuery.

HTML5 now supports things like setting a field to be required or adding some html text that disappears when you click in the field. Some of the new tags include:

  • required – Determines if the field must be filled in
  • placeholder – Text to be displayed before the user enters a value
  • autofocus – Default the curser to the field with this set
  • pattern – A regex expression to validate the field
  • min / max – Specify the min and max values that can be entered

For more visit theĀ HTML5 form attributes page on W3Schools

There are also new input types that will restrict the data that can be entered and the way the field is renderedĀ such as:

  • colour
  • date
  • datetime
  • email
  • month
  • number
  • range
  • search
  • tel
  • time
  • url
  • week

For more view is the HTML5 form input types page on W3Schools

HTML5 Series – Semantic Tags

Before HTML5 the HTML for your page’s layout was probably primarily made up of a lot of div tags with id’s a class’s to give them a visual structure and layout. It might look something like this…

<div id="header">My Site Name</div>
<div id="content">Bla bla bla site content......</div>
<div id="footer">Privacy policy, copywrite info etc</div>

This though has 2 problems. Firstly it’s not great for computers like search engines or screen readers to understand as everything is a div. There is extra code you can add to improve on this (have a look at Scheme.org). Secondly your code isn’t very, again because everything is a div.

HTML5 however introduces new semantic tags like <header>and
<footer> to make your data more structured. So the above example would become…

<header>My Site Name</header>
<article>Bla bla bla site content......</article>
<footer>Privacy policy, copywrite info etc</footer>

Other tags are as follows:

Tag Description
<article> For external content, like text from a news-article, blog, forum, or any other content from an external source
<aside> For content aside from the content it is placed in. The aside content should be related to the surrounding content
<details> For describing details about a document, or parts of a document
<summary> A caption, or summary, inside the details element
<figure> For grouping a section of stand-alone content, could be a video
<figcaption> The caption of the figure section
<footer> For a footer of a document or section, could include the name of the author, the date of the document, contact information, or copyright information
<header> For an introduction of a document or section, could include navigation
<hgroup> For a section of headings, using <h1> to <h6>, where the largest is the main heading of the section, and the others are sub-headings
<mark> For text that should be highlighted
<meter> For a measurement, used only if the maximum and minimum values are known
<nav> For a section of navigation
<progress> The state of a work in progress
<section> For a section in a document. Such as chapters, headers, footers, or any other sections of the document
<time> For defining a time or a date, or both