Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .openpublishing.redirection.visual-basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,11 @@
{
"source_path_from_root": "/docs/visual-basic/sample-applications.md",
"redirect_url": "/samples/browse/?languages=vb"
},
{
"source_path_from_root": "/docs/visual-basic/language-reference/functions/ctype-function.md",
"redirect_url": "/dotnet/visual-basic/language-reference/operators/ctype-operator",
"redirect_document_id": true
}
]
}
2 changes: 1 addition & 1 deletion docs/framework/data/adonet/sql/linq/basic-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Because LINQ to SQL queries translate to Transact-SQL before they are executed o

## Casting

Implicit or explicit casts are enabled from a source CLR type to a target CLR type if there is a similar valid conversion within SQL Server. For more information about CLR casting, see [CType Function](../../../../../visual-basic/language-reference/functions/ctype-function.md) (Visual Basic) and [Type-testing and cast operators](../../../../../csharp/language-reference/operators/type-testing-and-cast.md). After conversion, casts change the behavior of operations performed on a CLR expression to match the behavior of other CLR expressions that naturally map to the destination type. Casts are also translatable in the context of inheritance mapping. Objects can be cast to more specific entity subtypes so that their subtype-specific data can be accessed.
Implicit or explicit casts are enabled from a source CLR type to a target CLR type if there is a similar valid conversion within SQL Server. For more information about CLR casting, see [CType Function](../../../../../visual-basic/language-reference/operators/ctype-operator.md) (Visual Basic) and [Type-testing and cast operators](../../../../../csharp/language-reference/operators/type-testing-and-cast.md). After conversion, casts change the behavior of operations performed on a CLR expression to match the behavior of other CLR expressions that naturally map to the destination type. Casts are also translatable in the context of inheritance mapping. Objects can be cast to more specific entity subtypes so that their subtype-specific data can be accessed.

## Equality Operators

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ End If
- [Conversion Summary](../keywords/conversion-summary.md)
- [Efficient Use of Data Types](../../programming-guide/language-features/data-types/efficient-use-of-data-types.md)
- [Troubleshooting Data Types](../../programming-guide/language-features/data-types/troubleshooting-data-types.md)
- [CType Function](../functions/ctype-function.md)
- [CType Operator](../operators/ctype-operator.md)
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ Each of the preceding lines of code explicitly casts the `Object` variable `o1`

- [Procedure Overloading](../../programming-guide/language-features/procedures/procedure-overloading.md)
- [Overload Resolution](../../programming-guide/language-features/procedures/overload-resolution.md)
- [CType Function](../functions/ctype-function.md)
- [CType Operator](../operators/ctype-operator.md)
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ms.assetid: 8a8fb553-a8ac-424e-8103-26eea25eaa71
- [CShort Function](type-conversion-functions.md)
- [CSng Function](type-conversion-functions.md)
- [CStr Function](type-conversion-functions.md)
- [CType Function](ctype-function.md)
- [CType Operator](../operators/ctype-operator.md)
- [CUInt Function](type-conversion-functions.md)
- [CULng Function](type-conversion-functions.md)
- [CUShort Function](type-conversion-functions.md)
Expand Down
2 changes: 0 additions & 2 deletions docs/visual-basic/language-reference/functions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ The topics in this section contain tables of the Visual Basic run-time member fu

[Type Conversion Functions](type-conversion-functions.md)

[CType Function](ctype-function.md)

## Related Sections

[Visual Basic Language Reference](../index.md)
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ Dim i3 As Integer = CInt(Math.Round(s)) ' Result: 174

If you need to convert to or from a string representation of a value in a format other than the one specified by your locale, you cannot use the Visual Basic type conversion functions. To do this, use the `ToString(IFormatProvider)` and `Parse(String, IFormatProvider)` methods of that value's type. For example, use <xref:System.Double.Parse%2A?displayProperty=nameWithType> when converting a string to a `Double`, and use <xref:System.Double.ToString%2A?displayProperty=nameWithType> when converting a value of type `Double` to a string.

## CType Function
## CType Operator

The [CType Function](ctype-function.md) takes a second argument, `typename`, and coerces `expression` to `typename`, where `typename` can be any data type, structure, class, or interface to which there exists a valid conversion.
The [CType Operator](../operators/ctype-operator.md) takes a second argument, `typename`, and coerces `expression` to `typename`, where `typename` can be any data type, structure, class, or interface to which there exists a valid conversion.

