vb.net application logic


I was wondering if someone is able to help with the logic for an application.
I am currently looking at an application within the company that has received an error.
The issue is to do with the original coding as a case statement that the parameter has exceeded.
The application is coded in vb.net and needs to work out the number of pallets that should be used based on a quantity of cartons (for shipments).
I spoke to the warehouse and they said the following logic is used:

14 -40 cartons = 1 pallet
41- 80 cartons = 2 pallets
81 – 120 cartons = 3 pallets

And so on ………

14 -40 cartons = 26 cartons = 1 pallet
41- 80 cartons = 39 cartons = 2 pallets
81 – 120 cartons = 39 cartons = 3 pallets

I did think about trying to divide but if for example you took:
3125 / 39 = 81 this is not correct

a case statement exists and a quantity of 79 pallets should be used.

I am not sure what the best way of coding this would be? Any help would be appreciated.



Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

seems that you need to divide with 40

3125 / 40 = 78,125

superior rounding to 79

As @Neuropsykopat already indicated, it would be something like this:

Dim cartonsPerPallet As Double = 40.0
Dim cartons As Integer = 3125

Dim pallets As Integer

pallets = CType(Math.Ceiling(cartons / cartonsPerPallet), Integer)

Console.WriteLine("Pallets required: {0}", pallets)

Open in new window

Pallets required: 79

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You can also use "Integer Division" to get the number of pallets like this:  
Dim cartonsPerPallet As Integer = 40.0
Dim cartons As Integer = 3125

Dim pallets As Integer = cartons \ cartonsPerPallet ' <-- Integer Division!

Open in new window

Note that the slash is a BACKSLASH "\", as opposed to normal division with a forward slash "/".

See "\ Operator (Visual Basic)":

    "Divides two numbers and returns an integer result."

    "The result is the integer quotient of expression1 divided by expression2, which discards any remainder and retains only the integer portion. This is known as truncation."
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
*Obviously this doesn't account for an extra pallet needed for any remaining cartons.

You can use the Mod operator to find out if there are any remaining cartons:

    Dim pallets As Integer = (cartons \ cartonsPerPallet) + IIf(cartons Mod cartonsPerPallet > 0, 1, 0)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pallets = CType(Math.Ceiling(cartons / cartonsPerPallet), Integer)

if (cartons Mod cartonsPerPallet >0)
pallets += 1;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.