VB.NET DivRem Example – Code Sample Syntax
VB.NET DivRem Example – Code Sample Syntax
Purpose: – Illustrates using VB.NET 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 |
---|
Step 1: Click Visual Basic to Cut-n-paste code into clsDivRem.vb
Imports System.Math Public Class clsDivRem Public Sub 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 ' '******************************************************************************** ' This example is from http://idealprogrammer.com Console.WriteLine("Example #1: DivRem(dividend as int32, divisor as int32, byRef remainder as int32)") 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) 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)") Dim int64Dividend As Int64 = 7 Dim int64Divisor As Int64 = 5 Dim int64Quotient As Int64 = 0 Dim int64Remainder As Int64 = 0 int64Quotient = DivRem(int64Dividend, int64Divisor, 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() End Sub End Class |
Step 2: Click Visual Basic to Cut-n-paste code into Module1.vb
Module Module1 Sub Main() Dim myclsDivRem As New clsDivRem myclsDivRem.Main() End Sub End Module |
Prerequistes:
- Install Visual Basic (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.vb
Instructions:
- Use Visual Basic 2010 Express or Standard Edition
- Create new project;
- Click File/New Project
- Select Console Application Template
- Select Visual Basic for Language
- name of project could be VBNET_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 Visual Basic in code in step 1 above to copy code into clsDivRem.vb
- Click on Visual Basic in code in step 2 above to copy code into Module1.vb
- Click green arrow or press F5 to run program