views:

45

answers:

3

Can I add links in comments to a code block in Visual studio ?

For example:

// block 1
class class1
{
}

// block 2
class class2
{
    // review [___class1]
}

[___class1] is a link for class1

Thanks in advance.

+1  A: 

It depends what you want - you can just add a URL and VS will turn it into a link automatically within the code view.

Whether this is translated into a link when you generate the documentation will depend on what tool you use for that.

EDIT: Okay, it's possible that I missed your meaning. It's not terribly clear exactly what you're trying to do.

If you want to provide some example code, you can use the example tag:

/// <example>
/// Foo f = new Foo();
/// </example>

Is that what you meant?

You can't link to a particular block of code, but you can link to a member or type, for example:

/// <remarks>
/// You can use the <see cref="DoSomething" /> method to do something similar.
/// </remarks>
Jon Skeet
I believe Homam wants to link to a code snippet in his own source code file.
Giorgi
@Jon Skeet: can I add bookmarks in the code?
Homam
I think he wants to hyperlink his code to other portions of code, specifically *blocks* of code.
Randolpho
@ Giorgi, Randolpho: exactly, thanks
Homam
A: 

You can always put links in comments (since they are simply text, like the rest of the file).

It depends on the IDE how these will be displayed - Visual Studio will make it a clickable hyperlink.

Edit:

If you want to reference other sections of your code from comments, there is no current support in visual studio for this. The closest you can get is with code documentation comments using a referntial tag such as see, this will still not produce a hyperlink in the IDE.

Oded
+1  A: 

You can bookmark your code in Visual Studio, but that's stored in your user options file and isn't normally checked into source control. I don't know of a way to link to portions of code from other portions of code.

Your best bet might be to use document comments and a <see> tag:

/// <see cref="Fully.Qualified.Type.Name"/>

But that's going to be limited to fully qualified locations, i.e. types, methods, fields, and properties. A particular block (say, an if statement within a method) is right out, and it'll only link the documentation for that method/whatever to another documentation section, and then only if you generate the documentation using a tool like Sandcastle.

One other thing you might consider, and this is a very bad hack, is to use file hyperlinks, like so:

// file://c:/code/file.cs

There are caveats:

  • You have to use the full path name. Relative paths won't work, so it's going to be tied directly to your code, and won't work if you remap your source repository to another location
  • Visual Studio will stop at the first space, so spaces in file names or folder names will cause it to fail
  • You can't link to a portion of the code, just the whole file.
Randolpho