Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 04-12-2008, 02:52 PM   #1 (permalink)
Newbie
 
Join Date: Nov 2006
Posts: 54
Default List<> question

Hello,

when you create a new List for example:

Code:
List<BaseClass> m_list = new List<BaseClass>();
can you add the Class

Code:
public SubClass : BaseClass
{
 ....
}
to the List?

Code:
m_list.Add(new Subclass());
thx
Hurraa is offline   Reply With Quote
Old 04-12-2008, 03:00 PM   #2 (permalink)
Forum Expert
 
Greystar's Avatar
 
Join Date: Mar 2004
Location: NorthCentral IL, USA
Age: 35
Posts: 3,848
Default

C# Lists: Add some elegance to your code
Quote:
It is a fairly common programming scenario to find ourselves with a list of identical objects. In the past, without adequate support from programming languages, we found ourselves writing a lot of searching and sorting code, and that may have put you off using lists in favour of arrays. All that has changed with C# (particularly 2.0) - its implementation of a list makes handling such lists remarkably easy.

For example, given the following class Person:
Code:
public class Person

{

          public int age;

          public string name;

          public Person(int age, string name)

          {

                   this.age = age;

                   this.name = name;

          }

}
We can create a list of Person objects and add six people like so:
Code:
List<person>people = new List<person>();

people.Add(new Person(50, "Fred"));
people.Add(new Person(30, "John"));
people.Add(new Person(26, "Andrew"));
people.Add(new Person(24, "Xavier"));
people.Add(new Person(5, "Mark"));
people.Add(new Person(6, "Cameron"));
C#'s list mechanism provides us with a number of useful methods. Personally, I find ForEach, FindAll and Sort to be very useful. ForEach allows us access to each item in the list. FindAll allows us to search for objects in the list that match a specific condition. Sort allows us to sort the objects in the list. The following code demonstrates how we might use each of these methods:
Code:
Console.WriteLine("Unsorted list");

people.ForEach(delegate(Person p)
   { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

// Find the young
List<person> young = people.FindAll(delegate(Person p) { return p.age < 25; });
Console.WriteLine("Age is less than 25");

young.ForEach(delegate(Person p)
   { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

// Sort by name
Console.WriteLine("Sorted list, by name");
people.Sort(delegate(Person p1, Person p2)
   { return p1.name.CompareTo(p2.name); });

people.ForEach(delegate(Person p)
   { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

// Sort by age
Console.WriteLine("Sorted list, by age");

people.Sort(delegate(Person p1, Person p2)
   { return p1.age.CompareTo(p2.age); });

people.ForEach(delegate(Person p)
   { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });
And here is the output that we should expect:

Unsorted list
50 Fred
30 John
26 Andrew
24 Xavier
5 Mark
6 Cameron

Age is less than 25
24 Xavier
5 Mark
6 Cameron

Sorted list, by name
26 Andrew
6 Cameron
50 Fred
30 John
5 Mark
24 Xavier

Sorted list, by age
5 Mark
6 Cameron
24 Xavier
26 Andrew
30 John
50 Fred

Lists are powerful and result in fewer, and more elegant, lines of code. Hopefully this short example has demonstrated their ease and you will find yourself using them in your day-to-day development activities.
Maybe that will be helpful.
__________________
Quote:
(\__/)
(='.'=)This is Bunny. Copy and paste bunny into your
(")_(")signature to help him gain world domination.
Killable Guards (GS Version)
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."
Greystar is offline   Reply With Quote
Old 04-12-2008, 04:03 PM   #3 (permalink)
Master of the Internet
 
Join Date: Mar 2006
Location: Germany
Age: 17
Posts: 14,785
Send a message via AIM to Suil Ban Send a message via MSN to Suil Ban
Default

Quote:
Originally Posted by Hurraa View Post
Hello,

when you create a new List for example:

Code:
List<BaseClass> m_list = new List<BaseClass>();
can you add the Class

Code:
public SubClass : BaseClass
{
 ....
}
to the List?

Code:
m_list.Add(new Subclass());
thx
Yes, this will work just fine.

__________________
go fish
Suil Ban is offline   Reply With Quote
Old 08-21-2008, 01:48 AM   #4 (permalink)
Newbie
 
Join Date: Feb 2007
Posts: 69
Default

Quote:
Originally Posted by Hurraa View Post
Hello,

when you create a new List for example:

Code:
List<BaseClass> m_list = new List<BaseClass>();
can you add the Class

Code:
public SubClass : BaseClass
{
 ....
}
to the List?

Code:
m_list.Add(new Subclass());
thx
Simple answer: Yes

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.
DragonBoy is offline   Reply With Quote
Old 08-27-2008, 02:23 PM   #5 (permalink)
Forum Expert
 
Ravenal's Avatar
 
Join Date: Oct 2003
Location: Spokane Valley, WA
Age: 24
Posts: 1,528
Default

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
Ravenal is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5