# Thursday, 16 August 2012

Windows 8 RTM and Visual Studio 2012 now on MSDN

For those of us with an MSDN subscription: Windows 8 RTM and Visual Studio 2012 are both available for download. Go to https://msdn.microsoft.com/en-us/subscriptions to get your copy.

 

image

#    Comments [2] |
# Monday, 30 April 2012

String literals and ‘==’ versus Equals(…)

I had a little fun today playing with ‘==’ vs. the Equals(…) method. I knew that somewhere in .NET 2.0 (I believe) there had been some improvements in String.Empty versus “”, but I didn’t quite realize that this affected all string literals.

In the code below you can see how value types and reference types may behave differently when using ‘==’ versus the Equals method.
My surprise came when I discovered that line 39 and 41 both return TRUE. Apparently the string literals in an assembly get put in a master list of string constants and then reused where the same string literal gets used. Nice!
The way to force the reference comparison of the string against object to actually be different is to delay the creation of the string until runtime, like in line 52.

   1:  static void Main(string[] args)
   2:  {
   3:   
   4:      // value types
   5:   
   6:      int x = 10;
   7:   
   8:      int y = 10;
   9:   
  10:      Console.WriteLine( x == y );  // compare by value -> return true
  11:   
  12:      Console.WriteLine( x.Equals( y ) ); // compare by value -> return true
  13:   
  14:      Console.WriteLine( object.ReferenceEquals( x, y ) );  // compare by reference -> return false
  15:   
  16:   
  17:      Console.ReadLine();
  18:   
  19:      // Reference types
  20:   
  21:      StringBuilder s1 = new StringBuilder( "yes" );
  22:   
  23:      StringBuilder s2 = new StringBuilder( "yes" );
  24:   
  25:      Console.WriteLine( s1 == s2 ); // compare by reference -> return false
  26:   
  27:      Console.WriteLine( s1.Equals( s2 ) ); // compare by value -> return true
  28:   
  29:      Console.WriteLine( object.ReferenceEquals( s1, s2 ) ); // compare by reference -> return false
  30:   
  31:      Console.ReadLine();
  32:   
  33:      // object and string
  34:   
  35:      object o1 = GetName();
  36:   
  37:      string o2 = "Mark";
  38:   
  39:      Console.WriteLine( o1 == o2 );  // compare by reference -> return true
  40:   
  41:      Console.WriteLine( o1.Equals( o2 ) ); // compare by value -> return true
  42:   
  43:      Console.WriteLine( object.ReferenceEquals( o1, o2 ) ); // compare by reference -> return true
  44:   
  45:      Console.ReadLine();
  46:   
  47:   
  48:      // object and string
  49:   
  50:      object ss1 = "Mark";
  51:   
  52:      string ss2 = "Markx".Substring( 0, 4 );
  53:   
  54:      Console.WriteLine( ss1 == ss2 );  // compare by reference -> return false
  55:   
  56:      Console.WriteLine( ss1.Equals( ss2 ) ); // compare by value -> return true
  57:   
  58:      Console.WriteLine( object.ReferenceEquals( ss1, ss2 ) ); // compare by reference -> return false
  59:   
  60:      Console.ReadLine();
  61:  }
  62:   
  63:  static object GetName()
  64:  {
  65:      return "Mark";
  66:  }
#    Comments [4] |
# Wednesday, 29 February 2012

Windows 8 Consumer Preview

ISO images with the Windows 8 Consumer Preview are now available for download. Go to http://windows.microsoft.com/en-US/windows-8/iso to get the bits.

#    Comments [3] |

Microsoft Visual Studio 11 Ultimate Beta

Microsoft Visual Studio 11 Ultimate Beta has just become available for download. Go to http://www.microsoft.com/download/en/details.aspx?id=28975 to download the bits.

#    Comments [0] |
# Tuesday, 24 January 2012

Type-Safe Include extension method for Entity Framework

If you’re interested in the Include method in a LINQ to Entities queries to be type-safe, then Joe Ferner has the answer for you in this post: Type-Safe Entity Framework Include: http://www.nearinfinity.com/blogs/joe_ferner/type-safe_entity_framework_inc.html

#    Comments [0] |
# Tuesday, 17 January 2012

Generating an index in the database using Entity Framework Code First

If you’re using Entity Framework Code First, then you may want to create an index on some of your table. The way to do this is to call “Database.ExecuteSqlCommand” in the “Seed” method of your database initializer. The sample below shows how it is done (thanks to Rolf for pointing me in the right direction):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
 
namespace CodeFirstPlayground
{
  class Program
  {
    static void Main( string[] args )
    {
      Database.SetInitializer<CodeFirstSampleModel>( new CodeFirstSampleDbInitializer() );
 
      using ( var model = new CodeFirstSampleModel() )
      {
        var query = from c in model.Customers
                    where c.Name != null
                    select c;
 
        foreach ( var item in query )
        {
          Console.WriteLine( item.Name );
        }
      }
 
      Console.ReadLine();
 
    }
  }
 