For a comparison of `CType` with the other type conversion keywords, see [DirectCast Operator](../operators/directcast-operator.md) and [TryCast Operator](../operators/trycast-operator.md).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Visual Basic language keywords and run-time library members are organized by pur
|Convert date to serial number.|<xref:Microsoft.VisualBasic.DateAndTime.DateSerial%2A>, <xref:Microsoft.VisualBasic.DateAndTime.DateValue%2A>|
|Convert decimal number to other bases.|<xref:Microsoft.VisualBasic.Conversion.Hex%2A>, <xref:Microsoft.VisualBasic.Conversion.Oct%2A>|
|Convert number to string.|<xref:Microsoft.VisualBasic.Strings.Format%2A>, <xref:Microsoft.VisualBasic.Conversion.Str%2A>|
|Convert one data type to another.|[CBool](../functions/type-conversion-functions.md), [CByte](../functions/type-conversion-functions.md), [CDate](../functions/type-conversion-functions.md), [CDbl](../functions/type-conversion-functions.md), [CDec](../functions/type-conversion-functions.md), [CInt](../functions/type-conversion-functions.md), [CLng](../functions/type-conversion-functions.md), [CSng](../functions/type-conversion-functions.md), [CShort](../functions/type-conversion-functions.md), [CStr](../functions/type-conversion-functions.md), [CType](../functions/ctype-function.md), <xref:Microsoft.VisualBasic.Conversion.Fix%2A>, <xref:Microsoft.VisualBasic.Conversion.Int%2A>|
|Convert one data type to another.|[CBool](../functions/type-conversion-functions.md), [CByte](../functions/type-conversion-functions.md), [CDate](../functions/type-conversion-functions.md), [CDbl](../functions/type-conversion-functions.md), [CDec](../functions/type-conversion-functions.md), [CInt](../functions/type-conversion-functions.md), [CLng](../functions/type-conversion-functions.md), [CSng](../functions/type-conversion-functions.md), [CShort](../functions/type-conversion-functions.md), [CStr](../functions/type-conversion-functions.md), [CType](../operators/ctype-operator.md), <xref:Microsoft.VisualBasic.Conversion.Fix%2A>, <xref:Microsoft.VisualBasic.Conversion.Int%2A>|
|Convert date to day, month, weekday, or year.|<xref:Microsoft.VisualBasic.DateAndTime.Day%2A>, <xref:Microsoft.VisualBasic.DateAndTime.Month%2A>, <xref:Microsoft.VisualBasic.DateAndTime.Weekday%2A>, <xref:Microsoft.VisualBasic.DateAndTime.Year%2A>|
|Convert time to hour, minute, or second.|<xref:Microsoft.VisualBasic.DateAndTime.Hour%2A>, <xref:Microsoft.VisualBasic.DateAndTime.Minute%2A>, <xref:Microsoft.VisualBasic.DateAndTime.Second%2A>|
|Convert string to ASCII value.|<xref:Microsoft.VisualBasic.Strings.Asc%2A>, <xref:Microsoft.VisualBasic.Strings.AscW%2A>|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ Indicates that a conversion operator (`CType`) converts a class or structure to
- [Widening](widening.md)
- [Widening and Narrowing Conversions](../../programming-guide/language-features/data-types/widening-and-narrowing-conversions.md)
- [How to: Define an Operator](../../programming-guide/language-features/procedures/how-to-define-an-operator.md)
- [CType Function](../functions/ctype-function.md)
- [CType Operator](../operators/ctype-operator.md)
- [Option Strict Statement](../statements/option-strict-statement.md)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ Indicates that a conversion operator (`CType`) converts a class or structure to
- [Narrowing](narrowing.md)
- [Widening and Narrowing Conversions](../../programming-guide/language-features/data-types/widening-and-narrowing-conversions.md)
- [How to: Define an Operator](../../programming-guide/language-features/procedures/how-to-define-an-operator.md)
- [CType Function](../functions/ctype-function.md)
- [CType Operator](../operators/ctype-operator.md)
- [Option Strict Statement](../statements/option-strict-statement.md)
- [How to: Define a Conversion Operator](../../programming-guide/language-features/procedures/how-to-define-a-conversion-operator.md)
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
description: "Learn more about: CType Function (Visual Basic)"
title: "CType Function"
description: "Learn more about: CType Operator (Visual Basic)"
title: "CType Operator"
ms.date: 07/20/2015
f1_keywords:
- "vb.CType"
helpviewer_keywords:
- "expression conversion results"
- "explicit data type conversions [Visual Basic]"
- "CType function"
- "CType operator"
- "conversions [Visual Basic], expression"
ms.assetid: dd4b29e7-6fa1-428c-877e-69955420bb72
---

