Verifying enum value is valid in VB.NET - windows

My English may not be very good, so please excuse me if I don't make sense.
I am tasked with verifying an Enum contains a incrementing value.
The code looks like this:
Public Enum AccountMessageDescriptor
AccountInvalid = 1001
AccountReviewPending=1002
...
InvalidOperationOnAccount=1382
End Enum
Each Enum value has a description (Custom) attribute also. The file and description attribute is used elsewhere... It looks like the programmers update the enums and use the custom attribute for help messages instead of resource files etc with a new integer value...
For example, another error may be added as:
NewEnumItem=1383
I need to make sure (during build time we have a custom MSBuild task that does lots of other processing) if the enum is incrementing sequentially and that its not already in use.
Someone suggested using a collection, check if the enum value already exists and if not insert it.
I was thinking of just iterating through and checking if the current value is +1 the previous value (so its sequential and it can't be already in use because it has to always be +1). Does this seem flawed or am I not understanding something about .NET enums?
Emmi

If you set the first value and then exclude all following values they will be incremental. eg...
Public Enum testenum
first = 1001
second
third
End Enum
So second will be 1002 third will be 1003 etc etc...
If for some reason you do need to skip certain values then you can force the increment to start at a higher value eg..
Public Enum testenum
first = 1001
second
third
fourth = 2000
fifth
End Enum
So in this example fourth will be 2000 and fifth will be 2001

To iterate through an enum, you could try the following:
For Each amd As AccountMessageDescriptor In [Enum].GetValues(GetType(AccountMessageDescriptor))
'Do your thing here
Next
I'm not enterily sure if this way the values are sequential, but a quick test could sort this out for you.
Or you can store all the values in an array and check if the values are sequential:
Dim allAMD As AccountMessageDescriptor() = DirectCast([Enum].GetValues(GetType(AccountMessageDescriptor)), AccountMessageDescriptor())
'Loop through array and check with previous

Related

My flow fails for no reason: Invalid Template Language? This is what I do

Team,
Occasionally my flow fails and its enough test it manually to running again. However, I want to avoid that this error ocurrs again to stay in calm.
The error that appears is this:
Unable to process template language expressions in action 'Periodo' inputs at line '0' and column '0': 'The template language function 'split' expects its first parameter to be of type string. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#split for usage details.'.
And it appears in 2 of the 4 variables that I create:
Client and Periodo
The variable Clientlooks this:
The same scenario to "Periodo".
The variables are build in the same way:
His formula:
trim(first(split(first(skip(split(outputs('Compos'),'client = '),1)),'indicator')))
His formula:
trim(first(split(first(skip(split(outputs('Compos'),'period = '),1)),'DATA_REPORT_DELIVERY')))
The same scenario to the 4 variables. 4 of them strings (numbers).
Also I attached email example where I extract the info:
CO NIV ICE REFRESCOS DE SOYA has finished successfully.CO NIV ICE REFRESCOS DE SOYA
User
binary.struggle#mail.com
Parameters
output = 7
country = 170
period = 202204012
DATA_REPORT_DELIVERY = NO
read_persistance = YES
write_persistance = YES
client = 18277
indicator_group = SALES
Could you give some help? I reach some attepmpts succeded but it fails for no apparent reason:
Thank you.
I'm not sure if you're interested but I'd do it a slightly different way. It's a little more verbose but it will work and it makes your expressions a lot simpler.
I've just taken two of your desired outputs and provided a solution for those, one being client and the other being country. You can apply the other two as need be given it's the same pattern.
If I take client for example, this is the concept.
Initialize Data
This is your string that you provided in your question.
Initialize Split Lines
This will split up your string for each new line. The expression for this step is ...
split(variables('Data'), '\n')
However, you can't just enter that expression into the editor, you need to do it and then edit in in code view and change it from \\n to \n.
Filter For 'client'
This will filter the array created from the split line step and find the item that contains the word client.
`contains(item(), 'client')`
On the other parallel branches, you'd change out the word to whatever you're searching for, e.g. country.
This should give us a single item array with a string.
Initialize 'client'
Finally, we want to extract the value on the right hand side of the equals sign. The expression for this is ...
trim(split(body('Filter_For_''client''')[0], '=')[1])
Again, just change out the body name for the other action in each case.
I need to put body('Filter_For_''client''')[0] and specify the first item in an array because the filter step returns an array. We're going to assume the length is always 1.
Result
You can see from all of that, you have the value as need be. Like I said, it's a little more verbose but (I think) easier to follow and troubleshoot if something goes wrong.

A magic number overkill

I have a piece of code in which I call a function with argument of a string value. The string value is taken from a string array using the index.
if (validateRejectionCategory(rejectionData[0])) {
......
}
In this case is 0 still considered a magic number? Should I abstract 0 to a variable like REJECTION_CATEOGRY_POSITION? Does the function name not make it clear enough that the value would be a rejectionCategory?
Let me know your thoughts please.
In general extracting every numerical value to a constant is a bad habit. You have to maintain balance as with all things in life. Here it's clear that you want to validate the first element of rejectionData. Most uses of 0, 1, 2 are not magic, but are used in an algorithm.
You usually want to extract numeric values that aren't easily explainable in the context of code.
E.g. if your code looks like this in some main-like file:
app.modalTimeooutSeconds = 3.0
it's not really more readable to rewrite it as
let timeoutSeconds = 3.0
app.modalTimeoutSeconds = timeoutSeconds
unless you want to have a config file that are constants are stored, but you may not have enough constants to support having such a file.
Everything is context dependent and there are no good answers.

Pentaho: How to rollup time

I have time for example I have in records time written in this way: 00:02:24 or 15:22:45, and now I want to make another column (hours), where can be values for example for time 02:43:22 is value 2, or for time 23:22:14, is value 23 and so on. But I don't know how can I do that, I tried number range, but unsuccessfully.
Here is a picture, how i want to be:
Thanks.
You can use the Modified JavaScript Value step.
I do not know which type is your Time.
If it is a String, a var Hour = Time.substr(0,2); will do.
If it is a Date, use var Hour = Time.getHour();.
If the type is something else, then convert in a String first.
To do this:
drag-and.drop the step Modified JavaScript Valueand link it to the data flow provider (in the example a Data grid).
edit this step and add your script. (Note that you can quickly add the input variable with a double click. Note also that clicking on the Transformation Function in the left menu gives you the list of available function additional to the Javascript built-in collection).
Click on the Get variable button, keep the variable you need (here Hour), and define/redefine its type (here String).
That's done: OK and preview.
If needed, adapt to the type of your input flow. For example Hour = Hour+'' to force a type conversion into a String.

Protobuf: enum with default value

In google's Protocol Buffers, I use large enums and I have to assign each integer value explicitly:
enum Function {ProcessLibrary=0;
RotateLeft=1;
RotateRight=2;
...}
This is very annoying and ugly. Is there a way to avoid these integer values in the code?
something like:
enum Function {ProcessLibrary;
RotateLeft;
RotateRight;
...}
No, basically. This is deliberate to prevent huge errors when adding / removing enums, and to allow for non-contiguous enums.
In most real-world cases where the list of names is already defined elsewhere, you can write a 5 line script to add =n onto each - heck, a spreadsheet calculation and "fill down" would go a long way to it - paste names into the first column, copy the generated lines out of the second.

Create Random Integer Based on Id in Ruby

I have a scenario where I need to generate 4 digit confirmation codes for individual orders. I don't want to just do random codes due to the off chance that two exact codes would be generated near the same time. Is there a way to use the id of each order and generate a 4 digit code from that? I know I am going to eventually have repetitive codes with this but it will be ok because they will not be generated around the same time.
Do you really need to base the code on the ID? Four digits only gives you ten thousand possible values so you could generate them all with a script and toss them in a database table. Then just pull a random one out of the database when you need it and put it back in when you're done with it.
Your code table would look like this:
code: The code
uuid: A UUID, a NULL value here indicates that this code is free.
Then, to grab a code, first generate a UUID, uuid, and do this:
update code_table
set uuid = ?
where code = (
select code
from code_table
where uuid is null
order by random()
limit 1
)
-- Depending on how your database handles transactions
-- you might want to add "and uuid is null" to the outer
-- WHERE clause and loop until it works
(where ? would be your uuid) to reserve the code in a safe manner and then this:
select code
from code_table
where uuid = ?
(where ? is again your uuid) to pull the code out of the database.
Later on, someone will use the code for something and then you just:
update code_table
set uuid = null
where code = ?
(where code is the code) to release the code back into the pool.
You only have ten thousand possible codes, that's pretty small for a database even if you are using order by random().
A nice advantage of this approach is that you can easily see how many codes are free; this lets you automatically check the code pool every day/week/month/... and complain if the number of free codes fall below, say, 20% of the entire code space.
You have to track the in-use codes anyway if you want to avoid duplicates so why not manage it all in one place?
If your order id has more than 4 digits, it is theoreticly impossible without checking the generated value in a array of already generated values, you can do something like this:
require 'mutex'
$confirmation_code_mutex = Mutex.new
$confirmation_codes_in_use = []
def generate_confirmation_code
$confirmation_code_mutex.synchronize do
nil while $confirmation_codes_in_use.include?(code = rand(8999) + 1000)
$confirmation_codes_in_use << code
return code
end
end
Remember to clean up $confirmation_codes_in_use after using the code.

Resources