codeReference

A codeReference element is used to insert a commonly used code snippet into a conceptual topic. The code snippets are stored in an external file.

  Note

When using the Sandcastle Help File Builder or the standalone build components from it, the Code Block Component offers the same set of features and several others for both inline and external code snippets. In addition, it supports many other languages and can import code from working source code files which allows you to pull in examples from working projects that you can maintain and build without having to update a separate example snippets file. As such, you may prefer to use it rather than the code snippets file.

Usage

To use a code reference, simply insert its ID as the inner text of a codeReference element anywhere within a topic. You can combine multiple snippets with a common example ID into one code reference by specifying their sample IDs as a comma-separated list after the hash character.

Example

This example uses the samples from the example file in the Code Snippets File topic. The first three are the individual snippets. The last one combines the "create instance" snippets into one example.

 
Define a class
<codeReference>ClassDefinition#Define</codeReference>

Create a local instance
<codeReference>CreateInstance#Local</codeReference>

Create a static (shared) instance
<codeReference>CreateInstance#Static</codeReference>

An example of combining snippets with a common example ID
<codeReference>CreateInstance#Local,Static</codeReference>

Example Output

Define a class

C#
public class CSharpClass()
{
    // Members go here
}

Create a local instance

C#
CSharpClass x = new CSharpClass();

Create a static (shared) instance

C#
public static CSharpClass sharedInstance = new CSharpClass();

An example of combining snippets with a common example ID

C#
CSharpClass x = new CSharpClass();

...

public static CSharpClass sharedInstance = new CSharpClass();

See Also