Extending Strongly Typed Datasets

Strongly Typed Datasets are a rather nice addition to VS2005 (they don't lend themselves well to acronyms however)

You still need to create a database and tables and realationships but using STDs saves a writing huge amount of code.

If this code were generated by hand or by a utility such as CodeSmith then the classes could just be edited to add function as required (though most of the benefits of code generation are lost once the generated code is edited).

The STD source is of course auto-generated from the xsd file, a convienient way to view the C# source is to type the STD name and use the context menu to 'Go To Definition', or press F12.

This will open a c# source file with a guid name. You can edit this file but it will be overwritten each time the xsd is compiled.

You can add new methods and properties to the STD classes by using partial classes. First create a new cs (or vb if you swing that way) file with the filename same as your xsd - for example 'ObjectSTD.cs'.

Edit the class definition adding the partial keyword as below.

You can add a new method to the ObjectRow class within the ObjectSTD class with the following code...

public partial class ObjectSTD
{
    public partial class ObjectRow
    {
        public string funcToString()
        {
            return string.Format("({0}) {1}", ObjectId, Description);
        }
    }
}

Note that private properties and methods are available and intellisense supports these.

Author Dave Howard

I have been involved in IT development for the last 10 years - a lot of it around desktop applications and telecoms.

Comments

Paul Hayman said:

Are you saying that from within the partial class, the private methods and properties (which you would expect to be able to access) aren't available? Is this just an IntelliSence thing, does it compile.

23/Jul/2006 21:39 PM

Add Comment

Name
Comment
 

Your comment has been received and will be shown once it passes moderation.