Problem converting C# to VB

mooman_fl

Centurion
Joined
Nov 3, 2002
Messages
193
Location
Florida, USA
I finally ran into something I couldn't translate easily. Maybe one of you can help.

Code:
EXListViewItem lstvItem = this.GetItemAt(e.X, e.Y) as EXListViewItem;

Never seen the "as" keyword used that way in C# before and not sure quite what to do with in in VB
 
The GetItemAt method assumably returns an object (or type Object), the as keyword is used to cast the object to the left of it to the type listed on the right.
C#:
// cast the returned object to type EXListViewItem
this.GetItemAt(e.X, e.Y) as EXListViewItem
// different way of writing the same thing
(EXListViewItem)this.GetItemAt(e.X, e.Y)
In VB I believe you would use DirectCast. The code would look something like the code below, forgive me if thats not entirely right haven't got Visual Studio handy and casting isn't somthing I've handled much in VB.
Visual Basic:
Dim lstvItem As EXListViewItem = DirectCast(this.GetItemAt(e.X, e.Y), EXListViewItem)
 
IIRC the 'as' keyword in C# will cast the variable to the correct type if it can or return null if it can't.

The vb equivalent would be something like
Visual Basic:
//EXListViewItem lstvItem = this.GetItemAt(e.X, e.Y) as EXListViewItem;
dim lstvItem as EXListViewItem
if typeof(Me.GetItemAt(e.x, e.y)) is EXListViewItem then
    DirectCast(Me.GetItemAt(e.x, e.y), EXListViewItem )
end if

although I haven't tested it (or even compiled it too be honest) it should give you the general idea
 
Actually, it's nothing to do with DirectCast.

In VB2005, the equivalent is:
Dim lstvItem As EXListViewItem = TryCast(Me.GetItemAt(e.X, e.Y), EXListViewItem)

In VB2003, the equivalent is:
Dim lstvItem As EXListViewItem = CType(IIf(TypeOf Me.GetItemAt(e.X, e.Y) Is EXListViewItem, Me.GetItemAt(e.X, e.Y), Nothing), EXListViewItem)
 
TryCast is certainly the better solution I couldn't remember the keyword as I didn't have VS handy, plus I use VS 2003. The code provided by PlausiblyDamp would work perfectly fine so to say it's nothing todo with DirectCast is merely a matter of opinion. Your VS 2003 code does pretty much the exact same thing except it uses CType. To the best of my knowledge CType does the same job as DirectCast except it won't error if the object passed in is not of the right type. Since the code checks the type anyway this is somewhat irrelevant.
 
TryCast is a better solution but only for .Net / VS 2005.

However for .Net 1 / VS 2002 / 2003 users -

DirectCast will only work if the object is of the same type (or inherited from / implements) as the target type.

CType will succeed if the object can be converted to the target type.

In this respect they are fundamentally different. CType will always work where DirectCast would, however the reverse is not the case - DirectCast can fail where a CType would succeed. Regardless of performance etc. I would prefer to be explicit and use a conversion when I'm converting and a cast when I am casting as this makes the resultant code cleaner and easier to read.

Attempting to us the c# 'as' keyword will fail when converting between types without an implicit conversion operator (it works if an implicit one is present, but if that was the case the casting would be unnecessary anyway); based on this I treated it as behaving like DirectCast rather than CType as CType would have resulted in behaviour different to the C# original.
 
Thanks for that explaination. I wasn't aware of the finer points of those types of statements and I am sure that bit of knowledge will come in very handy later. :D
 
Cags & Plausibly Damp:
You're right - for 2003, DirectCast is indeed just as good.

Since both our solutions check the type anyway before casting, the behavior will be identical (even though the behavior of DirectCast and CType on their own may differ).
 
Back
Top