Posts Tagged ‘Visual Studio’

Fixing Visual Studio 2012 defaults

Written by Cornelius J. van Dyk on . Posted in Annoyances, How Do I...

One of the biggest pet peeves I experienced when switching to Visual Studio 2012 was the fact that the default command buttons were changed.  In going for a cleaner look, many of the main command buttons most developers use most frequently, was removed from the toolbar interface and buried in the menu structure instead.

Personally, the buttons I find myself using most frequently are the Navigate Backwards and Navigate Forwards (image ), Comment Selected Lines and Uncomment Selected Lines (image ) and lastly Increase Line Indent and Decrease Line Indent (image ).

The Navigate functions are still there, but the Comment and Indent functions are buried deep within the Edit/Advanced menu.  While it’s true that the Comment functions do have hot keys attached to them, I hardly consider Ctrl+K,Ctrl+C for a single click to be useful to most developers save the few that know the hotkeys by heart.

As for the Indent commands, they don’t have any hotkeys and you’d have to click through Edit/Advanced/Indent to get the action.  Though small annoyances, these can be addressed by simply adding the commands back to the command bar.  Here’s how to do just that:

  1. Click Tools on the menu bar.
  2. image  
  3. On the popup menu, click “Customize”.
  4. After the Customize dialog window opens, click over to the “Commands” tab.
  5. Select the “Toolbar” radio button.
  6. In the dropdown to the right of the Toolbar radio button, select the “Standard” toolbar to work with.
  7. The toolbar’s controls will be previewed in the bottom left of the dialog window.
  8. To the right of the preview, click the “Add Command” button.
  9. image  
  10. In the Add Command dialog window that opens up, select the “Edit” category to the left.
  11. On the right, scroll down through the commands and locate the “Line Indent” command.
  12. Select the command and click the “OK” button.
  13. image
  14. The command will be added to the top of the Controls preview.
  15. image  
  16. Repeat steps 8 through 12 for the “Line Unindent”, “Selection Comment” and “Selection Uncomment” commands as well.  The interface doesn’t allow multi-select, though that would be nice touch in a future update.
  17. Once you have all the commands, use the “Move Up” and “Move Down” buttons to the right to arrange the commands in the order you wish them to appear on the toolbar.
  18. image  
  19. When you’re happy with the arrangement, click the “Close” button.  Your new toolbar should now reflect your favorite buttons again. 🙂
  20. image


Cheers
C




image

SPFarm.Local is null in SharePoint 2010 causing C# Console App to throw a “Object reference not set to an instance of an object” error

Written by Cornelius J. van Dyk on . Posted in Annoyances

This one is one of the more annoying errors you’ll encounter in SharePoint development. The simple reason for that is because the error you’re receiving is bogus. I mean, sure it’s a “valid” error since the SPFarm.Local is actually null and you’re trying to reference it, but it doesn’t actually lead you anywhere. As a SharePoint developer/admin/architect, you will often be faced with the need to iterate content in the farm. Every good SharePoint developer knows that it all starts with the simple statement: SPWebService svc = SPFarm.Local.Services.GetValue<SPWebService>(“”); This should NEVER fail, unless you don’t actually have a SharePoint farm on your DEV box, but naturally you do, right? 🙂 So then why would you ever see the below from your debugger window? image[18] The problem stems from the architectural nature of SharePoint. SharePoint as you know, has been purely x64 based since the 2010 version. Add to that the fact that most utilities we write in the C# world to pull information from SharePoint, are Console apps, and you have the ingredients for our problem which is caused by Windows attempting to run a x86 based app under a x64 based architecture. Now where this should normally work, it doesn’t in this case. The real problem is that no architectural error is thrown. The code simply returns null and does not work. It can lead to many frustrating hours of debugging. Since console apps are by their very nature x86 based, it’s very easy to make the mistake of compiling your console app to the x86 architecture. If we go to the project properties for this console app, we’d find the following: image_thumb[14] The “Platform target” is the setting we’re interested in. By default Visual Studio 2010 will set this value to x86 for console apps. It’s easy to forget to reset this value when quickly knocking out a console app. Here’s how this should be set: image[38] Once we make this simple change and press F5, we’ll be able to step past the problematic line of code and continue on our merry way. Hopefully this article will save someone some time and frustration.

Cheers
C




image

How do I – Programmatically select a field inside InfoPath XML using C#

Written by Cornelius J. van Dyk on . Posted in How Do I...

As we know, InfoPath forms are actually XML documents behind the scenes… a special kind of XML, but XML nonetheless. As such we should be able to figure out how to change the value of a field pretty easily, right? You’d think so, but unless you live inside XML every day (so few of us do these days) it might now be as easy as you’d have thought. After getting the InfoPath document loaded to an XmlDocument object, one would expect a statement thus:

doc.SelectSingleNode("//my:Family", nsm).InnerText = "New Value";
to have the desired effect. Alas, it’s not that simple. It never is, is it? Smile Instead, this is what you’re greeted with: image_thumb_38C22EAC What we need to do is ensure that your XmlNamespaceManager is properly assigned with the content from the given InfoPath form in order to translate the “my:” part of our xPath statement. If we take a look at the InfoPath document’s OuterXml value we see this: Untitled-1 With so many xmlns attributes in the InfoPath document, we need some automation to prepare our XmlNamespaceManager so we can interact with the data properly. Adding this little code snippet to our existing code, should resolve that nasty error:
foreach (XmlAttribute att in doc.DocumentElement.Attributes)
{
  if (att.Prefix == "xmlns")
  {
    nsm.AddNamespace(att.LocalName, att.Value);
  }
}
Remember to create the XmlNamespaceManager object with a reference to the base XmlDocument’s NameTable thus:
nsm = new XmlNamespaceManager(doc.NameTable);
Throw all that together and we are now able to manipulate the InfoPath field programmatically to our heart’s content.

Cheers
C




image

How do I – Use a bitmask enum in C#

Written by Cornelius J. van Dyk on . Posted in How Do I...

In today’s world where processor memory is plentiful, many developers will often times just use a List<bool> for tracking a series of on/off values.  Though the compiler can optimize this code somewhat, the CPU cycles spent managing the List<> structure when super fast bitwise operations could have been done, isn’t really the best way to go.  So when one of my mentees asked about checking values in an enum bitmask, I thought it best to blog the answer for the benefit of others as well.

Let’s assume I’m trying to track the on/off values of say a permissions mask.  I would define my values using the Flags attribute on an enum thus:

[Flags]
public enum Permissions
{
    All = 4,
    Update = 2,
    Read = 1,
    None = 0
}

If I now proceed to define a variable of our enum, I can set some of it’s bit values using the the bitwise AND operator thus:

Permissions perm = Permissions.Read & Permissions.Update;

If I now check for a value in the mask that is NOT turned on, it is done with the bitwise OR operator thus:

if ((perm & Permissions.All) != 0)
{
    //it never gets here
}

The same applies for values that ARE turned on in the mask thus:

if ((perm & Permissions.Update) != 0)
{
    //it gets here
}

In addition, I can turn on more flag altering future check for them thus:

perm = perm & Permissions.All;
if ((perm & Permissions.All) != 0)
{
    //now it gets here
}

The major advantage to using the bitmask enum with bitwise operators is speed.  These operations are done by the processor literally just flipping a single bit.

Happy coding!

 



Cheers
C




image