  public class CodeFirstSampleModel : DbContext
  {
    public CodeFirstSampleModel()
      : base( "CodeFirstSampleDB" )
    {
      Customers = this.Set<Customer>();
    }
 
    public DbSet<Customer> Customers { get; set; }
 
  }
 
  public class CodeFirstSampleDbInitializer : DropCreateDatabaseAlways<CodeFirstSampleModel>
  {
    protected override void Seed( CodeFirstSampleModel context )
    {
      context.Database.ExecuteSqlCommand( "CREATE INDEX IX_Customer_Name ON Customers (Name) " );
      
      Customer c = new Customer()
      {
        Name = "Mark",
        LastOrder = DateTime.Now
      };
 
      context.Customers.Add( c );
 
      base.Seed( context );
    }
  }
 
 
  public class Customer
  {
    [Key]
    public int Id { get; set; }
    [Required]
    [MaxLength( 50, ErrorMessage = "Customer name must be 50 characters or less." )]
    public string Name { get; set; }
    
    public DateTime? LastOrder { get; set; }
  }
}
#    Comments [3] |
# Wednesday, 04 January 2012

SharePoint UG Maine

Just a quick service announcement:

Winxnet, in Portland, has started hosting the first Maine based SharePoint User Group.

For more information visit: www.winxnet.com/spugme

#    Comments [3] |
# Monday, 02 January 2012

Microsoft Most Valuable Professional 2012

mvp-logoI’m honored to have been awarded the Microsoft Most Valuable Professional 2012 Award for contributions in the Visual C# technical communities. I’m honored to be counted amongst the 228 people world wide to receive this award.

“At Microsoft, we believe that technical communities enhance people’s lives and the industry’s success because independent experts, like you, help others extract greater value from products and technologies through the free and objective exchange of knowledge. As a Microsoft MVP, you are part of a highly select group of experts that represent technology’s best and brightest who share a deep commitment to community and a willingness to help others.”
    - Heather Kostes

Thank you Microsoft and thank you Heather! I’m looking forward to organizing more events again this year for the Maine Developer Network and hanging out with the Bangor Area .NET Developers and I’m looking forward to Maine Code Camp #3 and also to meeting my fellow MVPs and product groups in Seattle at the MVP Summit 2012.

- Mark

#    Comments [0] |
# Monday, 26 December 2011

How to discover what font was used

Sometimes you’re working on a website and you get some images with text in them, but no one remembers what fonts was used in the image. No fear! There is a website call “What The Font” that will take your picture and tell you what font was used: http://new.myfonts.com/WhatTheFont/

#    Comments [0] |
# Monday, 12 December 2011

Silverlight 5 release

Silverlight 5 got released this weekend and can be downloaded here: http://www.silverlight.net/downloads.

Summary of the features

(from the Silverlight 5 download package)

Improved media support
  • Low Latency Audio Playback
  • Variable Speed Playback
  • H/W Decode of H.264 media
  • DRM Key Rotation/LiveTV Playback
  • Application-Restricted Media
Improved Text support
  • Text Tracking & Leading
  • Linked Text Containers
  • OpenType and Pixel Snapped Text
  • Postscript vector printing
  • Performance improvements for Block Layout Engine.
Building next-generation business applications
  • PivotViewer
  • ClickCount
  • Listbox/ComboBox type-ahead text searching
  • Ancestor RelativeSource Binding
  • Implicit DataTemplates
  • DataContextChanged event
  • Added PropertyChanged to the UpdateSourceTrigger enum
  • Save File and Open File Dialog
  • Databinding Debugging
  • Custom Markup Extensions
  • Binding on Style Setters
Silverlight 5 performance improvements
  • Parser Performance Improvements
  • Network Latency Improvements
  • H/W accelerated rendering in IE9 windowless mode
  • Multicore JIT
  • 64-bit browser support
Graphics improvements
  • Improved Graphics stack
  • 3D
"Trusted Application" model
  • Multiple window support
  • Full-Trust in-browser
  • In-browser HTML support
  • Unrestricted File System Access
  • P/Invoke support
Tools improvements
  • Visual Studio Team Test support
#    Comments [3] |
# Monday, 14 November 2011

Augusta Developer Event, November 16th 2011

The next Maine Developer Network meeting will be in Augusta on Wednesday November 16th, starting at 9:00am finishing at 12:00 pm.
It is going to be a unique event about Windows Azure: "Playing Games in the Cloud" and will be hosted by Jim O'Neil, Microsoft Developer Evangelist. It will be more of a workshop than just presentation, so bring your laptop and make sure it is prepped to for the class.
For more info see: www.maine-devnet.org

#    Comments [3] |
# Tuesday, 01 November 2011

Streaming XML using LINQ to XML (continued)

