IDataObject Interface VB.NET

Namespace
System.Windows.Forms

Createable
No

Description
The IDataObject interface is used by the Clipboard for data-transfer operations. It is also used for drag-and-drop operations. An instance of the IDataObject interface is returned by the Clipboard object's GetData method.

Public Instance Methods
Those methods marked with a plus sign (+) are covered in more detail in their own entries:
GetData +
GetDataPresent +
GetFormats +
SetData

VB .NET/VB 6 Differences
The IDataObject interface is new to VB .NET.

IDataObject.GetData Method

Class
System.Windows.Forms.IDataObject

Syntax
GetData(format [,autoconvert])

format
Use: Required Data Type: String or Type object

Field member of the DataFormats class (see later for more information on this) or a Type object representing the format of the data

autoconvert Use: Optional Data Type: Boolean

True to convert the data to the specified format

Return value
An Object that contains Clipboard data in the specified format

Description
Retrieves the data of the given format, optionally converting the data format

Rules at a Glance

  • The format argument can be one of the following string values:

DataFormats.Bitmap
DataFormats.CommaSeparatedValue
DataFormats.Dib
DataFormats.Dif
DataFormats.EnhancedMetafile
DataFormats.FileDrop
DataFormats.Html
DataFormats.Locale
DataFormats.MetafilePict
DataFormats.OemText
DataFormats.Palette
DataFormats.PenData
DataFormats.Riff
DataFormats.Rtf
DataFormats.Serializable (a format that encapsulates any type of Windows Forms object)
DataFormats.StringFormat
DataFormats.SymbolicLink
DataFormats.Text
DataFormats.Tiff
DataFormats.UnicodeText
DataFormats.WaveAudio

  • If format is a string, the autoconvert argument can be supplied in the method call.
  • If the GetData method cannot find data in format, it attempts to convert the data to format.

If the data cannot be converted to the format, or if the data was stored with autoconvert set to False, the method returns Nothing.

Example
The following example extracts the text that is currently on the Clipboard:

IDataObject.GetDataPresent Method

Class
System.Windows.Forms.IDataObject

Syntax
GetDataPresent(format [,autoconvert])

format Use: Required Data Type: String or Type object

Field member of the DataFormats class (see later for more information on this) or a Type
object representing the format of the data for which to search

autoconvert Use: Optional Data Type: Boolean

True to convert the data to the specified format

Return value
Boolean value indicating whether the Clipboard holds data of the specified format or of a format that can be converted to format

Description
Returns a Boolean value indicating whether the Clipboard holds data of the specified format or of a format that the present data can be converted to

Rules at a Glance

  • The format argument can be one of the following string values:

DataFormats.Bitmap
DataFormats.CommaSeparatedValue
DataFormats.Dib
DataFormats.Dif
DataFormats.EnhancedMetafile
DataFormats.FileDrop
DataFormats.Html
DataFormats.Locale
DataFormats.MetafilePict
DataFormats.OemText
DataFormats.Palette
DataFormats.PenData
DataFormats.Riff
DataFormats.Rtf
DataFormats.Serializable (a format that encapsulates any type of Windows Forms object)
DataFormats.StringFormat
DataFormats.SymbolicLink
DataFormats.Text
DataFormats.Tiff
DataFormats.UnicodeText
DataFormats.WaveAudio

  • If format is a string, the autoconvert argument can be supplied in the method call. A value of False indicates that the function should determine whether the data stored by the IDataObject instance is in the format defined by format; a value of True, whether the data stored by the IDataObject instance is in or is capable of being converted to the format defined by format.

Example
The following code will inform us whether the Clipboard contains a bitmap:

IDataObject.GetFormats Method

Class
System.Windows.Forms.IDataObject

Syntax
GetDataFormats([autoconvert])

autoconvert
Use: Optional Data Type: Boolean

True to retrieve all formats that the Clipboard data is associated with or can be converted to; False to retrieve only native data formats

Return Value
A String array containing a list of all supported formats

