Static or private

This is one of those questions that has calmed down in my head from the very first day of programming in VB as a whole. I have my own personal ideas about this, but I wonder what the general consensus is regarding the use of static variables.

Static Variable:

Public Function Whatever() as Boolean
     Static Flag as Boolean
     If not Flag then
        ' do something
        Flag = True
     end if
     Return Something
End sub

VS:

Private variable:

Private Flag as Boolean
Public Function Whatever() as Boolean
     If not Flag then
        ' do something
        Flag = True
     end if
     Return Something
End sub

Unless someone else knows, the above are functionally equivalent, except that the "Private" flag is available for use elsewhere in the class.

Questions begin to arise with Statics .. how ..

Where are they stored .. when they are REALLY created and located, etc.

Obviously, the compiler adds them to the heap of data (I know the poor use of THAT words) for the class somehow ... But is there a penalty for this in terms of overhead, garbage collection, etc.

, , , .

- - ?

PS: , SO...

. , . , , , .

....

.

https://weblogs.asp.net/psteele/7717

+4
2

, .

"" , . , , , , .

, , .

...

  • SIMPLE (, ..) , . , , 1000 ... .

  • , , () , Dispose Dispose.

  • , - . , , - .

  • , Private variable, , , . stat_Half_Time_Score

0

- - ?

. SOLID: S " ". :

.

X, X. , , . , .

, function-static (= local), - .

. .

( /, .)

+5

Source: https://habr.com/ru/post/1668755/


All Articles