Partition Function VB.NET

Class
Microsoft.VisualBasic.Interaction

Syntax
Partition(number, start, stop, interval)

Return Value
A String containing the range within which number falls

Description
Returns a string that describes which interval contains the number

Rules at a Glance

  • start must be greater than or equal to 0.
  • stop cannot be less than or equal to start.
  • Partition returns a range formatted with enough leading spaces so that there are the same number of characters to the left and right of the colon as there are characters in stop, plus one. This ensures that the interval text will be handled properly during any sort operations.
  • If number is outside of the range of start, the range reported is:

: (start - 1)

If number is outside the range of end, the range reported is:

(last_end_range + 1):

  • If interval is 1, the range is number:number, regardless of the start and stop arguments. For example, if interval is 1, number is 100, and stop is 1000, Partition returns 100: 100.
  • If any of its arguments are Null, Partition returns a Null.

Example

The code:

produces the following output:
-5 is in interval : -1
0 is in interval 0: 9
5 is in interval 0: 9
10 is in interval 10: 19
15 is in interval 10: 19
20 is in interval 20: 29
25 is in interval 20: 29
30 is in interval 30: 39
35 is in interval 30: 39
40 is in interval 40: 49
45 is in interval 40: 49
50 is in interval 50: 59
55 is in interval 50: 59
60 is in interval 60: 69
65 is in interval 60: 69
70 is in interval 70: 79
75 is in interval 70: 79
80 is in interval 80: 89
85 is in interval 80: 89
90 is in interval 90: 99
95 is in interval 90: 99
100 is in interval 100:100
105 is in interval 101:
110 is in interval 101:

Programming Tips and Gotchas

  • The Partition function is useful in creating histograms, which give the number of integers from a collection that fall into various ranges.

VB .NET/VB 6 Differences

  • The Partition function is new to VB .NET.

Pi Field

Class
System.Math

Syntax
Math.PI

Return Value
A Double containing the approximate value of the irrational number pi

Description
This field returns the approximate value of the irrational number pi. In particular:

Math.PI = 3.14159265358979

Rules at a Glance
This is a Shared member, so it can be used without creating any objects.

VB .NET/VB 6 Differences
The Pi field is new to VB .NET.

Pmt Function

Class
Microsoft.VisualBasic.Financial

Syntax
Pmt(rate, nper, pv[, fv[, due]])

A value indicating when payments are due. EndOfPeriod (0) indicates that payments are due at the end of the payment period; BegOfPeriod (1) indicates that payments are due at the beginning of the period. If omitted, the default value is 0.

Return Value
A Double representing the monthly payment

Description
Calculates the payment for an annuity based on periodic, fixed payments and a fixed interest rate. Anannuity can be either a loan or an investment.

Rules at a Glance

  • rate is a percentage expressed as a decimal. For example, an interest rate of 1% per month is expressed as 0.01.
  • If fv is omitted, the default value of 0 (reflecting the complete repayment of a loan) is used.
  • For pv and fv, cash paid out is represented by negative numbers; cash received is represented by positive numbers.
  • If due is omitted, the default value of 0 (reflecting payments at the beginning of each period) is used.

Programming Tips and Gotchas

  • rate and nper must be calculated using payment periods expressed in the same units. For example, if nper reflects the total number of monthly payments, rate must be the monthly interest rate.

Pow Function

Class
System.Math

Syntax
result = Math.Pow(x, y)

x, y
Use: Required
Data Type: Double

Return Value
A Double that is x (the base) raised to the power y (the exponent)

Description
This is a generalized exponential function; it returns the result of a number raised to a specified power.

Rules at a Glance
This is a Shared member, so it can be used without creating any objects.

VB .NET/VB 6 Differences
The Pow function is new to the .NET Framework.

PPmt Function

Class
Microsoft.VisualBasic.Financial

Syntax
PPmt(rate, per, nper, pv[, fv[, due]])

A value indicating when payments are due. It can be either DueDate.EndOfPeriod (or 0), for payments due at the end of the period, or DueDate.BegOfPeriod (or 1), for payments due at the beginning of the period. The default value is DueDate. EndOfPeriod.

Return Value
A Double representing the principal paid in a given payment

Description
Computes the payment of principal for a given period of an annuity, based on periodic, fixed paymentsand a fixed interest rate. An annuity is a series of fixed cash payments made over a period of time. It can be either a loan payment or an investment.

Rules at a Glance

  • The value of per can range from 1 to nper.
  • If pv and fv represent liabilities, their value is negative; if they represent assets, their value is positive.
  • If fv is omitted, its default value of 0 is used.
  • If due is omitted, the default value of 0 (reflecting payments at the beginning of each period) is used.

Programming Tips and Gotchas

  • rate and nper must be expressed in the same time unit. That is, if nper reflects the number of monthly payments, rate must be the monthly interest rate.
  • The interest rate is a percentage expressed as a decimal. For example, if nper is the total number of monthly payments, an annual percentage rate (APR) of 12% is equivalent to a monthly percentage rate of 1%. The value of rate is therefore .01.

Print, PrintLine Procedures

Class
Microsoft.VisualBasic.FileSystem

Syntax
Print(filenumber, [outputlist( )])
PrintLine(filenumber, [outputlist( )])

filenumber
Use: Required
Data Type: Integer
Any valid file number.

outputlist can be either a scalar variable, a list of comma-delimited expressions, or a parameter array. Its comma-delimited expressions or parameter array can include the following:

Spc( n)
Use: Optional
Insert n space characters before expression.

Tab( n)
Use: Optional
Position the insertion point either at the next print zone (by omitting n) or at column number
(n).

Description
Outputs formatted data to a disk file opened for Append or Output.

Rules at a Glance

  • Print and PrintLine are identical, except that PrintLine advances to the next line after printing.
  • The Tab(n) argument does not actually insert any tab characters (Chr(9)); instead, it fills the space from the end of the last expression to column n (or to the start of the next print zone) with space characters.
  • The Print procedure uses the locale settings of the current system to format dates, times, and numbers, using the correct separators.
  • outputlist can be either a comma-separated list of expressions or a parameter array.

Example
The following code shows how to use the Print procedure to write to a file using both a commaseparated
list of arguments and a parameter array:

Programming Tips and Gotchas
You may find that sequential data files written using the Print procedure are misinterpreted by the Input function. For heavily structured sequential data, you may get better results with the Write procedure, which ensures that all fields are correctly delimited.

VB .NET/VB 6 Differences

  • In VB 6, the Print statement requires a # symbol in front of filenumber. In VB .NET, this usage is not supported.
  • In VB 6, the final argument in outputlist, charpos, allows you to specify the starting character position of the next output. In VB .NET, however, this argument is not supported.

Private Statement

Syntax
Private [WithEvents] varname[([subscripts])] [As [New] _
type] [, [WithEvents] varname[([subscripts])] _
[As [New] type]] . . .

A keyword that denotes the object variable, varname, can respond to events triggered from within the object to which it refers.

Denotes varname as an array and specifies the number and extent of array dimensions.

Used to automatically create an instance of the object referred to by the object variable, varname.

Description
Used at module level to declare a private variable and allocate the relevant storage space in memory. Private can also be used with procedures and class modules.

Rules at a Glance

  • A Private variable's scope is limited to the module in which it is created.
  • WithEvents is only valid when used to declare an object variable. The WithEvents keyword informs VB that the object being referenced exposes events. When you declare an object variable using WithEvents, an entry for the object variable is placed in the code window's Object List, and a list of the events available to the object variable is placed in its Procedures List. You can then write code in the object variable's event handlers in the same way you write other more common event handlers.
  • There is no limit to the number of object variables that can refer to the same object using the WithEvents keyword; they will all respond to that object's events.
  • You cannot create an array variable that uses the WithEvents keyword.
  • The New keyword cannot be used in the same object-variable declaration as WithEvents. This is because WithEvents is designed to trap event notifications that would ordinarily be inaccessible to a Visual Basic program. Consequently, WithEvents can only be used when defining an instance of an existing object.
  • The subscripts argument has the following syntax:

upperbound [, upperbound]

For example:
Private strNames(10, 15)

defines a two-dimensional array with 11 elements in the first coordinate and 16 elements in the second coordinate. Thus, the first element is strNames(0,0), and the last element is strNames(10,15).

  • Using the subscripts argument, you can declare up to 60 multiple dimensions for the array.
  • To declare an array with no specified size, use commas with no integers between them, as in:
  • Private sNames( )

Private sThings(,)

You can set or change the number of elements of an array using the ReDim statement.

  • The New keyword is used only when declaring an object variable. For example:
  • Private oEmployee As Employee
  • Employee = New Employee
  • or:
    Private oEmployee As New Employee

  • The New keyword can only be used with early-bound objects.
  • datatype may be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, String, a user-defined type, or an object type.

Programming Tips and Gotchas

  • All variables created at procedure level are Private by default. That is, they do not have scope outside of the procedure in which they are created.
  • A new type of scope was introduced in Visual Basic 5.0. The Friend scope is halfway between Public and Private. It is useful in situations where Private is too restricting and Public is too open. For more information, refer to the "Friend Statement" entry.
  • You should note that when you use the New keyword to declare an object variable, its class constructor is fired when the object variable is declared.
  • The WithEvents keyword cannot be used with local variables whose scope is limited to a function or procedure.

VB .NET/VB 6 Differences

  • In VB 6, the subscripts argument takes the form:

[lowerbound To] upperbound [, [lowerbound To] upperbound]

  • VB .NET, however, does not allow you to set the lower bound of an array.
  • In VB 6, an array whose number of elements are declared in advance is a fixed array; it cannot be redimensioned. In VB .NET, all arrays are dynamic and can be redimensioned.
  • In VB .NET, variables declared with the New keyword on the same line as the Private statement are no longer created when their first reference is encountered. Hence, whereas in VB 6, declaring an object variable using a statement such as:

Dim oObj As New MyApp.SomeObject could interfere with object destruction, in VB .NET this is not the case.

  • In VB 6, the type argument can be Currency. The Currency data type, however, is not supported by VB .NET.

Property Statement

Syntax

Gives the property scope through all procedures in all modules in the project. If used within a createable class module, the property is also accessible from outside the project. Public, Private, and Friend are mutually exclusive.

Restricts the scope of the property to those procedures within the same module. Public, Private, and Friend are mutually exclusive.

Only valid in a class module, it gives the property scope to all modules within a project, but not to modules outside the project. Public, Private, and Friend are mutually exclusive.

Specifies that the property is the default property. Must have both a Get and a Set block.

Indicates that the property is read-only. Must have only a Get block. (If you try to write a Set block, VB will generate a syntax error.)

Indicates that the property is write-only. Must have only a Set block. (If you try to write a Get block, VB will generate a syntax error.)

Overloads
Indicates that more than one declaration of this function exists (with different argument signatures).

Overrides
For derived classes, indicates that the function overrides the function by the same name (andargument signature) in the base class.

Overridable
Indicates that the function can be overridden in a derived class.

NotOverridable
Indicates that the function cannot be overridden in a derived class.

MustOverride
Indicates that the function must be overridden in a derived class.

In a derived class definition, indicates that calls to derived class members that are made through a base class ignore the shadowed implementation.

Shared
A shared function is callable without creating an object of the class. It is, in this strange sense, shared by all objects of the class. These are also called static functions.

A comma-delimited list of variables to be passed to the property as arguments from the calling procedure.

Description
Declares a class property

Rules at a Glance

  • Property procedures are Public by default.
  • The Friend keyword is only valid within class modules. Friend procedures are accessible to all procedures in all modules and classes within a project, but are not listed in the class library for that project. Therefore, they cannot be accessed from projects or applications outside the defining application.
  • Properties and procedures defined using the Friend keyword cannot be late bound.
  • The Default keyword can only be used in the case of parameterized properties. Typically, these are properties that either return collection objects or are implemented as property arrays.
  • By default, arguments are passed to the property procedures by value (ByVal).
  • type defines not only the data type returned by the property, but also the data type of the value to be assigned to the property.
  • A Property Get procedure is very similar to a function: the value returned by the property is indicated by assigning that value to a variable whose name is the same as the property.
  • In a Property Set procedure, the value being assigned to the property is represented by the keyword Value. Its data type is represented by the As type clause.
  • If an Exit Property statement is executed, the Property procedure exits and program execution immediately continues with the statement following the call to the property. Any number of Exit Property statements can appear in a Property procedure.

