54

What is the Visual Basic equivalent of the following C# boolean expression?

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

Note: The variable data is declared as IEnumerable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steven
  • 13,501
  • 27
  • 102
  • 146

4 Answers4

81

As I recall

TypeOf data Is System.Data.DataView

Edit:
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)
Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 3
    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
  • 1
    @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
  • 2
    @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
  • This seems to work fine in Visual Studio, but at dotnetfiddle.net, the first piece of code causes a compilation error, saying, " is a class type and cannot be used as an expression." Is there something you technically have to import to use the first type of comparison, which Visual Studio imports automatically? – Panzercrisis Nov 21 '17 at 13:16
42

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)

      (except where types involved implement a cast operator)

Community
  • 1
  • 1
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
  • Be very, very careful with the casts. They are not strictly equivalent to the C# versions (there's no real way to get full C# cast semantics in VB). – skolima Dec 14 '16 at 11:01
3

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:

VB:

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

C#

Panel pnl = c as Panel;
if (pnl != null) {
    pnl.Visible = false;
}
Nick N.
  • 12,902
  • 7
  • 57
  • 75
  • @Teejay please explain – Nick N. Jul 02 '14 at 08:29
  • `TryCast` is really unefficient in comparison with methods explained in Powerlord answer! – Teejay Jul 02 '14 at 08:31
  • @Teejay So it is better to remove my answer? – Nick N. Jul 02 '14 at 08: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 02 '14 at 08:40
  • 1
    Maybe you should only emphasize the sentence *this way you can use the casted type later on*. – Teejay Jul 02 '14 at 08:43
1

Try this.

GetType(Foo)
SubZero
  • 11
  • 2