Select to view content in your preferred language

Referencing a method from a different class within an Else statement and using 2 variables with the method

5494
12
Jump to solution
04-27-2015 09:05 AM
TiffanySelvidge1
Frequent Contributor

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.

Any assistance will be appreciated.

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. I reference the FacetClass namespace (SingleFacet1) within the 'using' section although both SingleFacetForm1 and FacetClass are from the same namespace.

 
public partial class SingleFacetForm1 : Form
    {

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

            //IFeatureLayer pFLayer;
            IGeoFeatureLayer pSearchLayer;
            IFeatureClass pSearchFC;
            IFeatureCursor pFCursor;
            IQueryFilter pQueryFilter;
            //pFLayer = pSearchLayer;
            pSearchLayer = (IGeoFeatureLayer)pFLayer;
            pSearchFC = pSearchLayer.DisplayFeatureClass;

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

            pQueryFilter.WhereClause = strSelField + " like '" + facetStr + "%'";
            pFCursor = pSearchFC.Search(pQueryFilter, false);

            if (pSearchFC.FeatureCount(pQueryFilter) < 1)
            {
                ValidFacet = false;
            }
        }
0 Kudos
12 Replies
TiffanySelvidge1
Frequent Contributor

Thank you for explaining! I really appreciate it.

I have one more follow-up question regarding the use of 'bool'. I noticed you changed validFacet to a variable within the body of the code rather than the original use as a method pointing back to public static bool ValidFacet.

My question is this: How come the error 'not all code paths return a value' is not being thrown for ValidFacet when it is never used in the body of the code? In my attempts to use return type values other than 'void' I keep receiving the 'not all code paths' error message. For example:

        public static IPoint NewPoint (double X, double y)

        {

           //create a new point and put the coordinates provided

            IPoint pntNew;

            IPoint newPoint;

            pntNew = new Point();

            pntNew.PutCoords(X, y);

            //return the new geometry

            newPoint = pntNew;

        }

I either have the 'code paths' error or if I change newPoint to NewPoint then I receive a method error. The only way to not have an error is to remove IPoint which seems like it will change the function of NewPoint. So, how come your procedure can use 'bool', use validFacet as a variable, not mention ValidFacet in the code body, and not receive an error?

0 Kudos
DanielRouleau
Occasional Contributor

The key here is in the difference with how VBA and C# return values. In VBA for example, you set the name of the Function to the value you want to return, and this will take that value and send it back to whoever called NewPoint:

Set NewPoint = ptnNew

However in C#, you can't assign a value to the name of the method, so you instead use the keyword return, like so:

IPoint newPoint;

newPoint = pntNew;

return newPoint;

So the Set NewPoint is essentially the same as using the return keyword, which should clear up the errors you are seeing. You can see the last line in the ValidFacet method I wrote is "return validFacet" which ensures that the method will return a value before you reach the end.

TiffanySelvidge1
Frequent Contributor

That is amazing. I went back to the other functions and fixed them up going by what you said here. I just assigned all the variable method dopplegangers the 'as' value stated in the VBA code.

Thank you very much for explaining. This is information that I could not find elsewhere and was not covered in my classes.

0 Kudos