November 2007 - Posts

Don't "p" on your XAttributes..

If you programmatically add attributes with namespaces to an XElement, when the XElement is written out it will give each attribute a namespace prefix and then define a xmlns for that prefix.  The way XElement does this is first it examines the namespaces already active and also those defined in the element for a match. If it doesn't find a match, it then creates a new prefix based on the count of the current namespaces in scope, e.g p1, p2, p3,....pn.

Now sadly, although it did first scan for namespaces, it appears it doesn't scan for prefixes in the current element *after* the current attribute. It does scan for previous declared prefixes, and will use one if the namespace matches. If the namespace doesn't match but a prefix clashes with the proposed prefix, the prefix is added a 0, e.g p10, p20, ... pn0. If that one also clashes then the number will be increased e.g p11, p21, ... pn1. And so forth.

The problem is it doesn't check for clashes inside the element it arbitrarily adds a namespace to. This code causes an exception due to the same attribute being named twice.

 

   Dim books = New XElement("books")

   books.Add(New XAttribute("{abc}attrib1", "one"))

   books.Add(New XAttribute(XNamespace.Xmlns.GetName("p1"), "zzz"))

 

The moral of the story is don't use "pn" for your namespace prefixes. (where n is any integer). You end up potentially "p"ing on your code ;)

Posted by bill | with no comments
Filed under: , , , ,

Nice recovery...

I was working with Visual Studio and a few other applications, and put the computer to sleep while I took a break. Shortly after I resumed the computer did a hard restart. I presume it is something to did with Vista and the power management, but the nice thing was Visual Studio actually recovered the temporary project for me :)
Posted by bill | 1 comment(s)
Filed under: ,

XML namespace prefix 'xmlns' is not defined

When working with VB9 you may get this cryptic error :
XML namespace prefix 'xmlns' is not defined

If you are using only default namespaces you can fix this by including a definition for the xmlns as an Import statement or as an xmlns attribute on the XML literal.  But if you are using prefixed namespaces imported at file level, then you must add an imports at project level via the project properties reference tab.

You have two choices:

  • Import System.Xml.Linq for the entire project, or
  • Import a namespace for the entire project

If you decide to import a namespace it can be any namespace at all.  I named one _do_not_use.  Problem is it will show up in XML axis properties in intellisense, but with a name like _do_not_use, that isn't really a big problem. Of course you could add one that you might want to use.

 

imports

 

You may prefer to simply import System.Xml.Linq, but I thought I'd at least throw all the alternatives out there for you to decide what suits you best.

hopefully this is another bug that will make it into 2008 SP1 ;)

Posted by bill | 8 comment(s)
Filed under: , , , ,

Cleaning up your XML literal namespaces

If you use XML literals in your code, adding one to another:

Dim e1 = <a:books></a:books>
dim e2 = <a:book></a:book>
e1.Add(e2)

You will have the xmlns declaration repeated in each of the elements, when really it is only needed once per the document or outer element. The problem is caused by VB adding a xmlns declaration as an attribute to the root element. It can get a bit more complex if you have duplicate namespace declarations with different prefixes.  So I decided to write a CleanUpNS extension, that keeps the xml written clean by removing un-necessary namespace declarations. To use it, simply add a call to CleanUpNS to the end of your literals, e.g:

Dim e1 = <a:books></a:books>.CleanUpNS
dim e2 = <a:book></a:book>.CleanUpNS
e1.Add(e2)

 

   <Runtime.CompilerServices.Extension()> _

 Function CleanUpNS(ByVal el As XElement) As XElement

      Dim current = el.LastAttribute

      Do While current IsNot Nothing

         Dim temp = current.PreviousAttribute

         If current.IsNamespaceDeclaration AndAlso el.Name.NamespaceName = current.Value Then

            current.Remove()

         End If

         current = temp

      Loop

      Return el

   End Function

 

I go into more details about how this works and how the XML is stored and emitted in my January On VB article in Visual Studio Magazine

Posted by bill | 11 comment(s)
Filed under: , , , , ,

VB samples in VS 2008 SDK ?

Beth posted about the VS 2008 SDK saying it had many VB samples.  When I installed it I didn't see any in the DSL tools, so I thought there weren't any at all. turns out there are some VB samples, but no DSL samples as well as others completely missing. This is what is there:

 

 