# CType Function (Visual Basic)
# CType Operator (Visual Basic)

Returns the result of explicitly converting an expression to a specified data type, object, structure, class, or interface.

Expand All @@ -35,8 +35,8 @@ Any expression that is legal within an `As` clause in a `Dim` statement, that is
> [!TIP]
> You can also use the following functions to perform a type conversion:
>
> - Type conversion functions such as `CByte`, `CDbl`, and `CInt` that perform a conversion to a specific data type. For more information, see [Type Conversion Functions](type-conversion-functions.md).
> - [DirectCast Operator](../operators/directcast-operator.md) or [TryCast Operator](../operators/trycast-operator.md). These operators require that one type inherit from or implement the other type. They can provide somewhat better performance than `CType` when converting to and from the `Object` data type.
> - Type conversion functions such as `CByte`, `CDbl`, and `CInt` that perform a conversion to a specific data type. For more information, see [Type Conversion Functions](../functions/type-conversion-functions.md).
> - [DirectCast Operator](directcast-operator.md) or [TryCast Operator](trycast-operator.md). These operators require that one type inherit from or implement the other type. They can provide somewhat better performance than `CType` when converting to and from the `Object` data type.

`CType` is compiled inline, which means that the conversion code is part of the code that evaluates the expression. In some cases, the code runs faster because no procedures are called to perform the conversion.

Expand All @@ -53,8 +53,8 @@ If the data type of `expression` or `typename` is a class or structure you've de
|Keyword|Data types|Argument relationship|Run-time failure|
|---|---|---|---|
|`CType`|Any data types|Widening or narrowing conversion must be defined between the two data types|Throws <xref:System.InvalidCastException>|
|[DirectCast Operator](../operators/directcast-operator.md)|Any data types|One type must inherit from or implement the other type|Throws <xref:System.InvalidCastException>|
|[TryCast](../operators/trycast-operator.md)|Reference types only|One type must inherit from or implement the other type|Returns [Nothing](../nothing.md)|
|[DirectCast Operator](directcast-operator.md)|Any data types|One type must inherit from or implement the other type|Throws <xref:System.InvalidCastException>|
|[TryCast](trycast-operator.md)|Reference types only|One type must inherit from or implement the other type|Returns [Nothing](../nothing.md)|

## Overloading

Expand All @@ -66,7 +66,7 @@ Type conversions of dynamic objects are performed by user-defined dynamic conver

## Example

The following example uses the `CType` function to convert an expression to the `Single` data type.
The following example uses the `CType` operator to convert an expression to the `Single` data type.

