473,544 Members | 1,872 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 1181
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
2227
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
13
2356
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...
7
1810
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
1604
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...
7
1373
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
9621
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...
70
3296
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
4011
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. ...
15
7840
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
1886
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
7452
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7643
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. ...
1
7405
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...
0
4938
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...
0
3441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
688
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.