If some variable(s) change value then save this new value in new variable(s) - massif

There is some fixed list of variables that I need to keep track of, and if their values have changed, then save their new value in a new variable. How can this be done most compactly ?
it is too long to use the if statement to check each variable, and save the new value of all variables whose value has changed once.

Related

How to dynamically pass a variable (of string type) value to another variable name in vb.net?

I am a former VFP programmer and was amazed by some powerful technics of VFP such as declaring dynamically a variable and assign to it a name from another string variable. I am looking for how to do the same in vb.net. I search but most solutions suggest array or list where I could not use the specific meaningful name of the variables.
I have a list of many variables in a table and for each variable I would like to dynamically declare a variable that have the name of the variable and assign to to it the variable value. Below is just 5 % of the full list
partial list of the variables
I can declare all the variable one by one but I would prefer a shorter way if any.
How could you assist me?
I have not tried anything.
You can't dynamically create variables in a strongly typed language. (You could create a dynamically typed variable, but that's not what you're looking for.)
Take a look at the Dictionary class, which is a collection of pairs (name and value), like a classic "hash array" or an object in JavaScript.
Edit. When you create a dictionary in VB.Net, you specify the datatype of the key (usually a String) and the datatype of the value. If all your values are Integers, yo do something like: Dim myDict As New Dictionary (Of String, Integer). If you really need to store different classes of objects (using the same dictionary), you may do: Dim myDict As New Dictionary (Of String, Object) (but you'll lose type safety).

VB.NET IIF Statement

In VB.NET I am trying to use SQL Parameter to send data to SQL Server Database, I am trying to convert string to decimal with following code if the value of the datagridrow is nothing then send 0 or use conversion to decimal argument.
SQL.Addparam("#amt", IIf(IsNothing(DataGridView2.Rows(index).Cells(7).Value), 0, CDec(DataGridView2.Rows(index).Cells(7).Value)))
(SQL and Addparam is called from class I created)
But with this code I get error "System.InvalidCastException: 'Conversion from string "" to type 'Decimal' is not valid.'"
I need help on this please.
Thank you
The specific issue is clearly that your "empty" cell does not actually contain a null reference, i.e. Nothing, but rather an empty String. You would need to test for an empty String as well as or instead of Nothing. If your cell might contain either Nothing, an empty String or a Decimal value, this should work:
Dim cellValue = DataGridView2.Rows(index).Cells(7).Value
SQL.Addparam("#amt",
If(String.IsNullOrEmpty(CStr(cellValue)),
Decimnal.Zero,
CDec(cellValue)))
Note the use of If rather than IIf and an actual Decimal value for the zero. String.IsNullOrEmpty will detect Nothing or an empty String in a single call.
That's really not the best solution though.
It appears that you are using an unbound grid and then looping through the rows and saving each row individually. That is really the wrong way to go. What you should be doing is creating a DataTable with the appropriate schema, populating it from the database if required, binding it to the grid, performing the required edits and then just saving the whole DataTable in one go with a single call to Update on a data adapter.
If you do that then an "empty" cell will contain DBNull.Value and ADO.NET will automatically save NULL to your database. If you don't want NULL values in a column then just set the AllowDBNull property to False and the DefaultValue property to whatever you want instead.
To create the DataTable schema, you can call Fill on a data adapter to do that and populate with data, or you can call FillSchema to not retrieve any data. Alternatively, you can just build the schema yourself.

Storing the set variable value of one macro inside a set variable outside the macro in which it is defined

I have the following macro that sets a variable with a string .How can i use this value inside another macro's set variable?Where somename="Outpatient"
#macro(deepak $somename)
#set($somename1=$somename)
#end
If this macro is called at any point in your template, before the second macro wants to use that variable, it should still be available. So simply
#set $var = $somename1
should work. Or simply use directly $somename1 of course.
From velocity user guide:
Once a value has been assigned to a variable, you can reference the variable anywhere in your HTML document.

When declaring an object, how to use variable as the name?

Can I use the value inside of a variable to name an object? If so, what is the syntax for that declaration?
Every object has to be given a distinct name. Dim XXXX as NEW_ARRAY is named XXXX. Since I will have 10,000 objects, I would like to automate the creation of those objects using a loop. But, if the object creation loop uses the same name over and over again, I understand that the object would overwrite itself 9999 times. There would only be one instance of that object.
I would like to use the value of a variable as that distinct name. However, I think that typing in a name of a variable in the name position while declaring the object would only overwrite the first object over and over again.
Is there a specific syntax that puts the VALUE of a previously declared variable as the name of an object?
So, XXXX= 1111
Dim "XXXX" as NEW_ARRAY would be named 1111
Then XXXX=2222
Dim "XXXX" as NEW_ARRAY would be named 2222
Then XXXX=3333
Dim "XXXX" as NEW_ARRAY would be named 3333.
Object doesn't have name, variable has. #StevenDoggart already elaborate about that for you.
If the question was "Can I use the value inside of a variable to name a new variable?" Short answer is "no, you can't". There is no point to have such specific feature in .NET.
You can achieve similar behavior using dictionary, as suggested in many posts asking about dynamic variable name in .NET. You can see dictionary key as variable name, and dictionary value as variable value. As far as I can see, what you can do given such dynamic variable name feature exists in .NET, can be done equally well using dictionary.

Values don't change in for loop

I have a List of structure's in my VB.NET program, and I'm looping over them, and change the values of the objects in the list, as follows
Dim retvals As List(Of SomeStruct) = parser.RetrieveData(new_path)
For i As Integer = 0 To retvals.Count - 1 Step 1
dim temp as SomeStruct = retvals(i)
temp.A = GetValueForA()
temp.B = GetValueForB()
Next
When I look into my List of structs after this loop, none of the values were overwritten. Why? I thought that I had references in my list, so if I change reference A to a struct, then reference B to the same struct should see the changes?
What am I missing?
Structs are a value type, whereas classes are a reference type. If you were to be using SomeClass instead of SomeStruct this code would work as you expect.
In the scenario of using a Class, retvals would be a list of pointers to class objects. "dim temp as ..." creates a copy of the pointer, and setting temp.A to something changes the object the pointer points to. So when the code is done, the pointer in the retval still points to the same object which has now been changed.
However, when using structs no pointers are stored. retvals is simply a list of the values stored in your structs. The "dim temp as ..." creates a copy of the entire struct. You modify this struct by changing temp.A, but the original struct never changes since you only modified a copy of it.
I realized what the problem was, the Structure in .NET is a value type, meaning the contents will get copied into the List. So changing the temp variable will not change the original in the List
I fixed it by using a class instead, which is a reference type.