storing a variable(caching)

i need to store one value in a counter variable in vsto excel appln which cannot be stored in database.when i close the appln and open it again it should hold the value(persist)

how can i implement this in excel application



Answer this question

storing a variable(caching)

  • DeadlySpider

    sir

    i am writing a vsto excel workbook application


  • Judas

    Hi,

    Sorry you're having troubles.

    The link below explains how to cache non-DataSet objects:

    http://msdn2.microsoft.com/en-us/library/ms178808(VS.80).aspx

    Caching a dataset and caching an int are no different.

    I'm not sure what the blue underline means. This largely depends where it is. When you build your application what errors or warnings do you see in the output window

    Thanks,

    Ade



  • Rachel Falzone - MSFT

    Sir the code you gave is

    [Cached]
    public int _myCounter = 0;

    ...

    _myCounter++;

    my visual studio editor is showing error(blue underline)

    what is customization class

    In the msdn link you gave, it describes about caching a dataset but not a variable like counter

    my task is to have a integer variable counter to be cached, pls give me some explanation

    thank u


  • Galher

    If you declare the counter as a public member on your customization class and mark it with the Cached attribute then VSTO will save this value along with the document and reload it next time the document is opened:

    [Cached]
    public int _myCounter = 0;

    ...

    _myCounter++;

    More details can be found here:

    http://msdn2.microsoft.com/en-us/library/75akte27(vs.80).aspx

    Thanks,

    Ade



  • simmonsj_98

    Hello,

    I can think of several things that might be preventing the changes to your variable from being saved:

    - Are you rebuilding your project every time you run it If you do this, the existing workbook (the one with the changed variable) gets overwritten, so you basically start over from the initial state again.

    - Are you saving the workbook before closing it The change to the variable will not be saved to the data cache unless you save the entire workbook.

    The following code works for me. Although you can manually save the workbook before closing it down, I added some code to save it automatically. Please see if this code works for you. After building it, the first time you run it the myCounter variable should have a value of 2. Then, close the workbook, and then open it again from the build directory. This time, the myCounter variable should have a value of 3, and so on after that.

    <Cached()> _
    Public myCounter As Integer = 1

    Private Sub Sheet1_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    myCounter = myCounter + 1
    MessageBox.Show("The current value of myCounter is: " & myCounter)
    Globals.ThisWorkbook.Save()
    End Sub

    I hope this helps,

    McLean Schofield



  • MP2006

    thank u sir for responding

    but still iam unable to get my problem solved

    can u please reply with the code for my requirement

    my requirement is

    create an integer variable named counter

    when a button is clicked in excel appln, this counter should be incremented

    when i close my application, counter variable should hold the value

    please send me the code sir


  • Hyperion_20

    In VB attributes use <...>, rather than [...]. You Cached attribute should look like this:

    <Cached> myCounter as Int

    http://www.ondotnet.com/pub/a/dotnet/excerpt/vbnut_8/index1.html



  • knallbunt

    Sir

    can u send the code in vbasic

    the IDE is showing error on

    [Cached]

    when i move the cursor on it, it is showing tooltip text 'declaration expected'

    thanku


  • maheshshinde2000

    Hi,

    I understand that you would like to cache a data value in your document. Please see the data caching docs that will show you how to do this.

    http://msdn2.microsoft.com/en-us/library/75akte27(VS.80).aspx

    Regards,
    Paul Stubbs
    Program Manager



  • JHibbins

    Then all you need to do is declare your counter variable as a public variable or get/set property on the customization code class and mark it as cached.

    namespace ExcelWorkbook15
    {
    public partial class Sheet1
    {
    [
    Cached]
    public int myCounter;

    Then add code to modify myCounter inside your event handler.

    If this is giving you compile errors then post them here and I'll have another look.

    Thanks,

    Ade



  • James Rosenvall

    OK So I'm a little confused. I noticed you refered to and "VSTO excel appln". I assumed, possibly erroneously, that what you meant was and excel appl(icatio)n. Is this the case or are you writing a managed Add-In or are you writing a VSTO Excel Workbook application

    The code I gave you and the links show you how to cache data in the VSTO Excel Workbook application case.

    Thanks,

    Ade



  • cam_church

    Partial Public Class ThisWorkbook

    <Cached()> Public i As Integer = 1

    In the button click event handler

    i=i+1

    im getting no errors, but the value is not retained when i close the application.again i value is initialised to 1

    pls pls solve this for me sir

    thank u


  • storing a variable(caching)