VS 2005 SDK

VS 2008 SDK

 

C#

VB

C#

VB

Categories

9

5

12

6

Samples

48

32

58

31

Projects

137

79

168

72

Files

1636

804

2296

731

 

 graph

Note:
The values for VS 2005 are for V4 after the extra VB samples are downloaded and installed. These didn't ship till only recently; prior to that there were only about 35 .vb files in total in the VS SDK.
File numbers and project numbers are based on search for .vb, .cs , .vbproj and .csproj files.

Posted by bill | with no comments
Filed under: ,

This month's Microsoft FU awards...

Despite VB being the *most popular* language on .NET, many teams over at Microsoft like to give VB the finger.  But before I start on this month's FU awards, a big anti-fu award (aka kudos) goes to Don Box who said :
"I'd be an idiot not to walk in their shoes as much as possible"

Sadly, other teams at Microsoft still don't get that.  So onto the FU list of shame for this month:

  • First mention just has to go to the XNA team.  Their continual bigotry is outstanding
     
  • The Windows Workflow Foundation Hosting Quickstart Sample comes in at second place, delivering only C# templates.  Well it's not like anyone who writes in VB would want templates for that now is it ?
     
  • A contender for equal first place is the Windows Media Centre SDK team who sends out a very big FU to all you wanting to code in VB.  They must figure media centre is too advanced for VB folk
     
  • The Health SDK team only gets a minor mention this month as they release new downloads but still haven't included VB samples even after being requested to.  Eric won the previous month's FU arrogance award.
     
  • Arguably the number one spot has to go to the Visual Studio SDK team.  They've released the 2008 SDK again with no VB samples for Controls, Debugger, DSL tools, Project or Team System Test.  Like 2005 we'll probably have to wait 2 or 3 years till version 4 is released before they even think about releasing the VB samples as a separate download. Way to go team !!
      

So to this month's top five FU award winners, congratulations !   I salute you with my middle finger.

Posted by bill | 7 comment(s)
Filed under: ,

What a nasty compiler bug !!

Kathleen has posted about a real nasty bug in the VB 9 (VB 2008) compiler. And I mean nasty !!

The compiler won't warn you, won't give an error of any sort.. it will just omit lines of code from your application !  Yep, it will compile as if nothing is wrong, yet it will remove your code !!  If you're lucky, your test scenarios will pick up on this, otherwise expect the unexpected .

It'd be nice if the VB team would post about this and let us all know the exact scale of the problem and when a fix will be available.

Posted by bill | with no comments
Filed under: , , , ,

Snippet paths and duplicates in VS 2008

If you install VS 2008 on a machine that has VS 2005 on it, you'll be prompted to let VS 2008 import as many of your settings from 2005 as possible.  Generally this is a good thing, but you might run into some issues with code snippets as you might have both the 2005 and 2008 collections loaded in VS 2008. 

The problem is Visual Studio stores snippets by default in the %InstallRoot%, which for VS 2005 is Program Files\Microsoft Visual Studio 8 and for VS 2008 it's Program Files\Microsoft Visual Studio 9.0. But when Visual Studio migrates your settings from 2005 to 2008, it just copies the registry value which include %InstallRoot% in the paths as a replacement variable.  So what this means is if you modified the snippets by hand or using some other tool, your new snippets will NOT include the ones you previously modified.

If on the other hand you used the snippet editor in 2005, it saves the paths as the full path, so in VS 2008 you'll have both the 2005 paths and the 2008 paths.  This also is not ideal.  You can quickly remove the root folders you don't want by right clicking on them and selecting remove. (Note: root level folders are the folders stored in the registry and have a blue asterisk on them in the snippet editor)

In either scenario you are still faced with maintaining multiple collections of snippets by default.  The best thing you can do is to create a folder in your user do documents folder, and then move sub folders and snippets to that path. This makes it easy to share snippets between versions of Visual Studio.  You'll need to open the collections to remove the paths that are in the %InstallRoot%, but once done you have a centralized common folder tree for your snippets.

In fact, I like the idea of doing that so much, I think I will add a wizard to the Snippet Editor that does this for you.  This would also let you have your snippets where you don't need administer rights to modify them.

