Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why doesn't this compile?

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
duncan.lithgow
580 Views, 8 Replies

Why doesn't this compile?

Baby steps here in C# and Macros... I'm going through Harry Mattisons udemy course on the API.

 

In the example below, why does it fail to compile when I comment out this bit near the end:

 

/*+ Environment.NewLine + s */

Is it because I've declared 's' but not used it?

 

Here's the whole macro:

 

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

namespace LearnTheRevitAPI
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("479D86D7-AD43-488D-981A-6BC42F8EC6E4")]
    public partial class ThisApplication
    {
        private void Module_Startup(object sender, EventArgs e)
        {

        }

        private void Module_Shutdown(object sender, EventArgs e)
        {

        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion

        public void selectedElement()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            SelElementSet selSet = uidoc.Selection.Elements;
            
            string s = "";
            foreach (Element e in selSet)
            {
                s += e.Name + Environment.NewLine;
            }
        TaskDialog.Show("Elements",selSet.Size/* + Environment.NewLine + s*/);
            
        }
    }
}

Hopefully a simple explanation will teach me something new about the syntax I'm overseeing.

8 REPLIES 8
Message 2 of 9

Why on earth is there no code highlighting here?
Message 3 of 9

And what if you change to selSet.Size.ToString() + "\r\n"
Kind regards,
Remy van den Bor
ICN Solutions B.V.
Liked this post or found it usefull, don't forget the Kudo It won't hurt (at least not me).
Message 4 of 9

Thanks for the reply. I'm spending time getting better aquanted with C# fundamentals. Seems like the right thing to do when I can't quite understand your answer.

Message 5 of 9

Dear Duncan,

Thank you for your query.

I would assume that the part of the statement that you commented out causes the Size property, initially an integer, to be converted to a string.

The suggestion by Remy also converts the Size property value from an integer to a string.

Yes, I agree that getting aquainted with C# and .NET programming fundamentals would help a lot, independently of the Revit API.

An hour or two of C# and .NET programming fundamentals training before moving on the the Revit API will probably save you some significant effort and frustration later on.

I hope this helps.

Good luck!

Best regards,



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 6 of 9

Thanks for the help. I'm getting there slowly. What I don't understand is why do I sometimes need to explicitely convert an integer to a string and sometimes not?

Message 7 of 9

Well it depends, when you want to use an integer as value, most of the time you don't ned to cast it but if you want to use it like in your example about, in a text, you need to use the .ToString() option.
Kind regards,
Remy van den Bor
ICN Solutions B.V.
Liked this post or found it usefull, don't forget the Kudo It won't hurt (at least not me).
Message 8 of 9

TaskDialog.Show requires a string for its MainInstruction argument.

When you concatenate a string and integer, the result is a string.

In this situation, Environment.NewLine is a string and selSet.Size is an integer.

 

When you remove the concatenation, you are trying to call TaskDialog.Show with an integer which is not allowed. Converting the integer to a string with ToString() fixes it.

 

Message 9 of 9
anagnam
in reply to: duncan.lithgow

I know this is an old topic and the SelElementSet class is now obsolete way back in 2015 (now 2016).

 

But this basic error keeps on popping up especially for those new to Revit API and C# like me.

 

So I thought I would dissect the issue for the benefit of those starting out like me 😉

 

So basically, if you type in the "opening parenthesis" after the method "Show", youll get a hint as to what overloads the TaskDialog.Show accepts.

 

In this case, the 3rd overload accepts boths string for the title and the body (mainInstruction) as shown in the screenshot below.

taskdialog.jpg

so when you comment out + Environment.Newline + s, like in the screenshot below (copying from your original code above)

taskdialog2.jpg

 

you are effectively saying  TaskDialog.Show("Elements",selSet.Size); which will cause the compiler to throw an error because....

taskdialog4.jpg

.. selSet.Size is an Integer (not a string, that the TaskDialog.Show expect for the mainInstruction) as shown when you hover your mouse to the word "Size". as shown below.

taskdialog3.jpg

so to make the compiler happy more precisely the TaskDialog.Show  3rd overload, you need to convert selSet.Size (which is an Interger) into string type.

And you do this by calling the ToString() method on the selSet.Size as shown in the screenshot below.

 

taskdialog5.jpg

And now, they are both strings and they satisfied the 3rd overload of TaskDialog.Show and so it will compile properly.

 

I hope this helps 😉

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community