Global non static variable

Mimoa

Member
Joined
Apr 29, 2020
Messages
24
Programming Experience
1-3
Hi again.
Can you help me (or just kick) with the use of global variable in VS code.
I am aware of static class of static variables
What I would like to use variable instantiated for every method in class. Again, I am new to C#, so maybe it is trivial and maybe it is not possible (with standards procedures. Because everything is possible ):
So I learned from VS error list I cannot do this
because> An object reference is required for the non-static field, method, or property 'SubscribeDataChange.client'

1590053777672.png



So I go this way:
1590053953644.png


Is it possible to have client instantiated for whole class?
 
You can apply the static modifier to the client field.
 
Static methods can't access instance members of themselves. How could they? The whole point of static and instance members is that instance members belong to a single object and static members belong to the whole type. If a static method was to be able to refer to an instance field, which object's field would it refer to?
 
Over the years, procedural programmers, object oriented programmers and functional programming programmers have learned to avoid the perils of using global variables. Procedural programmers shudder at the thought of debugging large codebases with globals because it is very difficult to reason with when and what will change the global value. Throw in multiple threads and that quickly becomes a dumpster fire. The only thing that may give you half a chance is a debugger that can actively track memory location changes (not just variable value changes due the nature of aliasing). At one point, object oriented programmers masked the fact that they were using global variables by using the Singleton design pattern, but have since admitted that the Singleton is just another global, and will readily say that Singletons are anti-patterns. Functional programmers admit that sometimes globals are necessary, but they try their very best to minimize their use and isolate the updating of the globals at the very end of their pipelines.

When you have a public static variable, you essentially have a global variable. They maybe convenient to use while learning and prototyping code, but don't let you prototype code suddenly be promoted to production code even if an angel investor offered you $1M to go live tomorrow and maintain the code for the next 5 years. (Only accept the $1M if you don't have to maintain the code, and you can use part of the money for your personal security detail to protect you from the angry developers who do have to maintain it. ;-) )
 
Another thing to note is that you may not need the field at all. In standard event pattern the sender parameter in event handler is the instance that raised the event, so in event handler you can cast that object to appropriate type:
C#:
var client = (EasyUAClient)sender;
 
Thanks all for your responses. I am aware of the fact that another programmer could plough through code when debugging. My code is quite simple (for now) and I will be maintaining it .. . for now..
 
Back
Top Bottom