Who is this for?
Software Engineers, .Net Developers, hobbyists.
What does the title mean?
By dynamic application variables I am referring to having an easy string-lookup class to manage data. It is dynamic due to allowing any datatype that inherits from Object which is perfect for Object Oriented Programming (OOP).
C# (see-SHARP): C-Based .Net language
.Net (DOT-net): Microsoft Programming framework. .Net Applications get assembled into MSIL(Microsoft Intermediate Language) instead of into a Native Application
.Net Languages: C#, Visual Basic, F#, J#, Iron Python, Visual C++
What am I talking about?
My Vars.cs class (written in C#) Available here. Code is not the prettiest but it is very effective.
What can this be used for?
Creating scripting languages (variable inception), dynamic code logic, string based client variables with any data allowed for values.
Real examples of how this can be utilized?
Stopwatches, Command Lists, Console/Text commands, IntelliSense functionality, caching accessed file data as raw data or a Type.
Code usage examples
//code logic to benchmark here
TimeSpan timeDifference = DateTime.Now.Subtract((DateTime)Vars.Get("Stopwatch"));
int elapsedMS = (int)timeDifference.TotalMilliseconds;
Vars.Set("a", Vars.GetI("a" + 1));
Vars.Set("dataList", new List<object>());
List<object> dataList = (List<object>)Vars.Get("dataList");
The code is efficient (not flawless) but it is able to perform well in extremely quick reaction situations and even in multithreaded situations (although this was NOT made fully thread-safe). I used Vars as a stopwatch for a Socket class and it was able to work even when the variable was set and checked 1ms apart from two threads for dozens of Sockets at the same time.
What can be learned from this code?
Dictionary<string, object> is the container for the variables. With this simple concept you can do just about anything you want for variables or have any combination of them, even combinations that barely make sense.
The beauty in.Net and how this is even possible is that every Type is an Object and Object can contain object. The end result is that an object can be any combination of data structures or classes. .Net also keeps track of what Type the Object originally was, allowing you to ask “if(Vars.Get(“variable”) is Dog)”
With typecasting and proper use of Parenthesis ( ) you can more easily access complicated datasets without making copies or additional variables.
With this simple method of dynamic memory allocation you now have a powerful tool in your code-belt. Read and analyze the code and it should help in learning some abstract programming concepts. Go out and
Soon after writing this class I made a very similar class but instead of string based variables it was string based Events. I use it for Socket commands so I can simply do:
That function will then call OnCommand with extra data containing the packet information. I will likely post about that full class another time.
.Net is an extremely powerful framework and the provided code can be executed in any other .Net language. The class itself provides vast functionality due to its simplicity and using the same programming concepts in this class on other projects may help developers in creating shortcuts for themselves. Sometimes the best solution is the simple one.