rocky050371
asked on
Storing a value that corresponds to a users selection
I have a schedule where the user can specify a job should run every n weeks, It also allows them to specify on what days of the weeks they would like it to run ie
Sun, Mon & Tuesday
Ideally I want to save their selection as a single integer, is this possible if I assign each day a binary figure.
Sun, Mon & Tuesday
Ideally I want to save their selection as a single integer, is this possible if I assign each day a binary figure.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It depends on how you know what "day" it is in your code, and what you need to compare with.
I'm not a code guru (as you'll see below), but in my dim and distant past, I'd have done something like this, and I'm sure it can be translated into a Case statement:
Dim strToday as String = "Wed" (or whatever/however you know today)
Dim intSched as Integer = #value read from wherever you're storing it#
Dim intShouldIRun as Integer
If intSched >64 then
if strToday="Sun" then intShouldIRun=1
intSched=intSched-64
End If
If intSched >32 then
if strToday="Sat" then intShouldIRun=1
intSched=intSched-32
End If
If intSched >16 then
if strToday="Fri" then intShouldIRun=1
intSched=intSched-16
End If
..etc...
if intShouldIRun<>0 then
run my scheduled job
End If
I'm not a code guru (as you'll see below), but in my dim and distant past, I'd have done something like this, and I'm sure it can be translated into a Case statement:
Dim strToday as String = "Wed" (or whatever/however you know today)
Dim intSched as Integer = #value read from wherever you're storing it#
Dim intShouldIRun as Integer
If intSched >64 then
if strToday="Sun" then intShouldIRun=1
intSched=intSched-64
End If
If intSched >32 then
if strToday="Sat" then intShouldIRun=1
intSched=intSched-32
End If
If intSched >16 then
if strToday="Fri" then intShouldIRun=1
intSched=intSched-16
End If
..etc...
if intShouldIRun<>0 then
run my scheduled job
End If
An alternative would be to use a flagged Enum, typically in the declaration section of a Module:
<Flags()> Enum Days
Sunday = 1
Monday = 2
Tuesday = 4
Wednesday = 8
Thursday = 16
Friday = 32
Saturday = 64
End Enum
<Flags()> Enum Days
Sunday = 1
Monday = 2
Tuesday = 4
Wednesday = 8
Thursday = 16
Friday = 32
Saturday = 64
End Enum
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If the user selected Monday, Tuesday, Wednesday + Friday
Then I store away the corresponding total 46, when I retrieve how will I know those four days have been selected?
Not sure how (selection And Days.Monday) seems to work either
I appreciate the help though this is definitely heading in the right direction
Then I store away the corresponding total 46, when I retrieve how will I know those four days have been selected?
Not sure how (selection And Days.Monday) seems to work either
I appreciate the help though this is definitely heading in the right direction
OK, Using Jame's day definitions, and the total of 46, but my method of decoding (as I don't know CBOOL functions either),
46<64, so Saturday is NOT selected
46=>32 so Friday IS selected - Subtract 32
14<16 so Thursday is NOT selected
14=>8 so Wednesday IS selected - Subtract 8
6=>4 so Tuesday IS Selected - Subtract 4
2=>2 so Monday IS Selected - Subtract 2
0<1 so Sunday is NOT Selected.
Sorry I can't write the code for you, but hopefuilly the above makes sense!
46<64, so Saturday is NOT selected
46=>32 so Friday IS selected - Subtract 32
14<16 so Thursday is NOT selected
14=>8 so Wednesday IS selected - Subtract 8
6=>4 so Tuesday IS Selected - Subtract 4
2=>2 so Monday IS Selected - Subtract 2
0<1 so Sunday is NOT Selected.
Sorry I can't write the code for you, but hopefuilly the above makes sense!
@CISPComputing
The AND operator on a flagged enumeration returns an Integer. When the project is configured for strict type checking, you cannot use an Integer with If, you need a Boolean.
CBool is the VB method to convert to a Boolean. Similar to Convert.ToBoolean(), but more efficient because the conversion is compiled into the assembly instead of being called at runtime.
The AND operator on a flagged enumeration returns an Integer. When the project is configured for strict type checking, you cannot use an Integer with If, you need a Boolean.
CBool is the VB method to convert to a Boolean. Similar to Convert.ToBoolean(), but more efficient because the conversion is compiled into the assembly instead of being called at runtime.
ASKER
So I take it I simply check my stored value (46 as a integer) against each flag element top determine what has been selected
Yes you do.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER