tags:

views:

675

answers:

4

I'm a C# programmer who is forced to use VB (eh!!!!). I want to check multiple controls state in one method, in C# this would be accomplished like so:

if (((CheckBox)sender).Checked == true)
{
    // Do something...
}
else
{
    // Do something else...
}

So how can I accomplish this in VB?

+6  A: 

C#:

(CheckBox)sender

VB:

CType(sender, CheckBox)
Adam Robinson
Just to clarify is Ctype is IL equivalent of (int)x boxing? or DirectCast. I thought DirectCast is the exact equivalent of this.
dr. evil
This casts are not equivalent. The VB version is actually a lexical cast, not a CLR cast.
JaredPar
The C# (CheckBox) -style cast lives somewhere in between DirectCast and CType(). DirectCast is _more_ strict, CType is closer (not the same as) to Convert.To____()
Joel Coehoorn
Wanted to refine my early comment. In this case the casts are equivalent. As a general rule though they are not as CType will consider certain lexical casts in addition to actual CLR hierarchy and conversion operators.
JaredPar
As a VB programmer I would use DirectCast in this circumstance.
pipTheGeek
+2  A: 

Adam Robinson is correct, also DirectCast is available to you.

Andrew Hare
Right. However, CType is generally more analogous, as casting in C# will use explicit conversions as long as you have the target object in the context of a convertible type (ie, not as just "object"). DirectCast won't use conversions, but CType will.
Adam Robinson
Very good point.
Andrew Hare
+6  A: 

VB actually has 2 notions of casting.

  1. CLR style casting
  2. Lexical Casting

CLR style casting is what a C# user is more familiar with. This uses the CLR type system and conversions in order to perform the cast. VB has DirectCast and TryCast equivalent to the C# cast and as operator respectively.

Lexical casts in VB do extra work in addition to the CLR type system. They actually represent a superset of potential casts. Lexical casts are easily spotted by looking for the C prefix on the cast operator: CType, CInt, CString, etc ... These cast, if not directly known by the compiler, will go through the VB run time. The run time will do interpretation on top of the type system to allow casts like the following to work

Dim v1 = CType("1", Integer)
Dim v2 = CBool("1")
JaredPar
MSDN appears to differ with you on this, as DirectCast requires type equivalence (or inheritance). CType requires compile-time checking, according to http://msdn.microsoft.com/en-us/library/4x2877xb(VS.80).aspx. The runtime is not involved.
Adam Robinson
@Adam, The documentation is incorrect. CType can be a lexical conversion and does involve the VB runtime. Try the following "Dim b as Integer = CType("42", Integer)". This both runs and involves the VB runtime
JaredPar
+1  A: 

DirectCast will perform the conversion at compile time but can only be used to cast reference types. Ctype will perform the conversion at run time (slower than converting at compile time) but is obviously useful for convertng value types. In your case "sender" is a reference type so DirectCast would be the way to go.

Ben
Good point to mention Directcast, as it can be faster...
Rowland Shaw