Take the 2-minute tour ×

What is the VB Equivalent of the following C# boolean expression?

data.GetType() == typeof(System.Data.DataView)

Note: The variable data is declared as IEnumerable.

share|improve this question
Have you tried an online converter, such as developerfusion.com/tools/convert/csharp-to-vb –  Dan Diplo Apr 12 '10 at 20:14

4 Answers 4

up vote 41 down vote accepted

As I recall

TypeOf data Is System.Data.DataView

As James Curran pointed out, this works if data is a subtype of System.Data.DataView as well.

If you want to restrict that to System.Data.DataView only, this should work:

data.GetType() Is GetType(System.Data.DataView)
share|improve this answer
Note, however, if the VB "is" is anyhting like the C# "is", that would be true if data is a DataView or derived from DataView. The original code would only be true if data was specifically a DataView object. –  James Curran Apr 12 '10 at 20:17
@James Curran: Good point, I've updated it with the other way I know of to check that (ironically, the way I originally had it before editing) –  Powerlord Apr 12 '10 at 20:30
@James: VB’s Is is actually identical to object.ReferenceEquals. But Typeof … Is indeed equals C#’s is operator. –  Konrad Rudolph Apr 12 '10 at 20:31

Just thought I'd post a summary for the benefit of C# programmers:

C# val is SomeType

      In VB.NET: TypeOf val Is SomeType

      Unlike Is, this can only be negated as Not TypeOf val Is SomeType

C# typeof(SomeType)

      In VB.NET: GetType(SomeType)

C# val.GetType() == typeof(SomeType)

      In VB.NET: val.GetType() = GetType(SomeType)

      (although Is also works, see next)

C# val.ReferenceEquals(something)

      In VB.NET: val Is something

      Can be negated nicely: val IsNot something

C# val as SomeType

      In VB.NET: TryCast(val, SomeType)

C# (SomeType) val

      In VB.NET: DirectCast(val, SomeType)

share|improve this answer

You could also use TryCast and then check for nothing, this way you can use the casted type later on. If you don't need to do that, don't do it this way, because others are more efficient.

See this example:


    Dim pnl As Panel = TryCast(c, Panel)
    If (pnl IsNot Nothing) Then
        pnl.Visible = False
    End If


Panel pnl = c as Panel;
if (pnl != null) {
    pnl.Visible = false;
share|improve this answer
Really unefficient –  Teejay Jun 26 at 8:40
@Teejay please explain –  Nick N. Jul 2 at 8:29
TryCast is really unefficient in comparison with methods explained in Powerlord answer! –  Teejay Jul 2 at 8:31
@Teejay So it is better to remove my answer? –  Nick N. Jul 2 at 8:31
I don't think you should. You answer is not incorrect, but it's not what OP requested: they only wanted to do a type check, but you went beyond by casting the object (which may not be necessary). If you had needed the casted object, then TryCast or as would have been OK. –  Teejay Jul 2 at 8:40

Try this.

share|improve this answer

Your Answer


