views:

73

answers:

2

Say I have this constructor:

/// <summary>
/// Example comment.
/// </summary>
public SftpConnection(string host, string username, 
    string password, int port) {...}

which has these overloads:

public SftpConnection(string host, string username, string password) 
    : this(host, username, password, 22) { }

public SftpConnection(string host, string username, int port) 
    : this(host, username, "", port) { }

public SftpConnection(string host, string username) 
    : this(host, username, "", 22) { }

and in reality, the XML comment is pretty large, with param, example and exception elements and so on.

Is there some way to add a special XML comment one-liner to the overloads, such that they use the exact same comments so that I don't need to copy-paste the whole, enormous original comments?

I'm thinking something like: <use cref="SftpConnection(string,string,string,int)" /> which doesn't work of course.

I am aware of the include element, but I get the impression it reads the comments from an XML file instead, which I don't want - I want the comment to still be visible in the code, but only once.

Thanks :-)

UPDATE: For those who ask this question in future: After some digging, I've learned that the functionality that I wanted simply does not exist. However some good alternatives might be:

  • Consolidating your overloads (see Timwi's answer)
  • Linking to your original comment using the see element
  • Using the include element to save your duplicated comments in an XML file and pasting them once, for only one of your overloads.
+1  A: 

Is this what you want?

/// <seealso cref="SftpConnection(string,string,string,int)"</seealso>
Richard J. Ross III
That does not work, but I will look into the seealso element, TY.
rmx
OK, I can't seem to get `seealso` to do _anything_. Long shot but is it perhaps something to do with the version of c# or .net?
rmx
I think its meant for creating rich HTML documentations using doxygen and other documentation tools... ill look into what it does, though...what version of VS are you using (or are you using Mono)?
Richard J. Ross III
VS 2010 - I think you have to use the fully-qualified names though so your example might not work.
rmx
After looking at visual studio's documentation and trying several workarounds, I have decided that the `<seealso></seealso>` is for reference only.
Richard J. Ross III
+4  A: 

You can’t really do this. I find it annoying too.

However, you can alleviate the problem by using default parameter values instead of lots of overloads. Instead of:

public SftpConnection(string host, string username, string password, int port)
public SftpConnection(string host, string username, string password)
public SftpConnection(string host, string username, int port)
public SftpConnection(string host, string username)

You can have just a single one:

public SftpConnection(string host, string username, string password = "",
                      int port = 22)

This has multiple advantages:

  • Need only one XML comment. The whole point of my answer. ☺

  • Users of Visual Studio can instantly see that the default value for port is 22. With the overloads, this is not obvious; you have to specifically mention it in the documentation.

  • You indirectly encourage client code to become more readable by encouraging the use of named parameters (e.g. port: 2222 instead of just 2222, which is less clear).

And the greatest part about this is that using default values does not remove the ability to still have several overloads if you need them. Typical examples where you want overloads with default values might be something like...

ReadFrom(string filename, ReaderOptions options = null)
ReadFrom(Stream stream, ReaderOptions options = null)
ReadFrom(byte[] rawData, ReaderOptions options = null)

In these cases, I would argue the XML comments should actually be different.

Timwi
You're right about that. I have changed my code as you suggested. Nevertheless, this is going to bug me now in the future! And there must be other situations where this functionality is needed.
rmx
Definitely. Like I said, I find it annoying too. Default values only *alleviate* the problem, not *solve* it...
Timwi
Of course this requires that you use the C# 4.0 compiler...
Tergiver
@Tergiver: Of course. I don’t normally consider it necessary to say because it’s not that common that people have to use something obsolete.
Timwi
@Timwi: Actually I find it very common that people use the older versions of .NET For some strange reason companies have a hard time moving over to new technologies sometimes. I for example am still using 3.5 for work.
Adkins