I have a room ID stored as an integer in a datagrid and I am trying to modify the room using this as a way of finding the room object.
I get the element with the following:
Dim RoomID As ElementId = TryCast(objRow.Item("Element_Id"), ElementId)
then try to use
Dim objRoom As Room = doc.Element(RoomID)
to get the object, but it gives me an error:
System.NullReferenceException: Object reference not set to an instance of an object.
I know the elementID is the correct number, I am thinking it is the wrong type? or maybe its the Element() that i am doing wrong?!?
Any ideas or examples of something like this that work? c# or vb is fine, thanks!
Hi there,
Instead of trying to cast a numeric value to ElementId, you should create a new instance of ElementId and pass in the value in its constructor:
Something like:
Dim RoomID As New ElementId(objRow.Item("Element_Id"))
I hope this helps.
______________________________________________________________
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
I don't use VB, so it might be different, but for C# the syntax is:
doc.get_Element(RoomID) (in 2012)
or
doc.GetElement(RoomID) (in 2013)
@adam.nagy wrote:Hi there,
Instead of trying to cast a numeric value to ElementId, you should create a new instance of ElementId and pass in the value in its constructor:
Something like:
Dim RoomID As New ElementId(objRow.Item("Element_Id"))
I hope this helps.
Thanks Adam, I tried that but I get the following message:
Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
'Public Sub New(id As Integer)': Argument matching parameter 'id' narrows from 'Object' to 'Integer'.
'Public Sub New(categoryId As Autodesk.Revit.DB.BuiltInCategory)': Argument matching parameter 'categoryId' narrows from 'Object' to 'Autodesk.Revit.DB.BuiltInCategory'.
'Public Sub New(parameterId As Autodesk.Revit.DB.BuiltInParameter)': Argument matching parameter 'parameterId' narrows from 'Object' to 'Autodesk.Revit.DB.BuiltInParameter'.
@peterjegan wrote:I don't use VB, so it might be different, but for C# the syntax is:
doc.get_Element(RoomID) (in 2012)
or
doc.GetElement(RoomID) (in 2013)
Thanks Peter, What would I need to import to use get_element? I get an error that get_element is not a member of 'Autodesk.Revit.DB.Document'.
I really appreciate your time in helping me learn this!
Sorry, it looks like doc.Element(ID) is the correct syntax in VB.
That leads me to think that you are not getting the correct ElementId.
1. How sure are you that you actually have a usable integer at objRow.Item when you retrieve the RoomID? Can you debug or MsgBox the RoomID at that point?
2. I am not familiar with DataGrid, but is there a reason you are storing the ElementId as an integer in the first place? In C#, Arrays, Lists and Dictionaries allow you to specify an ElementId as a storage type.
Hi there,
The error reason for the error message is that you use the inappropriate objec type for this statement
New ElementId();
The constructor of ElementId only accepts the integer. Howeve the value you get directly from DataGrid cell is Object type. You need to convert it to Interger first. Then passed in to the New ElementId().
After construct the right ElementId instance for room, you can use it to retrieve the room by peterjegan suggested.
doc.get_Element(RoomID) (in 2012)
or
doc.GetElement(RoomID) (in 2013)