C-Sharp DivRem Example – Code Sample Syntax

C-Sharp DivRem Example – Code Sample Syntax

Purpose: – Illustrates using .

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)

Parameters Description
dividend32 int32
divisor32 int32
remainder32 byRef int32

Result Data Type Description
int32 quotient

Syntax2:
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:

  1. Install C-Sharp (Express or Standard Edition)
  2. Install SQL Server Express
  3. Download Northwind and pubs Database
  4. Attach Northwind Database to Databases in Sql Express
  5. 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:
C-Sharp Syntax Functions DivRem

  1. Use C-Sharp 2010 Express or Standard Edition
  2. Create new project;
    • Click File/New Project
    • Select Console Application Template
    • Select C-Sharp for Language
    • name of project could be C-Sharp_Syntax.
  3. Add New folder named “Functions”
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: Functions
  4. Add New subfolder named “MathFunctions”
    • Right-click Functions folder in solution explorer;
    • add new folder;
    • name of folder could be: MathFunctions
  5. Add Class Named clsDivRem to MathFunctions folder
    • Right-click MathFunctions folder;
    • add new item;
    • Select class
    • Class name could be clsDivRem
  6. Click on cs in code in step 1 above to copy code into clsDivRem.cs
  7. Click on cs in code in step 2 above to copy code into Module1.cs
  8. Click green arrow or press F5 to run program