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.
Solved! Go to Solution.
Solved by R.van.den.Bor. Go to Solution.
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.
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,
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?
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.
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.
so when you comment out + Environment.Newline + s, like in the screenshot below (copying from your original code above)
you are effectively saying TaskDialog.Show("Elements",selSet.Size); which will cause the compiler to throw an error because....
.. 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.
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.
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 😉