Description
Retrieves a list of all the formats that the Clipboard data is associated with or can be converted to

Rules at a Glance
The elements in the array returned by the method can take any of the following values:
DataFormats.Bitmap
DataFormats.CommaSeparatedValue
DataFormats.Dib
DataFormats.Dif
DataFormats.EnhancedMetafile
DataFormats.FileDrop
DataFormats.Html
DataFormats.Locale
DataFormats.MetafilePict
DataFormats.OemText
DataFormats.Palette
DataFormats.PenData
DataFormats.Riff
DataFormats.Rtf
DataFormats.Serializable (a format that encapsulates any type of Windows Forms object)
DataFormats.StringFormat
DataFormats.SymbolicLink
DataFormats.Text
DataFormats.Tiff
DataFormats.UnicodeText
DataFormats.WaveAudio

IEEERemainder Function

Class
System.Math

Syntax
Math.IEEERemainder(x, y)
x and y

Use: Required Data Type: Double

Return Value
Returns the remainder after dividing x by y

Description
Returns a Double whose value is the remainder after dividing x by y

Example
Math.IEEEremainder(4, 3) ' Returns 1

Rules at a Glance

  • VB has a built-in Mod operator that also returns the remainder upon division.
  • The IEEERemainder function complies with the remainder operation as defined in Section 5.1 of ANSI/IEEE Std 754-1985; IEEE Standard for Binary Floating-Point Arithmetic; Institute of Electrical and Electronics Engineers, Inc; 1985.

Programming Tips and Gotchas
This is a Shared member, so it can be used without creating any objects.

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

If...Then...Else Statement

Syntax

Or, you can use the single line syntax:
If condition Then [statements] [Else elsestatements]

condition
Use: Required Data Type: Boolean

An expression returning either True or False or an object type

statements Use: Optional Program code to be executed if condition is true condition-n Use: Optional Same as condition elseifstatements Use: Optional Program code to be executed if the corresponding condition-n is True elsestatements Use: Optional

Program code to be executed if the corresponding condition or condition-n is False

Description
Executes a statement or block of statements based on the Boolean (True or False) value of an Expression

Rules at a Glance

  • If condition is True, the statements following the If are executed.
  • If condition is False and no Else or ElseIf statement is present, execution continues with the corresponding End If statement. If condition is False and ElseIf statements are present, the condition of the next ElseIf is tested. If condition is False and an Else is present, the statements following the Else are executed.
  • In the block form, each If statement must have a corresponding End If statement. ElseIf statements do not have their own End If. For example:
  • ElseIf and Else are optional, and any number of ElseIf and Else statements can appear in the block form. However, no ElseIf statements can appear after an Else.
  • condition can be any statement that evaluates to True or False.
  • If condition returns Null, it will be treated as False.
  • You can also use the If statement to determine object types by using the TypeOf and Is keywords, as follows:

If TypeOf objectname Is objecttype Then

  • statements are only optional in the block form of If. However, statements are required when using the single-line form of If in which there is no Else clause.

Programming Tips and Gotchas

  • You can use the single-line form of the If statement to execute multiple statements, which you can specify by delimiting the statements using colons. However, single-line If statements are hard to read and maintain and should be avoided for all but the simplest of situations.
  • In situations where you have many possible values to test, you will find the Select Case statement much more flexible, manageable, and readable than a bunch of nested If statements.
  • You will come across situations in which very large blocks of code have to be executed based one or more conditions. In these—and in all situations—you should try to make your code as readable as possible, not only for other programmers, but for yourself, since you will probably

need to revisit the code several months down the line. For example, consider a scenario in which, at the beginning of a procedure, a check is made to see if the procedure should be executed under a given set of circumstances. You have the choice of surrounding the whole code with an If...Then...End If construct, like this:

Or you can instead check for a False condition and, if found, exit the subroutine:

The latter alternative can be much easier to read.

  • Indentation is important for the readability of If, and especially nested If, statements. The set of statements within each new If...Else...EndIf block should be indented. When using the Visual Studio IDE, you can simply select a block of code and press the tab key to indent the complete selected block. The following example shows correctly indented code:
  • You may often run into code such as:

If iSuccess Then ...

where iSuccess is an Integer variable. The statement works because Visual Basic interprets all non-zero values as equal to Boolean True and all zero values as equal to Boolean False.
However, if Option Strict is on, statements such as these will generate a compiler error, since VB .NET will not automatically convert the iSuccess integer to the Boolean required by the If statement.

  • Logical comparison operators can be included in the condition expression, allowing you to make decisions based on the outcome of more than one individual element. The most common of these is And and Or. You can create conditions like:

Visual Basic will evaluate only as much of the condition as is necessary to determine the truth value of the condition, a method of evaluation known as short-circuiting. Concretely, VB will stop evaluating multiple expressions like with Or once the first condition is True, and will stop evaluating conditions with And once the first condition is False. For instance, in the previous case, if x = 0, the statement (1/x = 5) will not be evaluated, which is a good thing, since it would produce an error.

  • In Beta 2, VB .NET appears to use conditional short-circuiting in evaluating If clauses. If a portion of a compound expression calls an external routine or function, that subexpression will be evaluated even if the result of the expression is known. This is because calls to other routines may modify program variables. The following code illustrates this conditional shortcircuiting:

The two compound expressions with Or are functionally identical, as are the two compound expressions using And. The difference is that the second set of If statements call the DivBy0 function, whereas the first set perform the division by 0 inline, as part of condition. The first divisions by do not raise an error, since the second subexpression in each case is never evaluated. The second divisions by 0, though, do raise an error, since they involve a call to another routine.

  • The If statement is also used with objects to determine if an object variable is Nothing. This is done using the Is operator:

If Not (objectvar Is Nothing) Then

VB .NET/VB 6 Differences
VB 6 does not use short circuiting. If condition consists of multiple logical expressions, each is evaluated whether or not the value of condition is already known. VB .NET relies on conditional short-circuiting to evaluate multiple expressions in condition only until the value of condition is known, unless any expressions involve calls to functions that might modify program variables.

IIf Function

Class
Microsoft.VisualBasic.Interaction

Syntax
IIf(expression, truepart, falsepart)

expression Use: Required Data Type: Boolean

Expresion to be evaluated

truepart Use: Required Data Type: Any value or expression

Expression or value to return if expression is True

falsepart
Use: Required Data Type: Any value or expression

Expression or value to return if expression is False

Return Value
The value or result of the expression indicated by truepart or falsepart

Description
Returns one of two results, depending on whether expression evaluates to True or False

Rules at a Glance

  • IIf will evaluate only one of truepart or falsepart, depending on the value of expression.
  • The IIf function is the equivalent of:
  • If testexpression Then
  • Return truepart
  • Else
  • Return falsepart

End If

  • truepart and falsepart can be a variable, constant, literal, expression, or the return value of a function call.

Programming Tips and Gotchas

  • The IIf function is ideal for very simple tests resulting in single expressions. If you really feel the need, IIf function calls can be nested; however, your code can very quickly become difficult to read. The following code fragment illustrates the use of a nested IIf function:

TABLE

TABLE

  • In previous versions of VB, developers tended to avoid the IIf function in favor of the If statement for all but the most simple uses because of its poor performance. In VB.NET, the performance of IIf has been improved significantly, although it remains significantly slower than an If statement. The average number of seconds required to call the IIf function a million times and to execute an If...ElseIf...Else...End If statement a million times under the two VB versions showed the following differences:
  • In other words, the performance of IIf from VB 6 to VB .NET has improved by 100%. At the same time, the function is over 300 times slower than an If statement under VB .NET!

Implements Keyword

Syntax
Implements interfacename.interfacemember

interfacename Use: Required Type: String literal

The name of the interface being implemented by a class

interfacemember
Use: Required Type: String literal

The name of the interface property, function, procedure, or event that is being implemented by
a class

Description
Indicates that a class member provides the implementation of a member defined in an interface