There's also a couple of other minor changes I plan to make later this week, such as surfacing the collection selection onto the folder pane.

Oh, and if you are using the Snippet Editor as is with VS 2008 alongside VS 2005, you might be seeing duplicates for snippets in Visual Studio but not in the Snippet Editor.  This is yet another one of the undocumented "quirks" about how Visual Studio deals with snippets. If you look in the languages\CodeExpansions key registry you'll probably see some values stored under "Basic" and others under "Visual Basic".  This is a "temporary" state.  If you run Visual Studio's Snippet Manager and click on the OK, it will remove the "Basic" key and put all the values under the "Visual Basic" key, at which point the Snippet Editor and VS's Snippet Manager will all be showing the same collection.  When I wrote the Snippet Editor, once the "Visual Basic" key was established in the registry, it meant that the collection was already initialised. If it wasn't there then I'd initialise it for you. Same kind of thing with "CSharp" and "Visual C#".  But now with 2008 that pattern has changed, as in both keys can be there.

Unfortunately little of this if any is documented, so it means my code has to be reactionary rather than pro-active: That is, I have to fix it when they do things that break it ;)

Posted by bill | with no comments
Filed under: , ,

Bug in XML literals around the Imports keywords..

 In VB9, there's a bug when using XML literals with any type member that is a protected keyword.  For example, given the following psuedo types,

 

Class Doc
   Public Property [Imports]() As List(Of DocImports)
End Class

Class DocImports
 
  Public Property [Namespace]() As String

End Class

 

The following code won't compile:

 

Dim d As New Doc

Dim xml = <?xml version="1.0" encoding="utf-8"?>
                <Imports>
                   <%= From item In d.Imports _
                      Select <Import><Namespace><%= item.Namespace %></Namespace></Import> %>
                </Imports>

 

The trick is to escape the Imports property inside the the XML query:

Dim xml = <?xml version="1.0" encoding="utf-8"?>
                <Imports>
                   <%= From item In d.[Imports] _
                      Select <Import><Namespace><%= item.Namespace %></Namespace></Import> %>
                </Imports>

 

 

This seems to only apply to some keywords, not all. Imports, Sub, Function all cause the problem, yet Class, Private and Namespace don't.  One thing you'll see is the <%= substitution block won't get the characteristic highlight colour.

 

And another quirk is if the property is called Option, VB will colour that as if it is a keyword even when it is a property such as stock.Option.  This one only seems to impact Option, and does not impact the actual compile, only code aesthetics ;)

Posted by bill | 2 comment(s)
Filed under: , , , , ,

Is Vista more secure ?

Kathleen talks about security being Vistas biggest asset.... for me, a power user who use to run XP in limited access mode, using run as admin scripts, it isn't really anymore secure. In fact, I now find it harder to find a good software firewall that shows me what's happening and allows me to easily change things.  But for me, the biggest problem with Vista is I simply don't trust it.  The reason for this is the shell is explorer right ?  Yet explorer in vista is extremely buggy.  Even though I have it set not to remember any folder views, it seems to randomly decide to change views as I browse from folder to folder. Then recently I had it no longer allow me to select any more than one file at a time !  To fix that I had to browse the online forums to find that I should try deleting mru bags or similar in the registry and also try resetting all views.

So am I really meant to feel that the software is secure when explorer itself is so incredibly buggy ?  After all these years they still can't even write a reliable file explorer !  Given the short comings of such a major and central piece of software, it's hard for me to really believe that any of the rest is more secure.  Like if you bought a new car that supposedly had improved safety features, yet the dash was rattling apart, would you really feel more safe in it ?  With Vista I don't.

Posted by bill | with no comments
Filed under: ,

VS 2008 is LIVE !!!

Just spotted it on MSDN subscriber downloads.

 

Enjoy :)

Posted by bill | 2 comment(s)
Filed under: , , , ,

Ben Franklin ???

I see Paul Vick has suggested Ben Franklin as a better persona for VB than Mort.  How very ironic  !!

Ben has his picture on American currency... the $100 note in fact. 

Yes that's right folks. the "C Note"

I wonder if he means sharp or flat ;)

Posted by bill | with no comments
Filed under: ,
More Posts Next page »