English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

how would i code "a count of items entered" after i press calculate, meaning every time i press calculate it is equal to one item. The information will tally in a label named item count

2007-10-17 14:43:16 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

thx every1 for ur responses

2007-10-20 13:37:51 · update #1

2 answers

This question deals with the "Scope" of a variable and its duration within a program.

When you create a Subroutine or Function and call it one of the first thing that happens is the dimensioning of declared variables. This is done with the DIM statement and tells VB what variable names you will be using and what data types they are.

Private Sub myProgram()
Dim X as string
Dim num as Integer

num = num + 1
'your code goes here
end Sub

Above X & num are dimensioned and these two variables are usable just within the subroutine named myProgram(). These two variables are said to have scope within the subroutine myProgram(). This means that only this subroutine can use these variables and that as soo as this sub routine ends these variables and whatever there contents will be destroyed.

In order to do what you ask your program needs to store information in an area which will not get destroyed when the sub routine ends. There are two ways to do this.

1. Use the STATIC command to tell VB to not destroy the variable when exiting the subroutine or function.

2.) Declare variable in an area which has a wider scope.


First :
Dim X as String
Dim STATIC num as Integer

num = num + 1

STATIC causes VB to keep the num variable and its contents intact. So now the next time this subroutine is run
the variable num will be increment 1 to the previous value of num and not a newly created num that would have otherwise been initialized to zero.


Second:

You can widen the scope that a variable can be used by dimensioning a variable in an area common to more that one sub routine.
This is done at the top of a form or module using either (Dim, Public, Private)

Private m_num as intreger

Private Sub MyProgram()
dim X as string

m_num = m_num +1

end sub

THe variable m_num is a module level variable which has been decalres above all the proceedures within the form or module. Any of these proceedures may use this variable. I added the m_ as a prefix to help identify that it has module level scope.

Note that I can modify this variable from within a proceedure and there is not a dim statement within the proceedure because it has already been declared. (Private m_num as integer)

If you do not have the proper scope or a static variable then every time you reenter the proceedure your counter will be reset to zero. By using a variable with module level scope or a STATIC declaration the value stored in the variable will be preserved for the next time the proceedure is called.


To increment a variable you add 1 to itself

num = num + 1

so everytime this code is run the value stored in num will have one added to it and that result will be stored back in to num.

2007-10-17 16:13:09 · answer #1 · answered by MarkG 7 · 0 1

create a counter variable for the form

dim counter as integer

double click the calculate button to get into it's button pressed event.

counter = counter + 1
count.text = counter

2007-10-17 23:10:27 · answer #2 · answered by Tone Loco 2 · 1 1

fedest.com, questions and answers