Questions tagged [directcast]

DirectCast is a keyword in VB.NET for explicitly changing the type of an expression, but only via class inheritance or interface implementation.

The DirectCast keyword is one of three type change operators in Visual Basic. It only applies changes due to class inheritance or interface implementation. Compared to the other two, DirectCast is faster and simpler than CType but does not apply the conversions available with the latter, and TryCast is only for reference types but otherwise applies the same changes as DirectCast, which throws an InvalidCastException at runtime if the cast cannot be performed.

Docs

Q: Casting DataTypes with DirectCast, CType, TryCast
Q: Difference between DirectCast() and CType() in VB.Net

37 questions
113
votes
3 answers

Difference between DirectCast() and CType() in VB.NET

I am an experienced C/C++/C# programmer who has just gotten into VB.NET. I generally use CType (and CInt, CBool, CStr) for casts because it is fewer characters and was the first way of casting which I was exposed to, but I am aware of DirectCast and…
Caleb Hearth
  • 3,315
  • 5
  • 30
  • 44
52
votes
11 answers

C#'s equivalent to VB.NET's DirectCast

Does C# have an equivalent to VB.NET's DirectCast? I am aware that it has () casts and the 'as' keyword, but those line up to CType and TryCast. To be clear, these keywords do the following; CType/() casts: If it is already the correct type, cast…
csauve
  • 5,904
  • 9
  • 40
  • 50
42
votes
4 answers

Casting DataTypes with DirectCast, CType, TryCast

Ever since I moved from VB6 to VB.NET somewhere in 2005, I've been using CType to do casting from one data type to another. I do this because it is simply faster to type, used to exist in VB6 and I do not know why I have to be using DirectCast if…
Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
13
votes
1 answer

Directcast & Ctype differences with enums

Public Enum Fruit Red_Apple = 1 Oranges Ripe_Banana End Enum Private Sub InitCombosRegular() Dim d1 As New Dictionary(Of Int16, String) For Each e In [Enum].GetValues(GetType(Fruit)) d1.Add(CShort(e), Replace(e.ToString,…
Eric
  • 3,027
  • 6
  • 29
  • 34
10
votes
2 answers

What effect does DirectCast have an performance and late/early binding?

I always thought DirectCast() was fairly inexpensive, perforance- and memory-wise, and saw it basically as a way of helping me with IntelliSense, e.g. in event handlers: Public Sub myObject_EventHandler(sender As Object, e As System.EventArgs) …
mike
  • 1,627
  • 1
  • 14
  • 37
3
votes
2 answers

How can I do C style casting in VB.NET?

I have a object type variable (control .Tag) that I need to cast to a structured type, and change a member in. This is a contrived but representative example: Public Structure struct_COLOURS Dim ILikeRed as boolean Dim ILikeGreen as boolean End…
3
votes
3 answers

Check Checkboxes in a gridview based on a non-boolean value from a stored procedure

I am trying to check checkboxes on certain rows of a gridview based off of a selection from a dropdown. I have a gridview where the first column is a checkbox (cb_pain), and the second column is an ID (DrugID). The user makes a selection from the…
glitzsfa
  • 353
  • 1
  • 2
  • 14
3
votes
1 answer

How to get Resharper to show a Refactoring that it already has

Whenever Resharper encounters code like this: (treeListNode.Tag as GridLine).AdvertiserSeparation = 5; it presents you with a possible fix (since treeListNode.Tag as GridLine might be null). It says: 'Replace with Direct Cast', which turns the…
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
3
votes
1 answer

Casting nothing into value type: different behavior depending on the number of indirection

In VB, i have different behavior with DirectCast and casting into value type (double, int, ...) depending of the number of indirection DirectCast(nothing, Double) return 0 But, if I try to cast something like a element of a matrix equals to…
Fl0
  • 33
  • 1
  • 4
2
votes
2 answers

'objType' is not defined... Actually, it is, so why is this happening?

As you seen in this picture below, for some reason my DirectCast wont except ANYTHING for the second argument. It says it requires a type, but, it won't take any object at all! Thanks for any help! I'm using VB.net so all .net answers are…
Freesnöw
  • 30,619
  • 30
  • 89
  • 138
2
votes
2 answers

DirectCast(False, Nullable(Of Boolean)) error

I took some code from a C# project and put it into a converter. The original code was: (Nullable)false and the converter said the VB equivalent is: DirectCast(False, Nullable(Of Boolean)) I even compiled the C# project and looked at it in…
John the Ripper
  • 2,389
  • 4
  • 35
  • 61
2
votes
1 answer

In A Dynamic Form, How do I link the contents of a textbox (tbRef) to the button generated along with it?

I am still learing VB and have run into a problem with no decent tutorial. I have created a dynamic form that generates a Textbox and a Update button in each cycle of a loop. I have declared the following global variables: Dim tbRef As…
user6120842
2
votes
1 answer

DirectCast error

I have the following code: Imports System.Collections.ObjectModel Public Class clsCellListExtender Public Class List(Of T) Inherits Collection(Of T) Private _iID As Integer = 0 Protected Overrides Sub InsertItem(index…
tmighty
  • 10,734
  • 21
  • 104
  • 218
2
votes
3 answers

How to display confirmation message with GridView ShowDeleteButton CommandField

I'm trying to attach a Javascript function to confirm a delete of a record in a GridView. I know this can be done more easily using an asp:LinkButton in an ItemTemplate but I am trying to attach it to a CommandField - ShowDeleteButton button. I've…
John Adams
  • 4,773
  • 25
  • 91
  • 131
1
vote
1 answer

How do I dynamically get an object type and cast to it?

How do I get the object type so I can directly cast to it? This is the ideal method I would like to execute: Dim MyObjects As New List(Of Object) For Each O As Object In GlobalFunctions.GeneralFunctions.FindControlsRecursive(MyObjects, Form) …
John89
  • 45
  • 1
  • 7
1
2 3