The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,152 other subscribers

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:

  1. Go to the TOOLS menu
  2. Click on the Create Guid submenu
  3. In the dialog, select the format you want, then
  4. Click on the Copy button, then
  5. Click on the Exit button, then
  6. 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

7 Responses to “Visual Studio and Delphi: Getting a new GUID in the code editor”

  1. 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

  2. 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).

  3. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: