Weekday Function VB.NET

Class
Microsoft.VisualBasic.DateAndTime

Syntax
Weekday(datevalue, [dayofweek])

date

Use: Required

Data Type: Date or valid date

expression Any valid date expression

dayofweek

Use: Optional

Data Type: Constant of FirstDayOfWeek enumeration A constant indicating the first day of the week

Return Value
Integer

Description
Determines the day of the week of a given date

Rules at a Glance

  • The default for dayofweek is FirstDayOfWeek.Sunday.
  • To determine the day of the week, think of the day specified by dayofweek as day 1, and the value returned by the function as indicating the day relative to day 1. Then, for example, if the return value of WeekDay is 2, this specifies the day following dayofweek. A return value of 1 specifies dayofweek. A return value of 7 specifies the day before dayofweek.
  • The members of the FirstDayOfWeek enumeration are:
  • First Day Of Week enumeration

  • Passing a value of 0 as the dayofweek argument uses the system's locale settings to determine the first day of the week.

Example
Since the code:

Weekday(#3/26/2001#, FirstDayOfWeek.Sunday)

returns 2, the date 3/26/2001 is a Monday.

Programming Tips and Gotchas

  • If passing a date literal as datevalue, the Weekday function requires that all four digits of the year be present.
  • That the function's return value is relative to dayofweek makes it confusing to use the members of the FirstDayOfWeek enumeration to interpret the function's return value, to say the least. For example, the following expression evaluates to True if the day of the week is Tuesday:

If FirstDayOfWeek.Monday = WeekDay(CDate("10/1/96"), _
FirstDayOfWeek.Monday) Then

VB .NET/VB 6 Differences
The names of the named parameters of the function have changed from date and firstdayofweek
in VB 6 to datevalue and dayofweek in VB .NET.

WeekdayName Function

Class
Microsoft.VisualBasic.DateAndTime

Syntax
WeekdayName(Weekday, [abbreviate [, FirstDayOfWeekValue]])

Weekday Use: Required Data Type: Long The ordinal number of the required weekday, from 1 to 7 abbreviate Use: Optional Data Type: Boolean Specifies whether to return the full day name or an abbreviation FirstDayOfWeekValue Use: Optional Data Type: FirstDayOfWeek constant

Member of the FirstDayOfWeek enum indicating the first day of the week

Return Value
A String

Description
Returns the name of the day

Rules at a Glance

  • Weekday must be a number between 1 and 7, or the function generates an ArgumentException error.
  • The default value of abbreviate is False.
  • For a list of the members of the FirstDayOfWeek enumeration, see the "Weekday Function" entry.
  • The default value of FirstDayOfWeekValue is FirstDayOfWeek.Monday.

Programming Tips and Gotchas

  • Since Weekday is an integer, to determine the name of the day of a particular date, combine WeekDayName with a call to the WeekDay function, as the following code fragment shows
  • sDay = WeekDayName(Weekday(dDate, iFirstDay), _ bFullName, iFirstDay)

    Note that the value of the FirstDayOfWeek argument must be the same in the calls to both functions for WeekdayName to return an accurate result.

  • Unlike the Weekday function, the WeekdayName function behaves predictably. For example, if you ask for the name of the first day of the week when the week starts on Monday, the function returns Mon or Monday. If you ask for the fifth day of the week for a week that starts on Sunday, the function returns Thu or Thursday.

While...End While Statement

Syntax

While condition [statements] End While condition condition Use: Required Data Type: Numeric or String An expression evaluating to True or False statements Use: Optional Program statements to execute while condition remains True

Description
Repeatedly executes program code while a given condition remains True

Rules at a Glance

  • A Null condition is evaluated as False.
  • If condition evaluates to True, the program code between the While and End While statements is executed. After the End While statement is executed, control is passed back up to the While statement where condition is evaluated again. When condition evaluates to False, program execution skips to the first statement following the End While statement.
  • You can nest While...End While loops within each other.

Programming Tips and Gotchas
The While...End While statement remains in Visual Basic for backward compatibility only. In our opinion, it has been superceded by the much more flexible Do...Loop statement.

VB .NET/VB 6 Differences
In VB 6, the ending statement that accompanies the While construct is Wend; in VB .NET, it is End While.

With Statement

Syntax

With object [statements] End With object object Use: Required Data Type: Object A previously declared object variable or user-defined type statements Use: Optional Program code to execute against object

Description
This statement is used to execute a series of statements on an object without having to qualify each statement with the object name.

Rules at a Glance

  • The single object referred to in the With statement remains the same throughout the code contained within the With...End With block. Therefore, only properties and methods of object can be used within the code block without explicitly referencing the object. All other object references within the With...End With statement must start with a fully qualified object reference.
  • With statements can be nested, as long as the inner With statement refers to a subobject or a dependent object of the outer With statement.
  • A member of object is referenced within a With block by omitting the object name and simply including a period and the member name.

Example

Programming Tips and Gotchas
It is important that you do not include code within the With statement block that forces execution to branch out of the block. Similarly, do not write code that forces program flow to jump into a With block.
Both the With and its associated End With statement must be executed, or you will generate unpredictable results.

WithEvents Keyword

Syntax
Dim|Private|Public WithEvents objVarName As objectType

objVarName

Use: Required

Data Type: String

The name of any object variable that refers to an object that exposes events

objectType

Use: Required

Data Type: Any object type other than the generic

Object The ProgID of a referenced object

Description
The WithEvents keyword informs VB that the object being referenced exposes events for which you
intend to provide event handlers.
When you declare an object variable using WithEvents, an entry for the object variable is placed in the code window's drop-down Object List, and a list of the events available to the object variable is placed in the code window's drop-down Procedures List. You can then write code event handlers for the object variable.

Rules at a Glance

  • An object-variable declaration using the WithEvents keyword can only be used in an object or class module.
  • An object-variable declaration using the WithEvents keyword should only be placed in the Declarations section of the object module.
  • Any ActiveX object or class module that exposes events can be used with the WithEvents keyword. WithEvents is only valid when used to declare an object variable.
  • You cannot use WithEvents when declaring a generic Object type.
  • Unlike other variable declarations, the As keyword is mandatory.
  • 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.
  • You cannot use the WithEvents keyword in a local variable declaration.
  • If objectType does not expose any events, the WithEvents statement generates a compiler error.

Example
The following example demonstrates how to trap and respond to the events within an ADO recordset.
An object variable is declared using the WithEvents keyword in the declarations section of a form module. This allows you to write event-handling code for the ADO's built-in events, in this case the
FetchProgress event. (The FetchProgress event allows you to implement a Progress Bar control that shows progress in populating the recordset.)

Programming Tips and Gotchas

  • Placing the object-variable declaration that uses the WithEvents keyword in a procedure does not add the object variable name to the module's Object List. In other words, the events fired from the object would only have scope in the procedure and therefore cannot be handled.
  • Even if you declare the object variable using the Public keyword, the events fired by the object only have scope in the module in which the object variable has been declared.
  • Because you cannot use WithEvents to declare a generic Object type, WithEvents can only be used with early-bound object references. In other words, objects must have been added to the project using the References dialog box. Without this prior knowledge of the object's interface, VB has no chance of knowing how to handle events from the object.
  • If the object you are referencing doesn't expose any public events, you will generate a compile-time error, "This object does not raise Events."

VB .NET/VB 6 Differences
In VB 6, object variables in a code module could not be declared with WithEvents. In VB .NET, this
restriction has been lifted

Write Procedure

Class
Microsoft.VisualBasic.FileSystem

Named Arguments
No

Syntax
Write(filenumber, output)

filenumber

Use: Required

Data Type: Integer

Any valid file number

output

Use: Required

Data Type: Object (Any)

A comma-delimited list of expressions or a ParamArray to be written to the file

Description
Writes data to a sequential file

Rules at a Glance

  • output can contain multiple expressions delimited with either a comma, a semicolon, or a space.
  • output can also be an Object array containing values to be written to the file indicated by filenumber.
  • The following table describes how the Write procedure handles certain types of data, regardless of the locale, to allow files to be read universally:

Rules at a GlanceRules at a Glance

  • The Write procedure automatically does the following:

Delimits data fields with a comma
Places quotation marks around string data

Programming Tips and Gotchas
The structured data written to a file using the Write procedure is most suited to being read back from
the file using the Input procedure.

VB .NET/VB 6 Differences

  • The VB 6 Write statement requires that output be a comma-delimited list of literal values or variables. The VB .NET WriteLine procedure also allows outputlist to be a parameter array.
  • Calling the VB 6 Write statement with a single comma in place of outputlist forces a blank line to be written to the file. VB .NET requires that you call the WriteLine procedure.
  • The VB 6 Write statement allowed a # symbol to precede the filenumber argument. In the VB .NET Write procedure, this usage is not permitted.

WriteLine Procedure

Class
Microsoft.VisualBasic.FileSystem

Named Arguments
No

Syntax
WriteLine(filenumber, [output])

filenumber

Use: Required

Data Type: Integer Any valid file number

output

Use: Optional

Data Type: Object (Any)

A comma-delimited list of expressions or a ParamArray to be written to the file

Description
Writes data to a sequential file and then adds a line-feed character combination

Rules at a Glance

  • output can contain multiple expressions delimited with either a comma, a semicolon, or a space.
  • output can also be an Object array containing values to be written to the file indicated by filenumber.
  • The following table describes how the WriteLine procedure handles certain types of data, regardless of the locale, to allow files to be read universally:
  • Rules at a Glance

  • The WriteLine procedure automatically does the following:

Delimits data fields with a commaPlaces quotation marks around string dataInserts a new-line character (Chr(13) + Chr(10)) after the last item in output is written to the file

  • If the output argument is omitted, WriteLine writes a blank line to the file designated by filenumber.

Programming Tips and Gotchas
The structured data written to a file using the WriteLine procedure is most suited to being read back
from the file using the Input procedure.

VB .NET/VB 6 Differences
The WriteLine procedure is new to VB .NET as a partial replacement for the VB 6 Write procedure.

« Previous Topics
Val Function
Ubound Function
Tab Function
Next Topics »
Year Function


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

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

VB.NET Topics