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

If there is any at all?

2007-12-09 10:05:58 · 5 answers · asked by Jamaal E 3 in Computers & Internet Programming & Design

Re:
Well that's one difference. But what sets my normal variables from my static variables apart then?

2007-12-09 10:26:20 · update #1

Re Re:
When I hear static, I usualy think Static ip, something that you can change, but wont change from a program run or process.

2007-12-09 10:27:40 · update #2

5 answers

As mentioned a constant cannot be changed by the program.

Private Constant k_pi as double = 3.1415

now any where k_pi is used in the program the compilier will replace k_pi with its assigned value of 3.1415. Constants allow you to use a name for a numeric value that is not expected to change during program execution.



Static vs Dynamic Variables:

When a proceedure is exectued in VB, variables are dimensioned (DIM statement) and brought into existance for the life of the proceedure. That is any variables dimentioned within a proceedure exist only as long as the proceedure exists.

Private Function myCount() as integer
Dim cnt as integer
cnt = cnt + 1
return cnt
end function

The intent of the above proceedure is to count how many times the proceedure is called and return that count.
However the cnt variable is created every time the proceedure is called and then destroyed at the end of the function (dynamic allocation). This resets the count to zero each time the function is called no matter how many times the function is called because a new instance of the variable is called into existance and initialized to zero.

How ever by declaring the variable as static, you are telling VB to preserve the variable for the next time the proceedure is called. Once the variable is allocated memory space it is not destroyed at the end of the function.

Private Function myCount() as integer
Dim STATIC cnt as integer
cnt = cnt + 1
return cnt
end function

By decalring STATIC you tell VB to keep the variable cnt dimensioned and to not destroy it when ending the function. THe memory resources allocated for the variable will remain assigned to variable cnt for use within the declared function. This preserves the value of the variable and now the intended increment function will work as intended.

2007-12-09 13:17:24 · answer #1 · answered by MarkG 7 · 0 0

Vb Static Variable

2016-12-14 13:07:13 · answer #2 · answered by wingert 4 · 0 0

i do no longer understand VB, yet in C/C++ a static variable has 2 features that make it diverse from automatic or worldwide variables. First, it would not bypass away whilst the function returns. so which you're able to have an announcement of static long UseCount=0 and increment it later interior the function: UseCount++ once you print UseCount, it would desire to have the variety of calls to the function. this may be outstanding for protecting song of errors or no longer reinitializing some thing that's already initialized, etc. the 2nd function is that a static variable declared exterior of a function isn't in scope (no longer seen) to different information. This makes it "quasi-worldwide" because of the fact it is accessed from all applications interior the only document. This became super for merchandise oriented suggestions in C earlier C++ became created.

2016-11-14 05:42:26 · answer #3 · answered by ? 4 · 0 0

You can change the value of a static variable, you can't with constants.

ADDITIONAL DETAILS:

The term 'static' in static variables actually refers to the allocated memory address that stays static until the end of the program.

What it means for us as programmers is:
1. The value of a static variable is reserved throughout the lifespan of the program.
2. In object-oriented languages, access to that variable is shared by all objects.

Example for #1 above:

function abc () {
static int staticVar = 1;
int nonStaticVar = 1;

staticVar++;
nonStaticVar++;
// The code below prints the values of 'staticVar' and 'nonStaticVar', separated by a dash
printf("%d - %d\n", staticVar, nonStaticVar);
}

abc(); // This first call should print: 2 - 2
abc(); // This second call should print: 3 - 2, because in the previous call, the value of 'staticVar' was incremented by 1 and this value is reserved. While for 'nonStaticVar', every time the function is called, the value is reset to '1' (before it's then explicitly incremented inside the function).
abc(); // This third call should print: 4 - 2


Example for #2 above:

class Foo {
public static int staticVar = 1;
public int nonStaticVar = 1;

public void printMyVars () {
// The code below prints the values of 'staticVar' and 'nonStaticVar', separated by a dash
printf("%d - %d\n", Foo::staticVar, this.nonStaticVar);
}
}

Foo myFirstFoo = new Foo();
Foo mySecondFoo = new Foo();

Foo::staticVar++; // staticVar becomes '2'
myFirstFoo.printMyVars(); // this should print: 2 - 1
mySecondFoo.printMyVars(); // this should print: 2 - 1

myFirstFoo.nonStaticVar++; // nonStaticVar for myFirstFoo becomes '2', nonStaticVar for mySecondFoo is still '1'.
myFirstFoo.printMyVars(); // this should print: 2 - 2
mySecondFoo.printMyVars(); // this should print: 2 - 1

Foo myThirdFoo = new Foo();
myThirdFoo.printMyVars(); // this should print: 2 - 1


If you still have problems understanding this, would be good if you let us know the programming language you are familiar with, so that we can explain and give examples that are more targeted for that language.

2007-12-09 10:22:21 · answer #4 · answered by Robin T 5 · 2 0

You can only access static variables from a static context....but other than that ya, basically a constant is a named variable opposed to a static variable being...static.

Hope that helps :)

2007-12-09 10:37:59 · answer #5 · answered by Anonymous · 0 1

fedest.com, questions and answers