Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am attempting to learn ArcObjects using a tutorial that uses VB.NET and I am trying to convert it to C#. I have Visual C# 2008 Express Edition. This is an ArcMap Add-In project that creates a button that displays a message box with the name of the top most layer in the data frame. Here is the VB.NET code:

Dim pMxDoc As IMxDocument
pMxDoc = My.ArcMap.Application.Document
Dim pMap As IMap
pMap = pMxDoc.FocusMap 
Dim pFLayer As IFeatureLayer2
pFLayer = pMap.Layer(0)

Dim strDisplayField As String
strDisplayField = pFLayer.DisplayField

MsgBox(strDisplayField, vbOKOnly, "Test")

My conversion in C# so far:

using ESRI.ArcGIS.ArcMapUI:
using ESRI.ArcGIS.Carto;

IMxDocument pMxDoc = ArcMap.Application.Document as IMxDocument;
IMap pMap = pMxDoc.FocusMap;
IFeatureLayer2 pFLayer = pMap.

There is no 'Layer' property. Only 'LayerCount' to choose from. I even tried this:

IFeatureLayer2 pFLayer = ArcMap.Application.Document as IFeatureLayer2;
pFLayer = pMap.

this didn't work either. I have looked all over the ArcMap Object Diagram and I can't find the FeatureLayer2 class anywhere. According to ArcObjects SDK for .NET there is a Layer member for the IMap interface, but I don't have access to that. Any help would be grealty appreciated.

share|improve this question
    
Can you post a link to the tutorial you are using? –  blah238 Feb 15 '13 at 22:21
    
e-education.psu.edu/geog489/node/2084 –  KFP Feb 18 '13 at 16:17
add comment

2 Answers

Try this:

IFeatureLayer GetFirstFeatureLayer()
{
    IMxDocument document = ArcMap.Document;
    IMap map = document.FocusMap;
    ILayer layer = map.get_Layer(0);
    return layer as IFeatureLayer;
} 
share|improve this answer
    
This will not work in C# 3.0 (the version of C# supported by .NET 3.5 and VS2008). Indexed properties were added to C# 4.0 (the version of the language supported by .NET Framework 4.0 and Visual Studio 2010). –  blah238 Feb 16 '13 at 2:22
    
Also your use of a ternary operator on the last line is redundant, as will return null by default if the cast failed (as I explained in my answer). –  blah238 Feb 16 '13 at 11:11
    
Well now my comments just look silly. Thanks for editing your answer though :) –  blah238 Feb 16 '13 at 15:00
    
thanks for your help to correcting me :) –  Emi Feb 17 '13 at 16:10
add comment

It sounds like you need to spend some time reading the documentation. I would start with the topic Using ArcObjects (COM-based) in .NET.

You are not using the right interfaces and classes. You cannot query interface (QI) from IDocument to IFeatureLayer2, for example. Avoid using the as operator and use explicit casting instead. This will immediately result in an InvalidCastException if no compatible interfaces were found, aiding in debugging such issues, rather than setting the object to null and later resulting in the potentially misleading NullReferenceException in the case of as.

Also in C# 3.0, properties that take parameters (such as IMap.Layer) are accessed with a special getter method, e.g. get_Layer(i) instead of Layer[i]. It was not until C# 4.0, when support for indexed properties was added, that you can use the Layer[i] syntax to specify the index parameter. Since you are using Visual Studio 2008 you are limited to C# 3.0. Another thing to keep in mind is that ArcGIS 10.0 is limited to .NET 3.5, and while 10.1 works with .NET 4.0, it is not included in its installer.

SharpDevelop does a pretty reasonable job of converting between .NET languages. It can convert your entire solution at once, or individual files. I have not tried converting an add-in project before, so that might take some fiddling.

share|improve this answer
    
I'm wondering if that is going to be able to interpret proprietary things like ESRI's arcobjects? –  KFP Feb 15 '13 at 21:15
    
Can't hurt to try it. It will create a separate folder for the converted solution. –  blah238 Feb 15 '13 at 21:18
    
Oh, I've spent alot of time in the ArcObjects SKE help, etc. Still confused and that's why I posted the question. It seems as though any search on questions regarding arcobjects ends up with people just refering them to ESRI's help which in my honest opinion is very convoluted. All I'm trying to do is to get a start and to build off of that. As I stated originally, I don't have the property listed within VS and I've tried adding the appropriate references. –  KFP Feb 15 '13 at 21:32
    
It is not in the scope of this site to teach you how to program with ArcObjects. Write a focused, answerable question if you want a better answer. –  blah238 Feb 15 '13 at 21:45
    
Did you try get_Layer? –  blah238 Feb 15 '13 at 21:53
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.