Programming Tips and Gotchas

  • You should protect the values of properties by defining a Private variable to hold the internal property value and to control the updating of the property by outside applications through the Property statement, as the following template describes:

    • Otherwise, if the variable used to store a property value is public,

    its value can be modified arbitrarily by any application that accesses the

    class module containing the property.

    • Typically, arglist need be specified only in the case of property arrays. For example:

    • The class constructor is typically used to initialize property values to their default settings.

    VB .NET/VB 6 Differences
    The syntax for declaring properties in VB .NET is significantly different from the syntax in VB 6. Some
    of the differences include:

    • VB 6 includes individual Property Get (to retrieve a property value), Property Let (to assign a property value), and Property Set (to assign a reference to a property value) statements. VB .NET replaces this with a single Property...End Property construct.
    • In VB 6, all values—including the property values themselves—passed to property statements are expressed as parameters. In VB .NET, the value to be assigned to a property is represented by the Value keyword, rather than by a formal parameter.
    • In VB 6, because Property Set, Property Let, and Property Get procedures are separate, standalone constructs, it is possible to expose property procedures with mixed visibility (a private Property Let procedure, for example, and a public Property Get procedure). In VB.NET, because the Property statement defines the visibility of the property as a whole, mixed visibility is not supported.

    Protected Keyword

    Description
    Used to declare classes and their members.When the Protected keyword is used to modify a member declaration, the member being declared has direct access scope to the class module in which the member is declared, as well as to all derived classes in all projects. However, as far as object access is concerned, the member is considered Private; that is, it can only be accessed within the declaring class. (See the upcoming example.) Declaring a class module as Protected limits all of the class' members to Protected access (or stronger if the member has further specific access restrictions).

    Example
    Suppose we declare the following variable in a class module named Class1: Protected sProtectedVar As String Then within Class1 or any of its derived classes in any project, we can use the variable directly, as in:

    On the other hand, the following code, located in a form module, is illegal:
    Dim c as New Class1
    c.sProtectedVar = "Donna"

    VB .NET/VB 6 Differences
    The Protected keyword is new to VB .NET.

    Public Statement

    Syntax
    [Overrides] [Shadows] Public [WithEvents] varname[([subscripts])] _
    [As [New] type] [, [WithEvents] _
    varname[([subscripts])] [As [New] type]] ...

    In a derived class definition, indicates that a variable overrides a similar variable in a base class.

    In a derived class definition, indicates that calls to derived class members that are made through a base class ignore the shadowed implementation.

    A keyword that denotes the object variable, varname, can respond to events triggered from within the object to which it refers.

    The name of the variable, which must follow Visual Basic naming conventions.

    Denotes varname as an array and specifies the dimensions and number of elements of the array.

    Used to automatically create an instance of the object referred to by the object variable, varname.

    Description
    Used at module level to declare a public variable and allocate the relevant storage space in memory. A Public variable has both project-level scope—that is, it can be used by all procedures in all modules in the project—and, when used in a Class module, it can have scope outside the project. The Public keyword also applies to procedures and class modules.

    Rules at a Glance

    • The behavior of a Public variable depends on where it is declared, as the following table shows:

      TABLE

      TABLE

    • WithEvents is only valid when used to declare an object variable.
    • There is no limit to the number of variables that can refer to the same object using the WithEvents keyword; they will all respond to that object's events.
    • You cannot create an array variable that uses the WithEvents keyword.
    • The New keyword cannot be used in the same object-variable declaration as WithEvents.
    • The subscripts argument has the following syntax:

      upperbound [, upperbound]

    • Using the subscripts argument, you can declare up to 60 dimensions for the array.
    • To declare an array with no specified size, use commas with no integers between them, as in:

      Public sNames( )
      Public sThings(,)
      You can set or change a number of elements of an array using the ReDim statement.

    • The New keyword denotes that a new instance of the object will be created when the first reference to the object is made. Use of the New keyword therefore negates the need to use the Set statement.
    • You cannot use the New keyword to declare any of the following: variables of any intrinsic data type (the New keyword is for use with object variables only); instances of dependent objects (a dependant object is one that can only be created from a method or property in another object; a dependent object is not publicly createable); or a variable that uses the WithEvents argument.

    Programming Tips and Gotchas

    • Instead of declaring a variable as Public within either a form or class module, proper objectoriented programming techniques dictate that you should create a Property procedure that assigns and retrieves the value of a private variable.
    • Always use Option Explicit at the beginning of a module to prevent misnamed variables from causing hard to find errors.

    VB .NET/VB 6 Differences

    • In VB 6, the subscripts argument takes the form:

      [lowerbound To] upperbound [, [lowerbound To] upperbound]

    • VB .NET, however, does not allow you to set the lower bound of an array.
    • In VB 6, an array whose number of elements are declared in advance is a fixed array; it cannot be redimensioned. In VB .NET, all arrays are dynamic and can be redimensioned.
    • In VB .NET, variables declared with the New keyword on the same line as the Public statement are no longer created when their first reference is encountered. Hence, whereas in VB 6, declaring an object variable using a statement such as:

      Dim oObj As New MyApp.SomeObjectcould interfere with object destruction, in VB .NET this is not the case.

    • In VB 6, the type argument can be Currency. The Currency data type, however, is not supported by VB .NET.

    PV Function

    Class

    Microsoft.VisualBasic.Financial

    Syntax
    PV(rate, nper, pmt[, fv [, due]])

    Return Value
    A Double specifying the present value of an annuity

    Description

    Calculates the present value of an annuity (either an investment or loan) based on a regular number of future payments of a fixed value and a fixed interest rate. The present value is the current value of a future stream of equal cash flows discounted at some fixed interest rate.

    Rules at a Glance

    • The time units used for the number of payment periods, the rate of interest, and the payment amount must be the same. In other words, if you state the payment period in months, you must also express the interest rate as a monthly rate and the amount paid per month.
    • The rate per period is stated as a fraction of 100. For example, 10% is stated as .10. If you are calculating using monthly periods, you must also divide the rate per period by 12. For example, 10% per annum equates to a rate per period of .00833.
    • The fv argument indicates the future value or cash balance after the last payment. The default is 0, since that reflects the value of a loan after the final payment.
    • Payments made against a loan or added to the value of savings are expressed as negative numbers.
    • The due argument states whether the payment is made at the start of a period or at the end (the default value).

    Programming Tips and Gotchas

    Make sure that nper, rate, and pmt all reflect values for an identical time period. For example, if pmt represents a monthly payment, rate should represent the monthly interest rate, rather than an annual interest rate.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

VB.NET Topics