Select to view content in your preferred language

Replacement for Typeof = GetType?

2278
4
08-24-2010 01:04 PM
MatthewBuchanan
Occasional Contributor
This worked in VB6:
dim firstLayer as ILayer
dim dataSet as IDataset
dim workSpace as IWorkspace
dim strWorkingPath as String
If TypeOf firstLayer Is IFeatureLayer Or TypeOf firstLayer Is IRasterLayer Then
    dataSet = firstLayer
    workSpace = dataSet.Workspace
    strWorkingPath = workSpace.PathName
End If


In VB.NET (VS 2005) they say to use GetType, but I can't find any examples that make any sense.
0 Kudos
4 Replies
AndrewMay
Emerging Contributor
Hi
There's lots of ways you can do this - though that doesn't always make it any easier to find a solution...

Try

If firstLayer.GetType().Equals(typeof(ESRI.ArcGIS.Carto.IFeatureLayer)) Then

or

If firstLayer.GetType().ToString().Equals("ESRI.ArcGIS.Carto.IFeatureLayer") Then

Basically GetType is used on an instance of an object (such as firstLayer in your example), and typeof is used when you don't have an instance of the object (e.g. IFeatureLayer interface).

Hope this helps.
0 Kudos
MatthewBuchanan
Occasional Contributor
Thanks
The first option gives me an error right away in designer (blue underline).
 If firstLayer.GetType().Equals(typeof(ESRI.ArcGIS.Carto.IFeatureLayer)) Then

'IFeatureLayer' is a type in 'Carto' and cannot be used as an expression.

If firstLayer.GetType().ToString().Equals("ESRI.ArcGIS.Carto.IFeatureLayer") Then

The second example gives me a run time error: Object reference not set to an instance of an object.

I notice that when I type firstLayer. in the code window, the Intellisense doesn't give me an option for GetType. Is this in a library that I have to reference at the top of my code?
0 Kudos
AndrewMay
Emerging Contributor
Sorry, ignore the first example.  I was copying from C#.  Try this instead:

If (TypeOf (firstLayer) Is ESRI.ArcGIS.Carto.IFeatureLayer) Then

In the second example you're getting an error because you're not setting firstLayer to anything.  In the VB6 code you posted firstLayer is never assigned a value - I assume in your application this isn't the case though?
0 Kudos
MatthewBuchanan
Occasional Contributor
If (TypeOf (firstLayer) Is ESRI.ArcGIS.Carto.IFeatureLayer) Then

So it looks like TypeOf will work after all. This is the same line I had in my code originally (minus the extra brackets). I guess I was confused by the UPGRADE WARNING: TypeOf has new behavior" message. This message appeared in my code after I ran the VB6 to VB.NET upgrade wizard in VS 2005. In the "Click for more" it says that TypeOf must be replaced with GetType.

In Visual Basic 6.0, the   TypeOf function is used in an   If...Then...Else statement to determine whether an object reference is of a specified object type. In this context, a user-defined type is considered to be an object type. 

In Visual Basic 2005, user-defined types (now known as structures) are not object types and cannot be evaluated by the   TypeOf function.


It goes on to say that I should use a structure like this:
' Modified code
Dim m As MyType
Dim mTest As MyType
If m.GetType Is mTest.GetType Then ...


To answer your question, I am setting firstLayer to a layer in the TOC using a loop.
            For i = 0 To map.LayerCount - 1
                firstLayer = map.Layer(i)

So firstLayer is not Null when I get to the GetType line.
If firstLayer.GetType().ToString().Equals("ESRI.ArcGIS.Carto.IFeatureLayer") Then

So in conclusion even though it says to use GetType, I can continue to use TypeOf? It seems to work fine.
0 Kudos