[!code-vb[VbVbalrFunctions#24](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrFunctions/VB/Class1.vb#24)]

Expand All @@ -76,8 +76,8 @@ For additional examples, see [Implicit and Explicit Conversions](../../programmi

- <xref:System.OverflowException>
- <xref:System.InvalidCastException>
- [Type Conversion Functions](type-conversion-functions.md)
- [Conversion Functions](conversion-functions.md)
- [Type Conversion Functions](../functions/type-conversion-functions.md)
- [Conversion Functions](../functions/conversion-functions.md)
- [Operator Statement](../statements/operator-statement.md)
- [How to: Define a Conversion Operator](../../programming-guide/language-features/procedures/how-to-define-a-conversion-operator.md)
- [Type Conversion in the .NET Framework](../../../standard/base-types/type-conversion.md)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Introduces a type conversion operation based on inheritance or implementation.

`DirectCast` does not use the Visual Basic run-time helper routines for conversion, so it can provide somewhat better performance than `CType` when converting to and from data type `Object`.

You use the `DirectCast` keyword similar to the way you use the [CType Function](../functions/ctype-function.md) and the [TryCast Operator](trycast-operator.md) keyword. You supply an expression as the first argument and a type to convert it to as the second argument. `DirectCast` requires an inheritance or implementation relationship between the data types of the two arguments. This means that one type must inherit from or implement the other.
You use the `DirectCast` keyword similar to the way you use the [CType Operator](ctype-operator.md) and the [TryCast Operator](trycast-operator.md) keyword. You supply an expression as the first argument and a type to convert it to as the second argument. `DirectCast` requires an inheritance or implementation relationship between the data types of the two arguments. This means that one type must inherit from or implement the other.

## Errors and Failures

Expand All @@ -29,7 +29,7 @@ Introduces a type conversion operation based on inheritance or implementation.

|Keyword|Data types|Argument relationship|Run-time failure|
|---|---|---|---|
|[CType Function](../functions/ctype-function.md)|Any data types|Widening or narrowing conversion must be defined between the two data types|Throws <xref:System.InvalidCastException>|
|[CType Operator](ctype-operator.md)|Any data types|Widening or narrowing conversion must be defined between the two data types|Throws <xref:System.InvalidCastException>|
|`DirectCast`|Any data types|One type must inherit from or implement the other type|Throws <xref:System.InvalidCastException>|
|[TryCast Operator](trycast-operator.md)|Reference types only|One type must inherit from or implement the other type|Returns [Nothing](../nothing.md)|

Expand Down
1 change: 1 addition & 0 deletions docs/visual-basic/language-reference/operators/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ms.assetid: 3d3421df-fcc5-4888-9249-d78f16774ce6
- [Operator precedence in Visual Basic](operator-precedence.md)
- [Operators listed by functionality](operators-listed-by-functionality.md)
- [Data types of operator results](data-types-of-operator-results.md)
- [CType operator](ctype-operator.md)
- [DirectCast operator](directcast-operator.md)
- [TryCast operator](trycast-operator.md)
- [NameOf operator](nameof.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Introduces a type conversion operation that does not throw an exception.

If an attempted conversion fails, `CType` and `DirectCast` both throw an <xref:System.InvalidCastException> error. This can adversely affect the performance of your application. `TryCast` returns [Nothing](../nothing.md), so that instead of having to handle a possible exception, you need only test the returned result against `Nothing`.

You use the `TryCast` keyword the same way you use the [CType Function](../functions/ctype-function.md) and the [DirectCast Operator](directcast-operator.md) keyword. You supply an expression as the first argument and a type to convert it to as the second argument. `TryCast` operates only on reference types, such as classes and interfaces. It requires an inheritance or implementation relationship between the two types. This means that one type must inherit from or implement the other.
You use the `TryCast` keyword the same way you use the [CType Operator](ctype-operator.md) and the [DirectCast Operator](directcast-operator.md) keyword. You supply an expression as the first argument and a type to convert it to as the second argument. `TryCast` operates only on reference types, such as classes and interfaces. It requires an inheritance or implementation relationship between the two types. This means that one type must inherit from or implement the other.

## Errors and Failures

Expand All @@ -29,7 +29,7 @@ Introduces a type conversion operation that does not throw an exception.

|Keyword|Data types|Argument relationship|Run-time failure|
|---|---|---|---|
|[CType Function](../functions/ctype-function.md)|Any data types|Widening or narrowing conversion must be defined between the two data types|Throws <xref:System.InvalidCastException>|
|[CType Operator](ctype-operator.md)|Any data types|Widening or narrowing conversion must be defined between the two data types|Throws <xref:System.InvalidCastException>|
|[DirectCast Operator](directcast-operator.md)|Any data types|One type must inherit from or implement the other type|Throws <xref:System.InvalidCastException>|
|`TryCast`|Reference types only|One type must inherit from or implement the other type|Returns [Nothing](../nothing.md)|

Expand Down
2 changes: 1 addition & 1 deletion docs/visual-basic/misc/bc30423.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Optional parameters cannot be of the data type of a structure.
## See also

- [Structures and Classes](../programming-guide/language-features/data-types/structures-and-classes.md)
- [CType Function](../language-reference/functions/ctype-function.md)
- [CType Function](../language-reference/operators/ctype-operator.md)
Loading