473,408 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Class data variable question

KCT
The following class member function (DataClass.DataUpdate) is called by an
event handler at periodic intervals. I want to increment the counter
(Counter) each time it's called, but I don't know how to keep the variable
from being re-set to zero each time the class member function is called.

public class DataClass
{
public static void DataUpdate(Form1 frm)
{

int Counter = 0;

Counter++;

// code here to update a field on the form

}

}

I realize this is a very basic question. Any help would be greatly
appreciated.

Jan 7 '06 #1
3 1175
On Sat, 7 Jan 2006 14:23:48 -0500, KCT wrote:
The following class member function (DataClass.DataUpdate) is called by an
event handler at periodic intervals. I want to increment the counter
(Counter) each time it's called, but I don't know how to keep the variable
from being re-set to zero each time the class member function is called.

public class DataClass
{
public static void DataUpdate(Form1 frm)
{

int Counter = 0;

Counter++;

// code here to update a field on the form

}

}

I realize this is a very basic question. Any help would be greatly
appreciated.


Declare counter outside the function as static variable
Jan 7 '06 #2
You have two choices, depending upon the effect you want to achieve.

1. Declare the variable as a private field in your class:

public class DataClass
{
private int _counter = 0;

public static void DataUpdate(Form1 frm)
{
this._counter++;
// code here to update a field on the form
}
}

this will give you a different counter for each instance of DataClass
that you instantiate. However, this probably isn't what you want.

2. You probably want one counter for your whole program, in which case
follow "Someone"'s advice:
public class DataClass
{
private static int _counter = 0;

public static void DataUpdate(Form1 frm)
{
DataClass._counter++;
// code here to update a field on the form
}
}

This will give you one global counter for all instances of the class
DataClass within one run of your program.

Jan 9 '06 #3
"KCT" <KC@spamfree.com> wrote in message
news:qL********************@giganews.com...
The following class member function (DataClass.DataUpdate) is called by an
event handler at periodic intervals. I want to increment the counter
(Counter) each time it's called, but I don't know how to keep the variable
from being re-set to zero each time the class member function is called.

public class DataClass
{
public static void DataUpdate(Form1 frm)
{

int Counter = 0;

Counter++;

// code here to update a field on the form

}

}

I realize this is a very basic question. Any help would be greatly
appreciated.


Try this class:

namespace testcls
{
public class DataClass
{
// initializes to zero by default in the constructor
private int _counter;

public DataClass()
{
}

public int Counter
{
get { return _counter; }
set { _counter = value; }
}

public void DataUpdate()
{
// perform one iteration of update stuff
Counter++;
}
}
}

And here is a class I used to test the counter property (it is in VB):

Imports System
Imports testcls

Module Module1

Sub Main()

Dim test As New DataClass

Console.WriteLine(test.Counter.ToString)
test.DataUpdate()
Console.WriteLine(test.Counter.ToString)

End Sub
End Module

The output from this console application looks like this:

C:\dev\sln\testcon\bin>testcon
0
1

I hope that helps.

carl
Jan 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
7
by: bartek | last post by:
Hello, I've been pondering with this for quite some time now, and finally decided to ask here for suggestions. I'm kind of confused, actually... Maybe I'm thinking too much... Brain dump...
0
by: Ed West | last post by:
Hello, I am wondering about best practices for class hierarchies and using ADO.Net, especially the DataSet update/delete commands and the data relations... this needs to package/unpackage to xml...
7
by: fisab | last post by:
I apologise in advance for such a basic question, but I'm hoping someone will take the time to answer my question. In my code I define a Dataset : Partial Class Default_aspx Dim dsExcel As...
8
by: downwitch | last post by:
Either I don't understand (entirely possible), or there's no way to copy parts of a class hierarchy from one instance to another. Say I have a class called Foo, and it contains, among other...
70
by: garyusenet | last post by:
I'm using an example piece of code: - namespace Wintellect.Interop.Sound{ using System; using System.Runtime.InteropServices; using System.ComponentModel; sealed class Sound{ public static...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.