Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
Visual Studio
Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
You can programmatically add a data object to the data cache in a document by calling the StartCaching method of a host item, such as a Document, Workbook, or Worksheet. Remove a data object from the data cache by calling the StopCaching method of a host item.
The StartCaching method and the StopCaching method are both private, but they appear in IntelliSense.
Applies to: The information in this topic applies to document-level projects for Excel and Word. For more information, see Features available by Office application and project type.
When you use the StartCaching method to add a data object to the data cache, the data object does not need to be declared with the CachedAttribute attribute. However, the data object must meet certain requirements to be added to the data cache. For more information, see Cache data.
To programmatically cache a data object
Declare the data object at the class level, not inside a method. This example assumes that you are declaring a DataSet named
dataSet1that you want to cache programmatically.public DataSet dataSet1;Public dataSet1 As DataSetInstantiate the data object, and then call the
StartCachingmethod of the document or worksheet instance and pass in the name of the data object.dataSet1 = new DataSet(); if (!this.IsCached("dataSet1")) { this.StartCaching("dataSet1"); }dataSet1 = New DataSet() If Not (Me.IsCached("dataSet1")) Then Me.StartCaching("dataSet1") End If
To stop caching a data object
Call the
StopCachingmethod of the document or worksheet instance and pass in the name of the data object. This example assumes that you have a DataSet nameddataSet1that you want to stop caching.if (this.IsCached("dataSet1")) { this.StopCaching("dataSet1"); }If (Me.IsCached("dataSet1")) Then Me.StopCaching("dataSet1") End IfNote
Do not call
StopCachingfrom the event handler for theShutdownevent of a document or worksheet. By the time theShutdownevent is raised, it is too late to modify the data cache. For more information about theShutdownevent, see Events in Office Projects.