0

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.

blah238
35.9k8 gold badges97 silver badges204 bronze badges
asked Feb 15, 2013 at 20:36
2
  • Can you post a link to the tutorial you are using? Commented Feb 15, 2013 at 22:21
  • e-education.psu.edu/geog489/node/2084 Commented Feb 18, 2013 at 16:17

2 Answers 2

1

Try this:

IFeatureLayer GetFirstFeatureLayer()
{
 IMxDocument document = ArcMap.Document;
 IMap map = document.FocusMap;
 ILayer layer = map.get_Layer(0);
 return layer as IFeatureLayer;
} 
answered Feb 15, 2013 at 23:58
3
  • 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). Commented Feb 16, 2013 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). Commented Feb 16, 2013 at 11:11
  • Well now my comments just look silly. Thanks for editing your answer though :) Commented Feb 16, 2013 at 15:00
1

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.

Glorfindel
1,0962 gold badges10 silver badges15 bronze badges
answered Feb 15, 2013 at 21:07
8
  • I'm wondering if that is going to be able to interpret proprietary things like ESRI's arcobjects? Commented Feb 15, 2013 at 21:15
  • Can't hurt to try it. It will create a separate folder for the converted solution. Commented Feb 15, 2013 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. Commented Feb 15, 2013 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. Commented Feb 15, 2013 at 21:45
  • Did you try get_Layer? Commented Feb 15, 2013 at 21:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.