简体   繁体   中英

Visual Basic equivalent of C# type check

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 .

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)

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)

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;
}

试试这个。

GetType(Foo)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM