Classes and Lists

Bozzy

Active member
Joined
Aug 31, 2007
Messages
27
Programming Experience
Beginner
Hi,

I have a main class called "c_Assets". In that class I have a few sub-classes:
  • Building
  • Terrain
  • Scenic Object

I have made a list of the main classes (AssetsList as new list(of c_Assets))

I wanted this generic list to keep a main list of all the assets so I could easily select them. However, when I try to do this:

Assets.Add(New n_Toolset.c_Assets.Building())


It doesn't work because:
VB.NET:
Value of type 'n_Toolset.c_Assets.Building' cannot be converted to 'n_Toolset.c_Assets'.


Can you help me work this out?


Cheers,
Bozzy
 
Very simple explanation for the exception (intelissense warning i suppose).
The Generic AssetsList will accept only the object of c_Assets type.

VB.NET:
Dim newAssetClass As c_Assets = New c_Assets
'do something with the new instance of the c_Assets class 

AssetsList.Add(newAssetClass)

HTH :)
 
When you say sub-classes I understand this as inheritance, ie Building inherits c_Assets. When you have a list(of c_Assets) it also accepts inherited classes. Another OOP way is using an Interface and declare the List of this type, any object that implements this interface will fit in such list. If the classes are totally unrelated you have to a Object type List (same as ArrayList).
 
The main problem here is that he wants to add not only the class (inherited or not) but rather he wants to add a method ".Building()" which is member of the c_Assets class.
I guess you see the difference John.
 
No, it was expressed that Building was a "sub-class", but I don't think OP knew inheritance before this.
"()" is valid when you create a new instance with a parameterless constructor, if that was your confusion.
 
Back
Top