|
||
|
|
#1 (permalink) |
|
Newbie
Join Date: Nov 2006
Posts: 54
|
Hello,
when you create a new List for example: Code:
List<BaseClass> m_list = new List<BaseClass>(); Code:
public SubClass : BaseClass
{
....
}
Code:
m_list.Add(new Subclass()); |
|
|
|
|
|
#2 (permalink) | ||
|
Forum Expert
Join Date: Mar 2004
Location: NorthCentral IL, USA
Age: 35
Posts: 3,848
|
C# Lists: Add some elegance to your code
Quote:
__________________
Quote:
Just a Simple Staff Tool You can leave me messages. Ernest Gary Gygax - Quote "I would like the world to remember me as the guy who really enjoyed playing games and sharing his knowledge and his fun pastimes with everybody else." |
||
|
|
|
|
|
#3 (permalink) | |
|
Master of the Internet
|
Quote:
![]()
__________________
go fish |
|
|
|
|
|
|
#4 (permalink) | |
|
Newbie
Join Date: Feb 2007
Posts: 69
|
Quote:
SubClass is BaseClass so it can be used as BaseClass if casted correctly. However, it will be BaseClass so I am not entirely sure what the point would be. |
|
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
Join Date: Oct 2003
Location: Spokane Valley, WA
Age: 24
Posts: 1,528
|
ReadItem<T>() where T : Item
Directly from the Core this represents that if it is based then when you do someting like reader.ReadItem<MyItem>(); it'll reference MyItem etc. Its the same thing with BaseClasses and SubClasses... I have a good example of this as I hold two different types of Dictionary based Dictionary<ManagerGroup, DirectoryInfo> Where I can store my GManagerGroup because it's parent class is ManagerGroup Now if you need to get something of that source then you will need to do something like List<Item> items = new List<Item>(); items.Add( new Gold(1000) ); you then can reference 'if ( items[i] is Gold ) do something'... It is highly recommended to always to use the actual class that your going to store data in there, if your storing multiple "SubClasses" in a List then using BaseClass would be the best... Example: IF I am only storing Gold into a list then I will want to create List<Gold> goldpiles = new List<Gold>(); now if I need to store Gold, Gems, and Jewelry then Its best to reference "Item" in the list...
__________________
Creator of Genesis :: genesisworlds.com -- Genesis is the next replacement program for UO Landscaper & Dragon |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|