Regarding the static
keyword.
Up to this point, via my own research, I have a general idea of what the static
keyword is, but I feel that all these different descriptions and details have only confused me more. At the moment, I really don't feel that I know how to properly use "static
"; it seems to be used differently between C# and VB.NET and applies differently within the language depending on what you are using it for…
While reading the MSDN article Static (Visual Basic) , many questions arose, specifically when I read this sentence:
Normally, a local variable in a procedure ceases to exist as soon as the procedure terminates. A
static
variable remains in existence and retains its most recent value.
Is the VB.NET version of static
the same as C# or Java, is the concept the same for most languages?
If static
retain a value within a class, and we are able to access that certain member, function without instantiating the class, is this safe to use loosely? In other words, should we keep a close eye when using static
’s within classes? They remind me of global variables for some reason. Maybe I’m just being ignorant here and simply need more practice to understand their purpose.
What are good scenarios where using static
benefits and promotes reusability of code?
A static method can access static methods and variables as follows: A static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class.
use static variables when : The value of the variable is independent of the objects (not unique for each object). E.g. number of students. Show activity on this post. Static variable: When you need something that will be used through out the application and every instance need to know the variable.
The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. The static variable gets memory only once in the class area at the time of class loading.
In C# the static
keyword is the same as the shared
keyword in VB.NET. Namely, from Static Classes and Static Class Members (C# Programming Guide):
Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class.
In VB.NET static
works very differently, as it applies to variables, not types.
I do not know if there is a C# equivalent for this behavior.
1) Is the VB.NET version of static the same as C# or Java, and is the concept the same for most languages?
It's different. shared
is the same, though.
2) If static retain value within a class, and we are able to access that certain member, function without instantiating the class – Is this safe to use loosely? In other words, should we keep a close eye when using static’s within classes? They remind me of global variables for some reason, maybe I’m just being ignorant here and simply need more practice to understand their purpose.
Be very careful, they are global to the application domain.
3) What are good scenarios where using static benefits and promotes reusability of code?
I use static lists to cache sets of data that are used to populate dropdown lists so that I don't have to keep hitting SQL Server as one example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With