Solved

Help with overcoming C# .NET error - The name 'ViewExists' does not exist in current context.

Posted on 2014-03-05
4
986 Views
Last Modified: 2014-03-11
Please note code below where at this line item:

if (ViewExists(sNewNameCheck) == true)

it throws an error - The name 'ViewExists' does not exist in current context.

I'm new in C# .NET so please forgive me but I'd thought that it would work since the public bool ViewsExist(string sViewName) does exist within the same context.


Please note full listing of code here:  (has more than just the two areas of concern)  Some of the code does have Revit api .dlls though.

Also please note the two attachment listings of what ViewsExist looks like and the line item that is calling it!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Autodesk.Revit.DB;
using System.Diagnostics;
using Autodesk.Revit.UI;


namespace Copy_Views
{
    public partial class frmCopyViews : System.Windows.Forms.Form
    {
        static Autodesk.Revit.DB.Document _doc = null;
        private ExternalEvent m_ExEvent;
        private ExternalEventHandler m_Handler;
        //protected override void OnFormClosed(FormClosedEventArgs e)
        //{
        //    // we own both the event and the handler
        //    // we should dispose it before we are closed
        //    m_ExEvent.Dispose();
        //    m_ExEvent = null;
        //    m_Handler = null;

        //    // do not forget to call the base class
        //    base.OnFormClosed(e);
        //}

        public void EnableCommands(bool status)
        {
            foreach (System.Windows.Forms.Control ctrl in this.Controls)
            {
                ctrl.Enabled = status;
            }
       //     this.btnClose.Enabled = true;

            if (status == true)
                this.TopMost = true;
            else
                this.TopMost = false;

        }

        public frmCopyViews(ExternalEvent exEvent, ExternalEventHandler handler, Autodesk.Revit.DB.Document doc)
        {
            _doc = doc;
            InitializeComponent();

            m_ExEvent = exEvent;
            m_Handler = handler;
            buildGrid();
        }

        public bool ViewsExist(string sViewName)
        {


            // Filtered element collector is iterable
            string snamedata = "";
           
            IEnumerable<View> viewList = new FilteredElementCollector(_doc)
             .OfClass(typeof(View))
             .Cast<View>()
             .Where(q => !q.IsTemplate && q.ViewType != ViewType.SystemBrowser &&
                 q.ViewType != ViewType.ProjectBrowser)
             .OrderBy(c => c.ViewType).ThenBy(n => n.Name);

            foreach (View v in viewList)
            {
                snamedata =  v.Name;
                if (snamedata == sViewName)
                {
                    return true;

                }
            }

            return false;
            
        }

        public void buildGrid()
        {

            // Filtered element collector is iterable
            string namedata = "";
            DataTable dt = new DataTable();
            dt.Columns.Add("chk", Type.GetType("System.Boolean"));
            dt.Columns.Add("Name", Type.GetType("System.String"));
            dt.Columns.Add("Type", Type.GetType("System.String"));
            dt.Columns.Add("ID", Type.GetType("System.Int32"));
            IEnumerable<View> viewList = new FilteredElementCollector(_doc)
             .OfClass(typeof(View))
             .Cast<View>()
             .Where(q => !q.IsTemplate && q.ViewType != ViewType.SystemBrowser &&
                 q.ViewType != ViewType.ProjectBrowser)
             .OrderBy(c => c.ViewType).ThenBy(n => n.Name);

            foreach (View v in viewList)
            {
                DataRow dr = dt.NewRow();
                dr[0] = "false";
                dr[1] = v.Name;
                dr[2] = v.ViewType.ToString();
                dr[3] = v.Id.IntegerValue;
                dt.Rows.Add(dr);
                namedata += v.ViewType.ToString() + " " + v.Name + "\n";
                Debug.Print(v.Name);
            }

            dt.AcceptChanges();
            this._ViewData = dt;
        }

        public DataTable  _ViewData { get; set; }


