1

I am transferring a code from a VBA module to a C# arcmap add-in. I am using ArcMap 10.2 and Visual Studio 2010.

I cannot figure out how to translate what appears to be a method from a different class (module) within an ElseIf Not statement that places 2 variables from the current class within the method. The biggest problem for me in translating the code is that I can't really figure out with 100% certainty what the original code is saying with that ElseIf Not statement.


I have encountered another instance where the same situation arises; ValidFacet is used in a different class and with a string parameter. I still cannot figure out exactly what the original vba code is trying to say or how to convert the vba to C#.

If Not ValidFacet(pTopFac) Then
pTopFac = "END OF DATA"
End If

So far I have this (which doesn't work):

if (FacetClass.ValidFacet() != pTopFac)
{
pTopFac = "END OF DATA";
}

Since the statement seems to be checking to see if the variable's value exists in the ValidFacet, I have also tried

if (!FacetClass.ValidFacet.Contains(pTopFac))

The statement does not work but perhaps it is closer to what is needed?

. . .

. . .

This is the code snippet that I am referring to (ValidFacet is the method from a different module):

ElseIf Not ValidFacet(txtFacetNumber.Text & cboFacetLetter.Text) Then
MsgBox "The facet you entered does not exist." & vbCrLf & _
       "Please check and try again.", vbSystemModal

Here it is again within the original VBA code.

Private Sub cmdCreateFacet_Click()
  Dim strFacetNumber As String
  If txtFacetNumber.Text = "" Then
    MsgBox "You must inter a value for the facet.", vbCritical + vbSystemModal
    txtFacetNumber.SetFocus
    Exit Sub
  ElseIf cboFacetLetter.Text = "" And Not optFullFacet.Value Then
    MsgBox "You must inter a value for the facet letter.", vbCritical + vbSystemModal
    cboFacetLetter.SetFocus
    Exit Sub
  ElseIf Not ValidFacet(txtFacetNumber.Text & cboFacetLetter.Text) Then
    MsgBox "The facet you entered does not exist." & vbCrLf & _
           "Please check and try again.", vbSystemModal
    cboFacetLetter.Text = ""
    With txtFacetNumber
      .Text = ""
      .SetFocus
    End With
    Exit Sub
  End If

  strFacetNumber = txtFacetNumber.Text & cboFacetLetter.Text

  DrawMap strFacetNumber
  'MsgBox "Drawing"
  cmdExit_Click

End Sub 

Here is my C# translation so far. I have created a different class (FacetClass) for the ValidFacet method.

 private void searchBtn_Click(object sender, EventArgs e)
        {
            string facetNumberstr;


            if (facetNumbertxt.Text == "")

            {
                MessageBox.Show(new Form {TopMost = true }, "You must enter a value for the facet.");
                facetNumbertxt.Focus();
            }
                else if (qFacetCmb.Text == "" && !(wFacetrb.Checked))
                {
                    MessageBox.Show(new Form {TopMost = true }, "You must enter a value for the facet letter.");
                    qFacetCmb.Focus();
                }
                    else (FacetClass.ValidFacet !=(facetNumbertxt.Text && qFacetCmb.Text))
                    {    
                        MessageBox.Show(new Form {TopMost = true }, "The facet you entered does not exist." + newLine + " " + "Please check and try again.");
                        qFacetCmb.Text = "";
                        facetNumbertxt.Text = "";
                        facetNumbertxt.Focus(); 
                    }

            facetNumberstr = facetNumbertxt.Text + qFacetCmb.Text;


            facetNumberstr.DrawMap facetNumberstr;
            //facetNumberstr = new Draw(Map);
            //Draw(Map) (facetNumberstr);
            MessageBox.Show("Drawing");
            //cancelBtn_Click(); 
            cancelBtn_Click

        }

And just in case it helps, here is my C# code for ValidFacet:

    public static void ValidFacet()
    {
        string newLine = System.Environment.NewLine;

        IMxDocument pMxDoc = ArcMap.Document;
        IMap pMap = pMxDoc.FocusMap;
        IFeatureLayer pFLayer;
        bool ValidFacet = true; 
        string facetStr = "";      
        int y;

        pFLayer = (IFeatureLayer)pMap.get_Layer(0); 

        for (y = 0; y <= pMap.LayerCount - 1; y++)
        {
            if (pMap.get_Layer(y).Name == "Facet")
            {
                pFLayer = pMap.get_Layer(y) as IFeatureLayer;
            }

            else if (pFLayer as IFeatureLayer == null)
            {
                MessageBox.Show("Failed to find the Facet Layer." + newLine + "Please check and try again");
                ValidFacet = false;
            }

        }
11
  • What error are you getting? Something about your variable not existing?
    – Branco
    Apr 27, 2015 at 14:04
  • More likely, this is going to be an error where your variables/properties aren't accessible in another class. You either need to create and pass an instance of that class (FacetClass) or make it a static/shared (C#/VB) class. Passing an instance is the more preferred to keep a separation between your classes.
    – Branco
    Apr 27, 2015 at 14:09
  • Only assignment, call, increment, decrement and new object expressions can be used as a statement.
    – MapinTX
    Apr 27, 2015 at 14:10
  • I am referencing the class in the 'using' section and the current class does appear to recognize FacetClass as a class and ValidFacet as one of its methods.
    – MapinTX
    Apr 27, 2015 at 14:13
  • Are they within the same namespace?
    – Branco
    Apr 27, 2015 at 14:38

1 Answer 1

2

Thanks to Daniel from geonet, the problem has been solved. As far as referencing ValidFacet is concerned, the app needed to be changed from 'void' to 'bool'. Daniel also corrected various problems with ValidFacet so that it is now:

    public static bool ValidFacet(string facetStr)
    {

        IMxDocument pMxDoc = ArcMap.Document;
        IMap pMap = pMxDoc.FocusMap;            
        bool validFacet = true; 

        //pFLayer = (IFeatureLayer)pMap.get_Layer(0);
        IFeatureLayer pFlayer = null;


        for (int y = 0; y <= pMap.LayerCount - 1; y++)
        {
            if (pMap.get_Layer(y).Name == "Facet")
            {
                pFlayer = pMap.get_Layer(y) as IFeatureLayer;
                break; //stop looking, we found it
            }
        }

        if (pFlayer == null)
        {
            MessageBox.Show("Failed to find the Facet Layer." + Environment.NewLine + "Please check and try again");
            validFacet = false;
        }

        else
        {
            IGeoFeatureLayer pSearchLayer = pFlayer as IGeoFeatureLayer;

            if (pSearchLayer != null)
            {
                IFeatureClass pSearchFC = pSearchLayer.DisplayFeatureClass;

                //Set queryfilter and search for pages to update
                IQueryFilter pQueryFilter = new QueryFilter();
                const string strSelField = "qfname";

                pQueryFilter.WhereClause = strSelField + " like '" + facetStr + "%'";

                if (pSearchFC.FeatureCount(pQueryFilter) < 1)
                {
                    //We didn't find anything
                    validFacet = false;
                }
            }
        }

        return validFacet;
    }    

Changing from 'void' to 'bool' also corrected other instances where ValidFacet was referenced. Regarding the snippet I mentioned in my update, I previously changed to

if (!FacetClass.ValidFacet(pTopFac))

And now with the 'bool' update, it appears to work as well.

Your Answer

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

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