Public Virtual Auto-Properties and CodeRush

Saturday, February 11th, 2012

Since starting to make the move towards utilizing NHibernate in most of my projects I have been a little frustrated by the need to make properties virtual. This is by no means a criticism of NHibernate just the tooling, specifically Visual Studio 2010 and CodeRush. Whilst CodeRush has templates for everything the virtual keyword dosn’t seem to get a look in, fortunately it’s not hard to add support.

Thanks to Rory Becker’s article on Creating Virtual Methods I had everything I needed to create the template. Auto-properties are actually significantly less complex than methods as they are composed of little more than the type, property name and the { get; set; } block.

Templates are easy enough to create from the DevExpress Options Menu (DevExpress Menu -> Options) you jump to the templates page quickly with the search in the top left hand corner. I prefer to organize my custom templates into their own folder, then I can easily import and export these between machines via drop box.

1) Right click your preferred folder and select “New Template” inter “u?Type?” and press enter/OK.

2) Set the Context – I copied the context of my template from a?Type? and removed the InStruct directive, as virtual is not valid in structures, the Use: field should read something like this:

((InClass or InInterface) and OnEmptyLine) but not (InComment or InMethod or InPreprocessorDirective or InProperty or InString or VS2002 or VS2003 or VS2005)

It’s not critical you get this right, as it is probably going to work where you want it, if the above is wrong it may work where you don’t want it.

3) Enter the expansion

public virtual «?Get(Type)» «Caret»«Field(PropertyName)»«BlockAnchor» { get; set; }

This bit is essential to get right:

  • “public virtual” are string literals
  • the ?Get(Type) StringProvider is a macro that takes the ?Type? part of your template and emits the type of the property
  • Caret is the position of the Caret unsurprisingly
  • Field(PropertyName) gives you a place holder to enter the name of your property
  • BlockAnchor highlights the text between the Caret and BlockAnchor making the PropertyName field instantly replacable
  • { get; set; } is again a string literal

With that you are all set and can save and test out your new template in the code editor. Simply move to an empty line inside a class and type

us<space>

This should then expand to

public virtual string PropertyName { get; set; }

Changing code accessibility modifiers quickly

Tuesday, October 12th, 2010

I have two hints today both of them involving changing accessibility modifiers. The first is a feature of CodeRush that I accidentally discovered while testing out the Visual Studio 2010 Productivity Power Tools the second is a great new feature of Visual Studio 2010.

For those who don’t know what I mean by accessibility modifiers, those are the keywords you put before blocks of code that define how that code can be accessed, this is all enforced by the compiler giving you nice compiler error messages if you violate these rules. To provide an example the following auto-properties are all have the accessibility modifier “public”:

[Column] public string Name { get; set; }
[Column] public string Description { get; set; }
[Column] public decimal Price { get; set; }
[Column] public string Category { get; set; }

CodeRush gives you a handy way to change the User interface by clicking the icon to the left of the code block, you then get a set of actions you can perform upon the that block of code.

Code Rush Code Context Menu

As with most everything in CodeRush you can access this functionality from the keyboard anywhere within the scope of that code block by pressing Alt + Up or Alt + Down to cycle through the five possibilities:

  • public
  • internal
  • protected internal
  • protected
  • private

This means that if you need to change access modifiers, either to tighten up or relax the access points into your code you can do it by pressing a hand full of keys (between two and six to be exact), much faster than navigating to the top of the method highlighting and replacing the keyword.

My second hint is something that I have been using quite regularly, lets take the above example again:

[Column] public string Name { get; set; }
[Column] public string Description { get; set; }
[Column] public decimal Price { get; set; }
[Column] public string Category { get; set; }

I want to change all four fields to “be internal”, I could go in and change each one manually or better still use the above keyboard shortcuts (Alt + Down, Down, Alt + Down, etc.) the faster alternative would be to hold down the Alt key then select from the first character of the first “public” to the last character of the last “public” keyword.

While holding down Alt traditional selection behaviour is not followed and only the “public” keywords are selected on all four lines:

VS2010 Line Select

You can then type your new visibility modifier overwriting the selected components of all four lines in one fell swoop:

[Column] internal string Name { get; set; }
[Column] internal string Description { get; set; }
[Column] internal decimal Price { get; set; }
[Column] internal string Category { get; set; }

I think that is neat!

Using EVEMon Data with LINQPad

Saturday, July 31st, 2010

LINQPad is an awesome aide to .NET Developers. Written by Joseph Albahari of LinqBridge and the C# In a Nutshell series fame. LINQPad allows the developer to write, compile and run C# or VB.NET Expressions, Statements of Programs outside of Visual Studio.

Everything I am going to show you in this post can be done with Visual Studio simply by wrapping the text in a new console application, and adding references to the DLLs. However I strongly encourage you to download LINQPad and give it a go.

In the event you are using 64-bit windows and are still using LINQPad 2 you will need to download the x86 version of LINQPad 2, as all of the EVEMon assemblies are compiled for x86. If you have LINQPad 4 you don’t need to worry about.

Assuming you have EVEMon installed, the first step is to load the EVEMon.Common.dll assembly into LINQPad:

  1. Go ahead and fire up LINQPad ensure you have a new query window open
  2. Press F4 (Query -> Query Properties).
  3. Click the “Browse…” button at the bottom of the properties window.
  4. Navigate to the EVEMon install directory.
  5. Select “EVEMon.Common.dll”

While you have “Query Properties” open go to the “Additional Namespace Import” tab and add the following two lines:

EVEMon.Common.Data
EVEMon.Common

EVEMon.Common was designed to work as part of a long running process, namely the EVEMon application sitting in your system tray from when you turn your computer on until you turn it off, as such we need load the static data from the data files.

EveClient.Initialize();

Now we get to do some LINQ,

var allItems = from item in StaticItems.AllItems
	       where item.Family == ItemFamily.Ship
	       select new
	       {
	           item.Name,
	           item.Race,
	           CPU = item.Properties[DBConstants.CPUOutputPropertyID].Value.Value,
	           PG = item.Properties[DBConstants.PGOutputPropertyID].Value.Value,
	           item.Description
	       };

I could start to explain the above line by line, but there are lots of really good LINQ articles on the Internet, including one by Joseph Albahari. We will just say that the above pulls all of the ships out of EVEMon’s Items data file and selects the Name, Race, CPU, PowerGrid and Description property for each one.

Now we see my favorite aspect of LINQPad, the .Dump() extension method, simply running the following command:

allItems.Dump();

Will output the data we have just queried as a nice HTML Table:

That is all I have for you for now, I am working on a project that uses this data outside of EVEMon, keep an eye on Twitter where I will hopefully be providing a link for testing in the not too far distant future.

ASP.net 3.5 GridView RowCommand event fired twice

Thursday, April 1st, 2010

I am writing this up to hopefully save someone else time in the future, this particular problem took up six hours of my day yesterday causing quite a bit of frustration for me, as the developer, and the users of the application.

If you are searching for the solution scroll down to the bottom of the page where I will outline the solution I used to resolve the problem. It is also worth pointing out that this does appear to be fixed in .NET 4. Certainly I was able to consistently reproduce the problem with VS2008/.NET 3.5 on multiple different computers. However after converting the project to VS2010/.NET 5 I haven’t seen the issue.

Explanation of the problem

I wrote and maintain an application that publishes a list of courses and allows users to book onto these courses, what I have listed below is a simplified version of this application.

The administration console contains two lists:

  • Published Courses – courses visible to all employees.
  • Unpublished Courses - courses waiting to be published, only visible from the administration console.

Courses can be freely published (i.e. moved from Unpublished to Published) by clicking green tick. Courses that have not had any bookings made can be unpublished by clicking the red cross.

The cross and the tick are implemented as GridView ButtonFields:


<asp:ButtonField ButtonType="Image" CommandName="UnpublishCourse" 
    ImageUrl="~/images/unpublish.png" InsertVisible="False" Text="Unpublish" />

This application has been running for six months, the issue had not been observed up until yesterday. The user explained to me that when they were publishing courses they were always published in pairs, equally when unpublishing courses it was being done in pairs, concealingly unpublishing a course with bookings.

Investigating the problem