        private void frmCopyViews_Load(object sender, EventArgs e)
        {

            DataTable dt = _ViewData.DefaultView.ToTable(true, "type");
            foreach (DataRow dr in dt.Rows)
            {
                comboBox1.Items.Add(dr[0].ToString());
            }
            
            comboBox1.Items.Insert(0, "(All)");
           SetGrid( _ViewData);
            comboBox1.Text = "(All)";
            
        }

        void GetTotal()
        {
           
        }
        void SetGrid(object obj1)
        {
            dataGridView1.DataSource = obj1;

            dataGridView1.Columns["ID"].Visible = false;
            dataGridView1.Columns["Type"].Width = 100;
            dataGridView1.Columns["Name"].Width = 300;
            dataGridView1.Columns["chk"].Width = 30;
            lbltotal.Text = dataGridView1.RowCount.ToString();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > 0)
            {
                DataRow[] drows = _ViewData.Select("type='" + comboBox1.Text  + "'");
               
                SetGrid(GetTableFromrows(drows));
            }
            else
                SetGrid( _ViewData);
            
        }
        private DataTable GetTableFromrows(DataRow[] drows)
        {
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("chk", Type.GetType("System.Boolean"));
            dt1.Columns.Add("Name", Type.GetType("System.String"));
            dt1.Columns.Add("Type", Type.GetType("System.String"));
            dt1.Columns.Add("ID", Type.GetType("System.Int32"));
           
            foreach (DataRow dr in drows)
            {
                DataRow drow = dt1.NewRow();
                drow[0] = dr[0];
                drow[1] = dr[1];
                drow[2] = dr[2];
                drow[3] = dr[3];
                dt1.Rows.Add(drow);
            }
            return dt1;
        }


        void FilterByText(string _filter  )
        {
            DataRow[] drow;
            if (comboBox1.SelectedIndex > 0)
                drow = _ViewData.Select("Name " + _filter + " like '%" + txtName.Text + "%' and type ='" + comboBox1.Text + "'");
            else

                drow= _ViewData.Select("Name " + _filter + " like '%" + txtName.Text + "%'");
                SetGrid(GetTableFromrows(drow));
        }

        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                dataGridView1.Rows[i].Cells[0].Value = chkAll.Checked;
            }          
        }

        private void btnAction_Click(object sender, EventArgs e)
        {
            m_ExEvent.Raise();



            //DialogResult = System.Windows.Forms.DialogResult.OK;
            //Close();

            //for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            //{
            //    string checkvalue = this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();

            //    if (checkvalue == "True")
            //    {
            //        string sID = "";
            //        string sNewname = "";
            //        string sOldname = "";
            //        string sFind = txtAdd.Text;
            //        string sReplace = txtReplace.Text;
            //        sID = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
            //        sNewname = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
            //        sOldname = sNewname;
            //        if (sFind != "" && sReplace != "")
            //        {
            //            sNewname = sNewname.Replace(sFind, sReplace);
            //        }

            //        txtData.Text = sNewname;
            //    }
            //}
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            FilterThis(sender, e);
        }

        private void FilterThis(object senderValue, EventArgs eValue)
        {
            string filter = "";
            if (txtName.Text.Length > 0)
            {
                if (rbContains.Checked)
                    filter = "";
                else
                    filter = " not ";
                FilterByText(filter);
            }
            else
            {
                comboBox1_SelectedIndexChanged(senderValue, eValue);
            }
        }

        private void rbReplace_CheckedChanged(object sender, EventArgs e)
        {
            if (rbReplace.Checked == true)
            {
                txtReplace.Visible = true;
                lblReplace.Visible = true;
                lblAddText.Text = "Find";
                //gbCopyOptions.Text = "Find / Replace Options";
                //btnAction.Text = "Find / Replace";
            }
            else
            {
                txtReplace.Visible = false;
                lblReplace.Visible = false;
                lblAddText.Text = "Add Text";
                //gbCopyOptions.Text = "Copy Options";
                //btnAction.Text = "Copy";
            }
        }

        public string getName()
        {
            return txtName.Text;
        }

        public IList<ViewCopy> getViewInformation()
        {
            IList<ViewCopy> views = new List<ViewCopy>();

            string sID = "";
            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                string checkvalue = this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();

                if (checkvalue == "True")
                {
                 
                    string sNewname = "";
                    string sOldname = "";
                    string sFind = txtAdd.Text;
                    string sReplace = txtReplace.Text;

                    sID = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
                    sNewname = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                    sOldname = sNewname;

                    if (sFind != "" && sReplace != "")
                    {
                        sNewname = sNewname.Replace(sFind, sReplace);
                    }

                    ViewCopy view = new ViewCopy();
                    views.Add(view);
                    view.ViewID = sID;
                    view.NewName = sNewname;
                    view.OldName = sOldname;

                    //ids.Add(sID);
                    //txtData.Text = sNewname;
                }
            }

            return views;
        }

        private void txtName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            //
            // Detect the KeyEventArg's key enumerated constant.
            //
            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
            {

                ActiveControl = btnChange;


            }

        }

        private void btnChange_Enter(object sender, EventArgs e)
        {
            FilterThis(sender, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnList_Click(object sender, EventArgs e)
        {

        }

    }


    public class ExternalEventHandler : IExternalEventHandler
    {
        public string GetName()
        {
            return "ViewCopy";
        }

        public void Execute(UIApplication app)
        {
            copyTheView(app.ActiveUIDocument.Document);
            externalApp.thisApp.WakeFormUp();
        }

        public void copyTheView(Document doc)
        {
            IList<ViewCopy> viewsToCopy = externalApp.thisApp.getViews();
            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Transaction Name");
                foreach (ViewCopy viewToCopy in viewsToCopy)
                {
                    int idInt = Convert.ToInt32(viewToCopy.ViewID);
                    ElementId id = new ElementId(idInt);
                    Element element = doc.GetElement(id);
                    View view = element as View;

                   

                    string sNewNameCheck = "";
                    sNewNameCheck = viewToCopy.NewName.ToString();
                    
                    if (ViewExists(sNewNameCheck) == true)

                    {
                        View newView = doc.GetElement(view.Duplicate(ViewDuplicateOption.Duplicate)) as View;
                        newView.ViewName = viewToCopy.NewName;

                    }
                }
                tx.Commit();
            }
        }

    }


}