Rules at a Glance

  • The Implements keyword can only be used in a class module in which the Implements statement has been used to define an abstract base class that the class is to implement.
  • The Implements keyword follows the property, function, procedure, or event definition, and must be on the same line.
  • The class member implementing the interface member must be of the same type (property, function, procedure, or event) as the interface member, and its argument list and, in the case of functions and properties, return type must also be identical to that of the interface member.
  • Class members must implement all of the members declared in the interface.

VB .NET/VB 6 Differences

  • The Implements keyword is new to VB .NET. This means that the implementation of a property, function, procedure, or event does not have to use the name defined by the interface. This modifies the VB 6 practice, which requires that class members that implement an interface definition have the form interfacename_membername.
  • VB 6 does not allow derived classes to implement events defined in interfaces. VB .NET removes this restriction.

Implements Statement

Syntax
Implements InterfaceName [,InterfaceName][,...]

InterfaceName Use: Required Type: String literal

The name of the interface that a class implements

Description
The Implements statement specifies that you will implement an interface within the class in which the Implements statement appears.

Rules at a Glance

  • Implementing an interface or class means that the implementing class will provide code to implement every Public member of the implemented interface or class. If you fail to implement even a single Public member, an error will result.
  • The Implements statement cannot be used in a standard module; it is used only in class modules.
  • By convention, interface names begin with a capital I, as in IMyInterface.

Example

Programming Tips and Gotchas

  • If you do not wish to support a procedure from the implemented class, you must still create a procedure declaration for the implemented procedure. However, you can simply raise an error using the special error constant Const E_NOTIMPL = &H80004001 so a user will know that the member is not implemented in any meaningful way. Alternately, you can also raise a NotImplementedException exception
  • Interfaces, or abstract base classes, allow for greater coherence when developing in teams. For example, all developers could use a set of interfaces to produce controls and objects of a particular type without being constrained by implementation. That is, each developer would be free to implement a particular property or method in the way that he saw fit.
  • Maintaining compatibility across multiple versions dictates that interfaces should not change once they have been written and distributed. Any additional functionality required should be provided by defining additional interfaces.
  • VB .NET provides only single inheritance using the Inherits statement. However, by using interface-based inheritance with the Implements statement, you can in effect implement multiple inheritance.

VB .NET/VB 6 Differences

  • In VB 6, the Implements statement does not support events; any events publicly declared in an interface are ignored. VB .NET, on the other hand, allows derived classes to trap the events defined in interfaces.

Imports Statement

Syntax
Imports [aliasname = ] namespace

aliasname Use: Optional Type: String literal

The name by which the namespace will be referenced in the module

namespace Use: Required Type:

The name of the namespace being imported

Description
Imports namespaces and assemblies, making their classes and class members available to the current module

Rules at a Glance

  • A single Imports statement can import one namespace.
  • A module can have have as many Imports statements as needed.
  • Imports statements are used to import names from other projects and assemblies, as well as from namespaces in the current project.
  • Imports statements must be placed in a module before references to any identifiers (e.g., variables, classes, procedures, functions, etc.).
  • If aliasname is absent from an Imports statement, types in that namespace can be referenced without qualification.
  • If aliasname is present in an Imports statement, types in that namespace must be qualified with aliasname in order to be accessible.
  • The name aliasname must not be assigned to any other member within the module.

Programming Tips and Gotchas

  • In ASP.NET, a number of namespaces are imported automatically. These include System.Web and its child namespaces.
  • You do not use the Imports statement to import namespaces into an ASP.NET application. Instead, you can import a namespace into an ASP.NET application in a number of ways:
  • By creating an <add namespace> directive in a web.config configuration file. For example:

imports the System.IO namespace within the scope defined in the web.config file.
o By adding an @ Import directive to global.asax. For example:

<%@ Import namespace="System.IO" %

imports the System.IO namespace for the ASP.NET application.
o By adding an @ Import page directive. This has the same form as the global.asax directive, and must appear at the beginning of the page.



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