Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Storing a value that corresponds to a users selection

Posted on 2011-09-07
11
Medium Priority
?
242 Views
Last Modified: 2012-05-12
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.

0
Comment
Question by:rocky050371
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 10

Accepted Solution

by:
CSIPComputing earned 1336 total points
ID: 36494963
Storing which days of the week the user selected is simple.

Use Binary.

Assign each day a bit (i.e. bit 0=Mon, Bit 1=Tues, Bit 2=Wed, Bit3=Thurs, Bit 4 = Fri, Bit 5 = Sat, Bit 6=Sun), then you can "Translate" as follows: (see the code snippet)

Monday, Tuesday, Thursday becomes 1 + 2 + 8 = 11
Wed, Fri, Sun becomes 4 + 16 + 64 = 84

You can encode any number of days selected, and of course DE-code them in this way.

The every "N" weeks is just a number anyway... i.e. 1 = Every week, 2 = Every fortnight, 4=Every month etc.

So using 2 integers, you can encode your entire schedule.
Hope that makes sense!

n/a  Sun  Sat Fri Thu Wed Tue Mon
128   64   32  16  8   4   2   1

Open in new window

0
 

Author Comment

by:rocky050371
ID: 36495084
How would you recommend I decode, say for example 11 is in the database, do it via a select case?
0
 
LVL 10

Expert Comment

by:CSIPComputing
ID: 36495231
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 40
ID: 36500526
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

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 664 total points
ID: 36500549
Sorry, hit Submit by mistake before finishing.

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

Open in new window

To use it, you create a variable. The nice thing is that Intellisense will display the possible values when you try to assign to it:
Dim selection As Days
selection = Days.Monday Or Days.Friday Or Days.Sunday

Open in new window

An enumeration is an integer by default. Depending on how your environment is set, you might be able to use it as any other Integer. If you have errors, simple use is as CInt(selection).

To decode it, you use And in an If or a Select Case:
If CBool(selection And Days.Monday) Then
    Debug.WriteLine("Monday was selected")
End If

Open in new window

Or you could use that nice little feature of flagged enumerations:
MessageBox.Show(selection.ToString("g"))

Open in new window

The result will be "Sunday, Monday, Friday".
0
 

Author Comment

by:rocky050371
ID: 36501546
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

0
 
LVL 10

Expert Comment

by:CSIPComputing
ID: 36501650
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!
0
 
LVL 40
ID: 36504002
@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.

0
 

Author Comment

by:rocky050371
ID: 36504942
So I take it I simply check my stored value (46 as a integer) against each flag element top determine what has been selected

0
 
LVL 40
ID: 36506846
Yes you do.
0
 
LVL 10

Assisted Solution

by:CSIPComputing
CSIPComputing earned 1336 total points
ID: 36510827
Sounds like a plan :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question