Thursday, June 26, 2008

'CType' is Slow to Type

Ok It's not slow but the way my mind works, I tend not to think of casting until after everything else which leaves me wanting to change...

-------------------------------------------------------------
Dim X as XElement = SomeFunctionThatGetsAnXNode()
-------------------------------------------------------------

...into...

-------------------------------------------------------------
Dim X as XElement = CType(SomeFunctionThatGetsAnXNode(), XElement)
-------------------------------------------------------------

...and the number of keystrokes necessary to do this is in my opinion excessive.

So a couple of days ago, rather than knock up some kind of refactoring (which may have been extreme overkill), I thought about how I might do this with a "Selection Embedding".

Here is what I did.

1.> Create a selection embedding using the right-most embedding type.

-------------------------------------------------------------
Follow these steps to get to the Embedding options page:

1. From the DevExpress menu, select "Options...".
2. In the tree view on the left, navigate to this folder:

    Editor\Selections

3. Select the "Embedding" options page.
-------------------------------------------------------------

Then create a new embedding as shown

SelectionEmbeddingCType

2.> Create a shortcut for it.

-------------------------------------------------------------
Follow these steps to get to the Shortcuts options page:

1. From the DevExpress menu, select "Options...".
2. In the tree view on the left, navigate to this folder:

    IDE

3. Select the "Shortcuts" options page.
-------------------------------------------------------------

Duplicate a shortcut from amongst those in 'Selection\Embedding'.

Then alter its details to give it a suitable keystroke (I chose 'c') and change the parameter to match the name of your selection embedding.

Finally change the context of the shortcut under Selection on the right so that a "line fragment" is required (ticked) and that "Whole line" and "Multi line" is excluded(Crossed)

SelectionEmbeddingKeybinding

After doing this I can now highlight a line fragment and hit C.

This results in the selected text now being wrapped in a CType cast with the caret in the correct location for me to type the class to cast to.

Note you could also have added a marker to the end of the embedding to allow "Esc" to take  you to it but I considered that hitting "End" to be simpler in this case.

Any way.... Job Done.

Questions?  :)


2 comments:

Anonymous said...

VB is slow to type. (Sorry couldn't resist, but C# handles this issue)
XElement x = SomeFunctionThatGetsAnXNode() as XElement;

Rory said...

I would at least agree in this case that the C# syntax is perhaps better, but you have to remember that this can also be applied to DirectCast depending on which approach you wish to take.

Similarly other functions can be applied. I recently learned that the new VB 'If' syntax can take 2 params aswell as 3 which can lead to....
-------------------------------------------------------------
Dim x as String = If(Nothing, "SomeDefault")
-------------------------------------------------------------
In this case x would now be equal to the string "SomeDefault".

The technique described abouve would allow you to take...
-------------------------------------------------------------
X = SomeObject
-------------------------------------------------------------
Highligh the 'SomeObject' and perhaps hit 'd' for Default and have it wrapped thus...
-------------------------------------------------------------
x = If(SomeObject,[CaretGoesHere])
-------------------------------------------------------------

The technique described is presented as a means to wrap any small fragmant in oft used code.

Some parts of VB are more verbose. This is intentional. it was designed for readability rather than brevity.

Note that the new XML Literal syntax adds to this and manages to be more concise into the bargain.

No language is perfect. Hence my desire to have multi-language projects.