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: 

Empty IList<Level> collection

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
DavidVeld
1933 Views, 3 Replies

Empty IList<Level> collection

Does anybody know if one can populate a IList<Level> without using a FilteredElementCollector ?

 

Its impossible to declare a public ILIst as:  IList<Level> SelectedLevels = new IList<Level>();

since its what I believe a "abstract" class

 

I would like to create an empty IList<Level> SelectedLevels, then I can add Levels to this by using;

SelectedLevels.Add(Level);

 

but without content when adding an Level, I get message that IList<Level> cannot be null.

(Object reference not set to an instance of an object Exception)

 

So How can I create an Empty IList<Level> ?

 

Thanks

 

 

 

 

 

3 REPLIES 3
Message 2 of 4
DavidVeld
in reply to: DavidVeld

I noticed that using the following code is a solution:

 

IList<Level> SelectedLevels= new FilteredElementCollector(doc).OfClass(typeof(Level)).Cast<Level>().OrderBy(l => l.Elevation).ToList(); ;

SelectedLevels.Clear();

 

It fills the SelectedLevels with all levels in a drawing, and then clears it so I can add Levels to it manualy,

but this takes a bit of time and I guess not very coder-friendly. 

Message 3 of 4
arnostlobel
in reply to: DavidVeld

Hello  David,

 

when you need to create a list of items in order to pass them as an instance of IList<T> you need to instantiate a List<T> first, which is a concrete implementation of the IList interface. For example:

List<Level> levels = new List<Level>();
levels.Add(level1);
levels.Add(level2);

 Then you can use it as IList (since it is the interface List derives from):

IList<Level> ilevels = levels;

You can also just create an empty List, cast it right away as IList, and use Add method on the interface, for IList supports the Add method. The two approaches are equal.

IList<Level> ilevels = new List<Level>();
ilevels.Add(level1);
ilevels.Add(level2);

 

Arnošt Löbel

Autodesk Revit R&D

 

 

Arnošt Löbel
Message 4 of 4
ollikat
in reply to: DavidVeld

"It fills the SelectedLevels with all levels in a drawing"

 

Normaly this would be a bit pedantry to mention, but as you seem to have quite little experience with programming I'll say it here. Literally it doesn't fill SelectedLevels. The line you have there means, that a list of levels is created somewhere and you just assign a reference to that list into your SelectedLevel variable.

 

As asuming that you are using C# I also want to say that bear in mind that when you are dealing with objects made out of reference types, you only have a reference to certain data somewhere...assigning those in different variables doesn't copy the data anywhere.

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