Open in new window

ExpertsExchange1.PNG
ExpertsExchange2.PNG
0
Comment
Question by:stephenlecomptejr
  • 2
4 Comments
 
LVL 40
ID: 39908368
Typing error.

The method is called ViewsExists, with a s after View, but you are calling it without the s : ViewExist.

ViewsExist
ViewExist
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 39908383
I was about to do a major face plant into palm on that one.  

However as I copied/pasted the correct syntax it is still giving me the error - please note attachment.
ExpertsExchange3.PNG
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39908388
That's because you're working with two different classes. ViewsExist is a part of the frmCopyViews class; the method you are trying to call from, copyTheView, is a member of the ExternalEventHandler class. You'll probaly want to break the ViewsExist out into a utility class so that both classes can use it, if that's your intention.
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 39922588
Made the following class called Existing.cs

And then used this coding:

                    if (Copy_Views.Existing.ViewsExist(sNewNameCheck, doc) == true)
                    {
                        TaskDialog.Show("Warning", "View already exists!");
                    }

And it rebuilt without an error.

Thank you for your reply.  Sorry about the delay.   I was a little lazy expecting some code back but it was nice instead to try and figure it out on my own with applying what you were saying.  Again thank you very much!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace Copy_Views
{
    class Existing
    {

        public static bool ViewsExist(string sViewName, Document sRevitDoc)
        {


            // Filtered element collector is iterable
            string snamedata = "";

            IEnumerable<View> viewList = new FilteredElementCollector(sRevitDoc)
             .OfClass(typeof(View))
             .Cast<View>()
             .Where(q => !q.IsTemplate && q.ViewType != ViewType.SystemBrowser &&
                 q.ViewType != ViewType.ProjectBrowser)
             .OrderBy(c => c.ViewType).ThenBy(n => n.Name);

            foreach (View v in viewList)
            {
                snamedata = v.Name;
                if (snamedata == sViewName)
                {
                    return true;

                }
            }

            return false;

        }


    }
}

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now