Initially I tried to reproduce this on my local machine, backed up and subsequently restored the database locally made sure I was running the same revision as the server and fired it up. Couldn’t reproduce the problem, no matter how fast I clicked it wouldn’t happen. Tried various permutations of code and database but could only reproduce on the server.

Refreshed the binaries on the server with the HEAD from subversion, problem was still happening most of the time. I confirmed that it wasn’t an issue with the stored procedures by running them manually through LinqPad.

I started putting debug statements at the entry points to the critical parts of the code, this yielded an interesting output on my development machine, each time the cross or the tick was clicked UnpublishedGridView_RowCommand was fired twice. This gave me something to search for, seems I am not the only one to have this problem, Microsoft tried to reproduce it in 2006 but couldn’t.

Solving the problem

As it turns out there are several ways of fixing the problem, several people have used timers to “debounce” the RowCommand event, assuming that the event is always going to be fired twice a session variable can be used to filter out the second event.

Because the event is only fired twice when ButtonType=”Image” not when ButtonType=”Link” you can set the text property to the HTML to render your image. This resulted in the code above becomming:


<asp:ButtonField ButtonType="Link" CommandName="UnpublishCourse" 
    InsertVisible="False" Text="<img src=images/unpublish.png />" />

This proved to be the simplest possible solution, Visual Studio 2008 throws a warning about ASP.net validation, but I can live with that as long as the application works. In addition to the simplicity of the solution it also continues to work in ASP.net 4 (which doesn’t exhibit the double event behaviour).

Lack of AutoEllipsis support in ToolStripSystemRenderer

Sunday, March 7th, 2010

AutoEllipsis is a property introduced to System.Windows.Forms.Label with .NET 3.0, which in the event of the text overflowing the rendering rectangle of the Label will trim the end and add a Ellipsis (“…”), if this does occur the ToolTip for the label will also be set to the full (untrimmed text).

Unfortunately this functionality is not available for ToolStripStatusLabel. To make things worse in the event the text overflows it disappears completely. This bug, oversight, feature or whatever you want to call it cause some confusion after the release of EVEMon 1.3.0.1912. Several people assumed the new more verbose status bar was broken, being empty and all.

We put together a kludge fix, which would set the text and if it overflowed try to guess the length with Graphics.MeasureString. This worked fairly well, it cause some flickering when resizing the window and would leave a small gap on the right hand side of the StatusStrip.

I knew there must be a better way, and seeing an article about the StringFormat class reminded me of the need to find it. Searching about a bit found me a post on Joel on Software, I refined the code a little and came up with this (which is basically identical to Tom’s solution):

public class AutoEllipsisToolStripRenderer : ToolStripSystemRenderer
{
  protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
  {
    ToolStripStatusLabel label = e.Item as ToolStripStatusLabel;
 
    if (label == null)
    {
      base.OnRenderItemText(e);
      return;
    }
 
    TextRenderer.DrawText(e.Graphics,
      label.Text,
      label.Font,
      e.TextRectangle,
      label.ForeColor,
      TextFormatFlags.EndEllipsis);
  }
}

You need to wire this code into your StatusStrip:

this.MainStatusStrip.Renderer = new AutoEllipsisToolStripRenderer();

To the ToolStripStatusLabel will also need it’s Spring property set to true, and if you want the text left aligned the TextAlign Property will need to be set to MiddleLeft.

If you want the ToolTip to work correctly the StatusStrip will need to have ShowItemToolTips set to work, and the ToolStripStatusLabel AutoToolTip set to true. It isn’t perfect as the ToolTip is displayed when the text is not truncated, but it is close enough for my purposes.

I am exploring WPF at the moment, I was glad to see the default behaviour of a StatusBar was to just stop rendering the text at the bounds of control, an ellipsis could be added with the TextTrimming and TextWraping properties:

            Some Text Goes Here, this text may be very long as demonstrated here. In the event we run out of space an ellipsis is used.

LINQPad Crash

Tuesday, March 2nd, 2010

Update: I never did “fix” this problem, installing .NET 4 then using LINQPad 4 seems to work well.