Richard Blewett reminded me that the XmlReader.ReadSubtree method makes it even easier to use LINQ to XML with a streaming approach. The code sample below will load nodes from an arbitrary XML files and yield them to the caller as they’re read from file:

static IEnumerable<XElement> Load(string filename, string elementName)
{
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.IgnoreWhitespace = true;
    using (XmlReader reader = XmlReader.Create(filename, settings))
    {
        while (reader.ReadToFollowing(elementName))
        {
            // build element from subtree
            XElement element = XElement.Load(reader.ReadSubtree());
            yield return element;
        }
    }
}
#    Comments [0] |
# Monday, 31 October 2011

Streaming XML

I just rediscovered this blog post about reading and parsing XML as it becomes available in conjunction with LINQ to XML: http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx

It’s a little dated, but still relevant Smile.

#    Comments [3] |

What’s new in .NET Framework 4.5

Just came across this great picture of what’s new in .NET Framework 4.5 (click for larger version):

WhatsNewNET45-en

#    Comments [4] |
# Friday, 21 October 2011

Custom color in reports : convert color to Hex

I was implementing a client report (RDLC) using the Microsoft Report Viewer control and I wanted to set the background color of a table field based on value from my object source. At first I used the Color.ToKnownColor() method, but discovered that this does not work for all colors. I needed to convert to Hex. Here is the little extension method I used:

public static class ColorExtensions
{
 
    #region -- Data Members --
    static char[] hexDigits = {
     '0', '1', '2', '3', '4', '5', '6', '7',
     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    #endregion
 
 
    /// <summary>
    /// Convert a .NET Color to a hex string.
    /// </summary>
    /// <returns>ex: "#FFFFFF", "#554ECE"</returns>
    public static string ToHexString( this Color color )
    {
        byte[] bytes = new byte[3];
        bytes[0] = color.R;
        bytes[1] = color.G;
        bytes[2] = color.B;
        char[] chars = new char[bytes.Length * 2];
        for ( int i = 0; i < bytes.Length; i++ )
        {
            int b = bytes[i];
            chars[i * 2] = hexDigits[b >> 4];
            chars[i * 2 + 1] = hexDigits[b & 0xF];
        }
        return "#" + new string( chars );
    }
 
}
#    Comments [3] |
# Wednesday, 05 October 2011

Visual Studio 2010: Debugging a x86 WCF service on a x64 machine

I just ran into an issue where I have a WCF service that depends on a .NET assembly that is compiled specifically for x86. In order to use that assembly I need to compile the service as a x86 service. No problem, but now when I want to test or add a service reference to this WCF service I ran into the problem that WcfSvcHost and WcfTestClient both will run a x64 because I’m running Windows 7 x64.

How to solve this? I found the answer in the forums and adapted the answer for my specific problem:

1.Copy WcfSvcHost.exe and WcfTestClient.exe from C:\program files (x86)\Microsoft Visual Studio 10.0\Common7\IDE to a local directory. Keep a backup copy of this file, of course.
2.Start a Visual Studio 2010 Command Prompt (one of the links from the start menu -> Visaul Studio 2010)
3."cd" to the directory where your copy of WcfSvcHost is located.
4.Execute the command "corflags /32BIT+ /FORCE WcfSvcHost.exe"
5.Copy the files back to where you found it.
 
Now your WcfSvcHost and WcfTestClient will be running in 32 bit mode.

#    Comments [3] |
# Friday, 02 September 2011

Omnext @ New York CloudExpo 2011: “If it ain’t Dutch, it ain’t much”

Omnext was part of a Dutch trade delegation visiting the New York CloudExpo 2011.
Here is a link to a great article by Theo Loth (coordinator EuroCloud Nederland) about the event: omnext - cloudexpo 2011.pdf (in Dutch).

If your looking to find out more about Omnext and how Source2VALUE can help improve the ROI of your software development investments, then visit www.omnext.net or talk to Jaco and Andre at the 2011 European Outsourcing Summit in Madrid, Spain.

#    Comments [3] |
# Friday, 26 August 2011

SDN Conferences 2011

For those who will be in the Netherlands in October: the SDN is organizing it’s annual 2-day conference on October 3rd and 4th, 2011. A preview of the schedule is now online and registration has just opened: http://www.sdn.nl/SDN/SDNEvent/SDNConferences2011/tabid/162/Default.aspx

#    Comments [0] |
# Monday, 01 August 2011

Maine Code Camp #2 - Video Impression

A (short) video impression of Maine Code Camp #2:

#    Comments [2] |
# Monday, 11 July 2011

LINQ: Convert list to a dictionary of lists

Suppose I have a list of time cards from multiple employees, but I want to group them into dictionary, based on the Social Security Number (SSN) of the employee. Here is an example of how to convert a list of items into a dictionary of lists:

Dictionary<string, List<TimeCardInfo>> dict;
 
dict = ( from timecard in listOfTimeCards
         group timecard by timecard.SSN ).ToDictionary( x => x.Key, x => x.ToList() );
#    Comments [3] |