Visual Studio and Delphi: Getting a new GUID in the code editor
Posted by jpluimers on 2012/06/28
Earlier this week, I already wrote about different idioms in different IDEs.
Here is another one, again a feature I don’t use often: getting a fresh GUID in the IDE.
Visual Studio
Most often you need it as part of your AssemblyInfo.cs file:
[assembly: ComVisible(false)] [assembly: Guid("5F36D50A-CF90-4677-82D3-07DC1E88AE43")]
Note may you need a Guid attribute inside your AssemblyInfo.cs, even if your ComVisible atrribute as you might have specific classes in your assembly that have ComVisible set to true.
To get a new GUID in your code editor, perform the steps from this video:
- Go to the TOOLS menu
- Click on the Create Guid submenu
- In the dialog, select the format you want, then
- Click on the Copy button, then
- Click on the Exit button, then
- In the code editor, paste the GUID
Someone should write a CodeRush extension to make this easier (:
Delphi
Usually you need a GUID as part of an interface declaration:
type IMyInterface = interface['{E432418F-433A-42A7-AA1B-9BA2560AF299}'] // ... end;
It is always wise to have a GUID on your Delphi interfaces, as without the GUID, the is and as operators will not work on interfaces.
The Delphi IDE has a shortcut to add a GUID in your source code, so only one step is needed:
–jeroen
François said
1. Interesting to see the coincidence(?) with the other Delphi blogpost of the day: http://delphiblog.twodesk.com/programmer-productivity-using-the-keyboard-better.
2. Guys, don’t miss the obvious. The point is not that it cannot be done more or less easily (we already have 3 comments proposing a solution). It should be already there at my fingertips without having me even wondering which solution might work… An IDE is all about making you more productive, not writing VB macros.
WarrenP said
I wish I could write macros like this (or others) in Delphi without writing native OTA wizard (gexperts) code. I give the nod to Visual Studio here. A macro language in the IDE is far superior to our IDE capabilities, Ctrl Shift G being a tiny point by comparison.
W
jpluimers said
CodeRush had them for Delphi (:
piotrbrzezinski said
1. Create macro:
Public Module InsertGUIDModule
Sub InsertGUID()
Dim selection As EnvDTE.TextSelection
Dim startPoint As EnvDTE.EditPoint
Dim TypeLib As Object
Dim Guid As String
TypeLib = CreateObject(“Scriptlet.TypeLib”)
Guid = “””” + TypeLib.Guid.Substring(1, 36) + “”””
TypeLib = Nothing
selection = DTE.ActiveDocument.Selection()
startPoint = selection.TopPoint.CreateEditPoint()
startPoint.Insert(Guid)
End Sub
End Module
2. Create shortcut for this macro (Options/Keyboard).
Danny Thorpe (@danny_thorpe) said
Ya know, when I was doing my stint in VS devdiv, I asked about adding a shortcut to VS to produce a GUID at the cursor location. Asked many times. Asked how to do it. All I got were blank stares. :/
mterwoord said
It’s possible to do macro’s in VS like you can in Word. With that you can bind (For example) ctrl+shift+g to a new-guid macro..
Petr Vones said
The VS macro is pretty simple:
Sub Create_GUID()
DTE.ActiveDocument.Selection.Text = System.Guid.NewGuid().ToString(“D”).ToUpper()
End Sub