I found myself using LINQPad more often than creating console applications, so much so I dicided to make the small but worth while investment in the optional “Autocompletion” (Intelisense-like) component. The licence is great because I can have it installed on all three of my PCs without having to buy extra licences.

I was figuring out the limits of the Math.Pow function a few days ago on the laptop when the LINQPad upgrade message appeared, not sure what happened next because LINQPad crashed with the following exception.

System Specification:

  • Windows 7 Home Premium x64
  • .NET v2.0.50727 (+3.0 & 3.5)
  • .NET v4.0.20506
  • VisualStudio 2010 Beta1
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at LINQPad.Program.ProcessException(Exception ex)
   at LINQPad.Program.Start(String[] args)
   at LINQPad.ProgramStarter.Run(String[] args)
   at LINQPad.Loader.Main(String[] args)

If anyone has any theories as to how this can be fixed I would be very apprecitive if you could post in the comments.

So far I have tried:

  • Reinstalling from the latest (2.10.1) from the LINQPad website.
  • Restarted the computer.
  • Removing LINQPad through Add/Remove Programs.
  • Remove LINQPAD manually.
  • Rename %APPDATA%\LINQPad.
  • Looked for Native Images in C:\Windows\assembly – None there

It seems to me that LINQPad throws some exception, which it’s built in exception handler tries to handle then fails, this probably means that the above stack trace is probably not indicative of what is causing the problem. Not that I think it will make a difference but I am going to try upgrading to Visual Studio 2010 RC tomorrow then at least I wll be able to use LINQPad for .NET 4.

Preventing the PictureBox control from locking files

Sunday, February 28th, 2010

One of our more regular contributors to EVEMon posted on our forums showing that the application was incapable of updating cached files (specifically images), after a bit testing I discovered the following Exception was being thrown when trying to overwrite the file in question:

System.IO.IOException: The process cannot access the file 'path\filename' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at EVEMon.Common.FileHelper.OverwriteOrWarnTheUser(String srcFileName, String destFileName) in EVEMon.Common\FileHelper.cs:line 108
   at EVEMon.Common.FileHelper.OverwriteOrWarnTheUser(String destFileName, Func`2 writeContentFunc) in EVEMon.Common\FileHelper.cs:line 82
   at EVEMon.Common.Controls.CharacterPortrait.SavePortraitToCache(Image newImage) in EVEMon.Common\Controls\CharacterPortrait.cs:line 248

After a bit of searching around I discovered a post on StackOverflow identifying that System.Drawing.Bitmap(string filename) would lock the filename until the Bitmap was disposed of. The post presented a solution but no code, A bit of further searching confirmed my expectation that Image.FromFile(string filename) was subject to the same locking behaviour:

The file remains locked until the Image is disposed.

A bit more searching identified another post on StackOverflow which gave me the basic syntax and structure for the code I was going to need to implement this in EVEMon. The final code looks like this:

MemoryStream stream = new MemoryStream();
 
byte[] imageBytes = File.ReadAllBytes(cacheFileName);
stream.Write(imageBytes, 0, imageBytes.Length);
stream.Position = 0;
 
var image = Image.FromStream(stream);
return image;

It appears that GDI+ will lock any image that is loaded into a control in WinForms and WPF, several comments on StackOverflow and byte.com suggested that even disposing of the control and the FileStream was not a reliable way of being able to write to the file so the above method is seems to be be the best solution all round.

Controling Code Outlining with the Keyboard

Tuesday, November 3rd, 2009

Code outlining is a feature of supported by Visual Studio and many other editors, MSDN has some good documentation for VS2005, VS2008 and VS2010. If I were asked to explain this as briefly as possible, I would probably say:

Code Outlining is the logical partitioning of code in such a way that the user interface, or editor, is able to selectively hide the body of the content (such as a class, struct, enum or method) whilst leaving the signature or some identifying comment visible.

You can see this in action in Visual Studio 2008 with the following Screenshot:

CodeOutliningVS2008

I accidentally turned off Code Outlining today by hitting some keyboard shortcut that I didn’t know how to reverse, this lead me to discover several useful keyboard shortcuts for managing the display of your code from the keyboard.

As it turns out I managed to hit Ctrl-M followed by Ctrl-P (or just P in fact) which maps to Edit.StopOutlining, by default it seems that the Visual C# 2005 mapping scheme doesn’t provide a shortcut to enable Automatic Outlining so instead you can access the command through Edit Menu -> Outlining -> Start Automatic Outlining.

Enabled again, I get to play with code outlining from the keyboard:

  • To toggle (collapse an expanded block or expand a collapsed block) the closest outlined element use Ctrl-M followed by Ctrl-M.
  • To toggle everything use Ctrl-M followed by Ctrl-L (I find little use for this)
  • To collapse to definitions use Ctrl-M followed by Ctrl-O

The last one is the most useful when used in conjunction with Regions as after colapsing to definitions you will get something similar to this:

ColapseToDefinitionsVS2008

You might have noticed in the first screenshot that CodeRush Xpress adds a coloured line between the beginning and end of blocks of code, this is a nice feature if you have long blocks of code, which of course you shouldn’t have.

CodeRushXpressBlockLines

There we go, an errant key stroke can lead to learning and blogging, who would have thought it?

Adjusting Selections in CodeRush Xpress

Friday, October 16th, 2009

I found this function totally accidentally when I knocked my mouse into a key when something interesting popped up on Twitter. I happened to have a variable selected like so:

Variable Selected in Visual Studio

The key that I knocked was the Number Pad “+” key, and it expanded the selection like this:

VariableSelectedPlus1

As any self respecting Systems Administrator would do I wondered if doing exactly the same thing repeatedly would have equal or compound effects.

VariableSelectedAgainAgain

Strangely enough it worked, the selection will continue to expand selecting increasingly larger sections of code. The reverse works as well if you press the Number Pad “-” key the selection will be reduced.

WhatHappened

As I was looking for documentation on this function; I found that you could use the CamelCase select function to start selections off using Ctrl+Shift+Left (or Ctrl+Shift+Right) this will take your selection from the current cursor position to the next upper case character to the left (or right). This can be coupled with the Number Pad “+” to expand your selection from a variable to an expression, to a line then a block. Neat!

Each of these discoveries was announced with a handy little popup in the bottom right hand corner of the viewport telling me what happened, and if relevant what function was suppressed.

I am going to make a concerted effort to spend my lunch break watching some more videos on DevExpress’ training site. I got 10 minutes today and watched what appeared to be footage from a launch event or a conference which was interesting but didn’t really teach me anything specifically.

Find Files in CodeRush Xpress

Monday, October 12th, 2009

As I mentioned in my previous entry I have started using DevExpress’s CodeRush Xpress. It is a free cut down version of CodeRush that I heard about in an episode of .NET Rocks. I have wanted add something to my Visual Studio development experience and I can’t justify the cost of either Resharper or CodeRush at the moment. There has been quite a bit of discussion about Resharper vs. CodeRush and in my experience most people I have spoken to love one and hate the other.

I hope to be able to write a series of posts about some of the features found in CodeRush Xpress which I hope will clarify their purpose and use in my mind and maybe help someone find the function they are looking for.

QuickFileNavigationAbouFirst off “Quick File Navigation”, this is a search function for locating a file by file name, I am finding it increasingly useful when looking through patches as it allows me to very quickly jump to a file name. Particularly as I am trying to move my projects to a one class per file so if I can remember the class name I can find the code file very quickly.

The “Quick File Navigation” dialog is accessible through the keyboard short cut Ctrl+Alt+F. Typing will filter the list box by the name of the file matching anywhere in the filename including the extension.

An additional feature for those who like me use CamelCase in their file names; if you enter your filter terms in capital letters it will search for capitalized words, in order within file names. Thus entering “AW” into the search box will also bring up the AboutWindow.cs in the above solution.

Combining the above with the Ctrl-G keyboard shortcut in Visual Studio 2008 to go to a specific line we can do the following to go to line 162 in ShipLoadoutSelectWindow.cs:

Ctrl+Alt+F  →  S,L,S,W  →  Ctrl+G →  1,6,2

DevExpress have a great training video on their tv.devexpress.com sub site.

Ctrl+Alt+F → S,L,S,W → Ctrl+G → 1,6,2