C-Sharp DivRem Example – Code Sample Syntax
C-Sharp DivRem Example – Code Sample Syntax
Purpose: – Illustrates using C-Sharp DivRem Code Example.
Purpose:
takes an dividend, divisor, and remainder as Int32 or Int64
and returns quotient as same data type. Remainder is passed by
reference and it gets updated with the remainder when the function
executes.
Syntax:
DivRem(dividend32, divisor32, byRef remainder32)
DivRem(dividend32, divisor32, byRef remainder32)
Parameters | Description | dividend32 | int32 |
---|---|
divisor32 | int32 |
remainder32 | byRef int32 |
Result Data Type | Description | int32 | quotient |
---|
Syntax2:
DivRem(dividend64, divisor64, byRef remainder64)
DivRem(dividend64, divisor64, byRef remainder64)
Parameters | Description | dividend64 | int64 |
---|---|
divisor64 | int64 |
remainder64 | byRef int64 |
Result Data Type | Description | int64 | quotient |
---|
Quick Example | Dim int32Dividend As Int32 = 7 Dim int32Divisor As Int32 = 5 Dim int32Quotient As Int32 = 0 Dim int32Remainder As Int32 = 0 int32Quotient = DivRem(int32Dividend, int32Divisor, int32Remainder) int32Quotient Returns 1 int32Remainder Returns 2 |
---|
Step 1: Click cs to Cut-n-paste code into clsDivRem.cs
using System; public class clsDivRem { public void Main() { //******************************************************************************** // Purpose: takes an dividend, divisor, and remainder as Int32 or Int64 // and returns quotient as same data type. Remainder is passed by // reference and it gets updated with the remainder when the function // executes. // // ******************************************************************************* // Syntax: DivRem(dividend32, divisor32, byRef remainder32) // // Parameter1: dividend32 - int32 // // Parameter2: divisor32 - int32 // // Parameter3: remainder32 - byRef int32 // // Result: int32 - quotient // // ****************************************************************************** // Syntax2: DivRem(dividend64, divisor64, byRef remainder64) // // // Parameter1: dividend64 - int64 // // Parameter2: divisor64 - int64 // // Parameter3: remainder64 - byRef int64 // // Result: int64 - quotient // // Quick Example: Dim int32Dividend As Int32 = 7 // Dim int32Divisor As Int32 = 5 // Dim int32Quotient As Int32 = 0 // Dim int32Remainder As Int32 = 0 // int32Quotient = DivRem(int32Dividend, int32Divisor, int32Remainder) // int32Quotient // Returns 1 // int32Remainder // Returns 2 // //******************************************************************************** // This example is from http://idealprogrammer.com Console.WriteLine("Example #1: DivRem(dividend as int32, divisor as int32, byRef remainder as int32)"); Int32 int32Dividend = 7; Int32 int32Divisor = 5; Int32 int32Quotient = 0; Int32 int32Remainder = 0; int32Quotient = Math.DivRem(int32Dividend, int32Divisor, out int32Remainder); Console.WriteLine("Quotient=" + int32Quotient); // Returns 1 Console.WriteLine("Remainder=" + int32Remainder); // Returns 2 //write blank line to make output easier to read Console.WriteLine(" "); Console.WriteLine("Example #2: DivRem(dividend as int64, divisor as int64, byRef remainder as int64)"); Int64 int64Dividend = 7; Int64 int64Divisor = 5; Int64 int64Quotient = 0; Int64 int64Remainder = 0; int64Quotient = Math.DivRem(int64Dividend, int64Divisor, out int64Remainder); Console.WriteLine("Quotient=" + int64Quotient); // Returns 1 Console.WriteLine("Remainder=" + int64Remainder); // Returns 2 //write blank line to make output easier to read Console.WriteLine(" "); //Prevent console from closing before you press enter Console.ReadLine(); } } |
Step 2: Click cs to Cut-n-paste code into Module1.cs
Module Module1 Sub Main() Dim myclsDivRem As New clsDivRem myclsDivRem.Main() End Sub End Module |
Prerequistes:
- Install C-Sharp (Express or Standard Edition)
- Install SQL Server Express
- Download Northwind and pubs Database
- Attach Northwind Database to Databases in Sql Express
- Attach pubs Database to Databases in Sql Express
Notes:
- Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
- You can build a library of syntax examples by using same project over and over and just commenting out what you do not want to execute in Module1.cs
Instructions:
- Use C-Sharp 2010 Express or Standard Edition
- Create new project;
- Click File/New Project
- Select Console Application Template
- Select C-Sharp for Language
- name of project could be C-Sharp_Syntax.
- Add New folder named “Functions”
- Right-click project name in solution explorer;
- add new folder;
- name of folder could be: Functions
- Add New subfolder named “MathFunctions”
- Right-click Functions folder in solution explorer;
- add new folder;
- name of folder could be: MathFunctions
- Add Class Named clsDivRem to MathFunctions folder
- Right-click MathFunctions folder;
- add new item;
- Select class
- Class name could be clsDivRem
- Click on cs in code in step 1 above to copy code into clsDivRem.cs
- Click on cs in code in step 2 above to copy code into Module1.cs
- Click green arrow or press F5 to run program