I don't know how to describe it thoroughly in the title, but I need to set a permanent value of a variable/flag once a process has return true and maybe set some flag in the program itself the value rather than saving it to database. And once that variable/flag has already have that value then the program won't run the process again and just use the value. Is it possible? I'm using VB.Net. I can't use the database because database can be overridden and change values by using query. Thanks in advance!
-
Having more background as to *why* the value needs to remain constant across every execution might give insight in to what solution is most appropriate. TLDR; what are you trying to accomplish? – cwharris Oct 04 '17 at 05:58
-
@ChristopherHarris I'm trying to disable my system when the expired date comes, and since I don't host the database of my system, I can't just make another field in the database and store values in there for a reason that it can be manually overridden by executing query. – Rich Oct 04 '17 at 06:14
-
Because your concerns that value can be updated, you should save it where no body except you have access to. Best approach is run process which produce value every time, but if you it is very "expensive". You can create web API where you store your value. – Fabio Oct 04 '17 at 06:21
-
1If your code is running on the machine of the would be "attacker" then all you can do is place road blocks. I.e. a moderately skilled user could decompile your code and remove the variable check. The only 100% way is to keep the code you want to protect on *machines that you control*. Outside of that, you're probably best off purchasing a licensing component rather than trying to reimplement one yourself. – Damien_The_Unbeliever Oct 04 '17 at 07:38
4 Answers
You can simply use binary/XML serialization in a file to save the state of that variable through your program. Every time you restart your app you can access the value from that file to get its current state.
You can look at this example - http://www.centerspace.net/examples/nmath/csharp/core/binary-serialization-example.php
Basically, you will not save the value in the database but in a file. Anyways you need to persist the value somewhere.

- 2,872
- 1
- 23
- 38
-
Value can be updated in the file in same manner as it can be updated in database, what OP concern about. – Fabio Oct 04 '17 at 06:16
-
Some ways below
You did not specify if you are afraid that your application or another one could change the value
How I would do it
My ideas below
1)You could use an xml file for example and zip a copy of it with a strong password. Every time you update the first xml you will update also the encrypted zipped xml.You can use a FileSystemWatcher and capture any file change, so if something/someone has changed the file you just get a new copy from the zip
2)You can store the value in the DB and add a trigger to prevent delete/update
for example
-- delete trigger
CREATE TRIGGER Function_Value_Deleted
ON [dbo].[FunctionsValueTb]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (
SELECT [Flag] FROM deleted
)
BEGIN
ROLLBACK;
RAISERROR ('Record deletion is not allowed...', 16, 1);
END
END
*You can use also use THROW rather than RAISERROR
**Do the same for the insert and update actions
***You can also store the value into a log table or send an email

- 244
- 1
- 8
I found myself in a situation quite similar to yours a couple of days ago.
In the end, I decided to use the settings functionaly provided by .NET: it is easy to use and maintain, and so far it has given me good results.
Yo can see here what I am talking about: Best practice to save application settings in a Windows Forms Application
That thread refers to C# but is easily applicable for VB.NET: I just had to follow the same steps in order to add the Settings file:
Right click on the project in Solution Explorer, choose Properties. Select the Settings tab, click on the hyperlink if settings doesn't exist. Use the Settings tab to create application settings. Visual Studio creates the files Settings.settings and Settings.Designer.settings that contain the singleton class Settings inherited from ApplicationSettingsBase
And then, from my code, I use the settings like this:
Dim lastExecDate As Date = My.Settings.LastSuccessfulExecution
lastExecDate = lastExecDate.AddDays(1)
// Perform my next execution and do other stuff
My.Settings.LastSuccessfulExecution = lastExecDate
My.Settings.Save()
Next time you retrieve the parameter LastSuccessfulExecution, it will have the updated value.
One more remark, as stated in the post that I linked above:
Note that you need to set the scope property of your settings. If you select Application scope then Settings.Default.< your property > will be read-only
Finally, I see that you are using this to store the expiration date of a product, so you don't want the user messing around with it. According to this post, the actual values of the parameters are stored in an Application Data user folder. It is somehow obfuscated since it is not that easy to find and besides it contains a hash on its name... I don't know if that is well hidden enough for you.

- 776
- 6
- 10
If you want the value only to exist in memory when the application is running then you can use the main thread of the application and use:
int slotData = randomGenerator.Next(1, 200);
//to set the data
Thread.SetData(Thread.GetNamedDataSlot("SomeDataKey"), slotData);
//to get the data
int newSlotData = (int)Thread.GetData(Thread.GetNamedDataSlot("SomeDataKey"));
Or you can use the Windows Registry if your app only runs on Windows, if not then you would have to write the value/object to a file and read it from there.

- 4,088
- 1
- 16
- 21
-
I don't want the value exist only in memory, I want to set some flag in my application that if the expiry date comes, it won't be usable, and I want this to not be editable by any user. That's why I don't want this to edit by user. – Rich Oct 04 '17 at 08:15