473,789 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class data variable question

KCT
The following class member function (DataClass.Data Update) 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(Form 1 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 1196
On Sat, 7 Jan 2006 14:23:48 -0500, KCT wrote:
The following class member function (DataClass.Data Update) 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(Form 1 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(Form 1 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(Form 1 frm)
{
DataClass._coun ter++;
// 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.co m> wrote in message
news:qL******** ************@gi ganews.com...
The following class member function (DataClass.Data Update) 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(Form 1 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.WriteLi ne(test.Counter .ToString)
test.DataUpdate ()
Console.WriteLi ne(test.Counter .ToString)

End Sub
End Module

The output from this console application looks like this:

C:\dev\sln\test con\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
2278
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
13
2389
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. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
7
1825
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 follows... I need a class to represent a variable, with an associated data type and/or value. Though, I don't want it to be a variant type, and not a
0
1612
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 for client/server sending. The class properties pretty much correspond to database tables. This is a pretty long question so thanks in advance for reading and commenting!
7
1379
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 New DataSet I fill the Dataset as follows :
8
9633
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 things, a custom collection called SubFoos. If I do something like Dim MyFoo1 As Foo Dim MyFoo2 As Foo 'call something to populate MyFoo1 and its SubFoos...
70
3380
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 void MessageBeep(BeepTypes type){ if(!MessageBeep((UInt32) type)){ Int32 err = Marshal.GetLastWin32Error();
20
4044
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 tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
15
7873
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
1898
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(); TheObject.Dictionary<string, string= new Dictionary<string, string>(); .... } The first line (TheObject instance = new TheObject();) doesn't get
0
10412
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.